-
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
Changes from all commits
67f3e04
85472f9
26c1d9b
8045f9c
8424eb7
fe16318
c102e13
95b5adf
35e4860
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| " The current list of defaults by filetype is: | ||
| " * cpp, proto, javascript: clang-format | ||
| " * go: gofmt | ||
| " * python: autopep8 | ||
|
|
||
|
|
||
| call maktaba#library#Require('codefmtlib') | ||
|
|
@@ -156,6 +157,69 @@ if !exists('s:gofmt') | |
| call codefmtlib#AddDefaultFormatter(s:gofmt) | ||
| endif | ||
|
|
||
| " Formatter: autopep8 | ||
| if !exists('s:autopep8') | ||
| let s:autopep8 = { | ||
| \ 'name': 'autopep8', | ||
| \ 'setup_instructions': 'Install autopep8 ' . | ||
| \ '(https://pypi.python.org/pypi/autopep8/).'} | ||
|
|
||
| function s:autopep8.IsAvailable() abort | ||
| return executable(s:plugin.Flag('autopep8_executable')) | ||
| endfunction | ||
|
|
||
| function s:autopep8.AppliesToBuffer() abort | ||
| return &filetype is# 'python' | ||
| endfunction | ||
|
|
||
| "" | ||
| " Reformat the current buffer with autopep8 or the binary named in | ||
| " @flag(autopep8_executable), only targeting the range between {startline} and | ||
| " {endline}. | ||
| " @throws ShellError | ||
| function s:autopep8.FormatRange(startline, endline) abort | ||
| let l:executable = s:plugin.Flag('autopep8_executable') | ||
| if !exists('s:autopep8_supports_range') | ||
| let l:version_call = | ||
| \ maktaba#syscall#Create([l:executable, '--version']).Call() | ||
| " In some cases version is written to stderr, in some to stdout | ||
| let l:version_output = | ||
| \ version_call.stderr ? version_call.stderr : version_call.stdout | ||
| let 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 | ||
| let l:cmd = [ l:executable, | ||
| \ '--range', ''.a:startline, ''.a:endline, | ||
| \ '-' ] | ||
| let l:input = join(l:lines, "\n") | ||
| else | ||
| let l:cmd = [ l:executable, '-' ] | ||
| " Hack range formatting by formatting range individually, ignoring context. | ||
| let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n") | ||
| endif | ||
|
|
||
| let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() | ||
| let l:formatted = split(l:result.stdout, "\n") | ||
|
|
||
| if s:autopep8_supports_range | ||
| let l:full_formatted = l:formatted | ||
| else | ||
| " Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right. | ||
| let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : [] | ||
| let l:full_formatted = l:before + l:formatted + l:lines[a:endline :] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||
| endif | ||
|
|
||
| call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) | ||
| endfunction | ||
|
|
||
| call codefmtlib#AddDefaultFormatter(s:autopep8) | ||
| endif | ||
|
|
||
| "" | ||
| " Detects whether a formatter has been defined for the current buffer/filetype. | ||
|
|
||
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:
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: