-
Couldn't load subscription status.
- Fork 104
feat(formatters): add autopep8 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
autoload/codefmt.vim
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the setup instructions seem to trail off
It might make sense to generalize now that there is more than one.
Before merging any request you have we will need you to sign the CLA (contributor lisence agreement). Sorry the lawyercats are sticklers for this.
https://github.com/google/vim-codefmt/blob/master/CONTRIBUTING.md
autoload/codefmt.vim
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autopep8 1.0 and later support --range line line, which will give much better results than this hack. Unfortunately, Ubuntu seems to be stuck on a version almost 2 years old (0.9.1) which doesn't support it, so I could see people being stuck on an old version without it. I just filed https://bugs.launchpad.net/ubuntu/+source/autopep8/+bug/1414258 asking Ubuntu to update.
Could you file a feature request along with these changes to support true range formatting, with this as a fallback? "autopep8 --version" runs pretty quick for me, so I don't think running that once per vim session to detect native range support would be a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do u think it will be better to make that choice -- setting plugin.Flag manually or reading autopep --version output once and caching it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If reading autopep8 --version works well, I'd go with that approach. Seems like it will.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, while i gonna store cached value in Flag, then it will be possible to set it manually
or Flags are not suitable for such purpose and intended to be only for storing some default settings, but not any variable data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a flag like that wouldn't be unreasonable, except I figured it should be lazy and flag initialization should be eager (once at plugin installation time). Even though it's a quick check, doing it for N formatters plus the executable() checks all at once might not be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean to add that version check not on plugin initialization but inside FormatRange function, like:
if !s:plugin.Flag('autopep8_version') s:plugin.Flag('autopep8_version', GetVersion())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought lazy flag initialization like that might get confusing, but maybe not. Anyway, my vote would be to wait to add a flag until someone reports they need to override the behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
am i understood right, you mean to initialize version flag like that but not to set it manually by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was suggesting is instead of using a flag, for now use a script-local variable like
let l:executable = s:plugin.Flag('autopep8_executable') if !exists('s:autopep8_supports_range') let l:version_output = \ maktaba#syscall#Create([l:executable, '--version']).Call().stderr let s:autopep8_supports_range = \ matchlist(l:version_output, '\m\Cautopep8 \(\d\+\)\.')[1] >= 1 endif
Then, if for some reason a user reports the version check is not right for them and they'd rather configure it manually, it's easy to replace the script-local variable with flag operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, thanks
There's a list of formatters on line 28 that should be updated to include python, and then if you can run vimdoc on it after that it will update the help file in doc/ to match.
autoload/codefmt.vim
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, just noticed what autopep8 don't output errors like that.
actually i am not even sure if it can return unsuccessful result when using stdin at all (when it faces wrong syntax or smth it is just ignoring it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's fine to get rid of the try/catch and just let the ShellError bubble up. It's caught and handled in codefmt. You can add a @throws ShellError line into the vimdoc for good measure.
69c6c1e to
7a7b2c1
Compare
7a7b2c1 to
8424eb7
Compare
i think it's ready for review
btw, i already signed CLA (you need my e-mail to verify it, or what?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment that the '-' argument means stdin and put the '-' at the end of the list of args (I got a bit confused by the '-', '--range' list)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't find it too confusing, but it could be more obvious at a glance if the lines were broken up differently:
let l:cmd = [ \ l:executable, \ '--range', ''.a:startline, ''.a:endline, \ '-' ]
Matt, you still think a comment would be necessary like that? Was it more that you didn't notice the '-' or just didn't know what to make of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know what to make of it. I thought it was similar to the bash things
which say "all arguments after this one are files" which confused me and
sent me off to read the docs.
Matt
On Jan 26, 2015 11:51 AM, "David Barnett" notifications@github.com wrote:
In autoload/codefmt.vim
#4 (comment):
- " @throws ShellError
- function s:autopep8.FormatRange(startline, endline) abort
- " Hack range formatting by formatting range individually, ignoring context.
- let l:executable = s:plugin.Flag('autopep8_executable')
- if !exists('s:autopep8_supports_range')
let l:version_output =\ maktaba#syscall#Create([l:executable, '--version']).Call().stderrlet s:autopep8_supports_range =\ matchlist(l:version_output, '\m\Cautopep8 (\d+).')[1] >= 1- endif
- call maktaba#ensure#IsNumber(a:startline)
- call maktaba#ensure#IsNumber(a:endline)
- let l:lines = getline(1, line('$'))
- if s:autopep8_supports_range
I didn't find it too confusing, but it could be more obvious at a glance
if the lines were broken up differently:let l:cmd = [ \ l:executable, \ '--range', ''.a:startline, ''.a:endline, \ '-' ]Matt, you still think a comment would be necessary like that? Was it more
that you didn't notice the '-' or just didn't know what to make of it?—
Reply to this email directly or view it on GitHub
https://github.com/google/vim-codefmt/pull/4/files#r23544410.
I am going to wait for dbarnett's LGTM because he knows vimscripts subtleties bettter than I do.
I see that you did sign the CLA, thanks for doing that! The good news is that the CLA is good for all google stuff (except android), so you won't ever have to bother with that again.
LGTM. You can merge when you're happy with the l:cmd part.
autoload/codefmt.vim
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment on what cases you're handling here? To my eye the logic looks a little weird without any info about expected output.
BTW, it reads a little weird not having a space after the \.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so as i described in commit message, in some configurations version is being output into stdout, in some others -- to stderr.
i have no clue why that happens (it seems what i have the same library versions on both PCs there i tested it, but maybe some different environment variables or smth)
yup, that's a typo, i will fix it
feff34f to
d19ef70
Compare
d19ef70 to
35e4860
Compare
Add support for python via autopep8.
Thanks for the great patch!
mb it make sense to generalize them somehow?
because it's almost the same as gofmt one