Only use range arguments in visual mode for autopep8.

This commit is contained in:
Chiel ten Brinke 2016-04-06 15:55:27 +02:00
parent 81f5e989c2
commit 713e7c2580
2 changed files with 18 additions and 1 deletions

View File

@ -82,6 +82,12 @@ let g:autoformat_remove_trailing_spaces = 0
To disable or re-enable these option for specific buffers, use the buffer local variants:
`b:autoformat_autoindent`, `b:autoformat_retab` and `b:autoformat_remove_trailing_spaces`.
So to disable autoindent for filetypes that have incompetent indent files, use
```vim
autocmd FileType vim,tex let b:autoformat_autoindent=0
```
You can manually autoindent, retab or remove trailing whitespace with the following respective
commands.

View File

@ -23,9 +23,20 @@ endif
" Python
if !exists('g:formatdef_autopep8')
let g:formatdef_autopep8 = '"autopep8 - --range ".a:firstline." ".a:lastline." ".(&textwidth ? "--max-line-length=".&textwidth : "")'
" Autopep8 will not do indentation fixes when a range is specified, so we
" only pass a range when there is a visual selection that is not the
" entire file. See #125.
let g:formatdef_autopep8 = '"autopep8 -".(g:DoesRangeEqualBuffer(a:firstline, a:lastline) ? " --range ".a:firstline." ".a:lastline : "")." ".(&textwidth ? "--max-line-length=".&textwidth : "")'
endif
" There doesn't seem to be a reliable way to detect if are in some kind of visual mode,
" so we use this as a workaround. We compare the length of the file against
" the range arguments. If there is no range given, the range arguments default
" to the entire file, so we return false if the range comprises the entire file.
function! g:DoesRangeEqualBuffer(first, last)
return line('$') != a:last - a:first + 1
endfunction
if !exists('g:formatters_python')
let g:formatters_python = ['autopep8']
endif