Providing range for autopep8 works.

This commit is contained in:
Chiel92 2015-05-30 11:02:07 +02:00
parent a4533d1769
commit 55fca18a96
2 changed files with 30 additions and 14 deletions

View File

@ -40,7 +40,11 @@ endfunction
" Try all formatters, starting with the currently selected one, until one " Try all formatters, starting with the currently selected one, until one
" works. If none works, autoindent the buffer. " works. If none works, autoindent the buffer.
function! s:TryAllFormatters(...) function! s:TryAllFormatters(...) range
echom a:firstline.", ".a:lastline
"echom line('.')", ".line('v')
"echom line("'<")", ".line("'>")
"echom mode()
" Make sure formatters are defined and detected " Make sure formatters are defined and detected
if !call('<SID>find_formatters', a:000) if !call('<SID>find_formatters', a:000)
return 0 return 0
@ -65,6 +69,12 @@ function! s:TryAllFormatters(...)
return 0 return 0
endif endif
" Check for ranged definition
let formatdef_ranged_var = 'g:formatdef_ranged_'.b:formatters[s:index]
if exists(formatdef_ranged_var)
let formatdef_var = formatdef_ranged_var
endif
" Eval twice, once for getting definition content, " Eval twice, once for getting definition content,
" once for getting the final expression " once for getting the final expression
let &formatprg = eval(eval(formatdef_var)) let &formatprg = eval(eval(formatdef_var))
@ -104,15 +114,17 @@ text = '\n'.join(vim.current.buffer[:])
formatprg = vim.eval('&formatprg') formatprg = vim.eval('&formatprg')
verbose = vim.eval('verbose') verbose = vim.eval('verbose')
p = subprocess.Popen(formatprg, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) p = subprocess.Popen(formatprg, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
#print(text)
stdoutdata, stderrdata = p.communicate(text) stdoutdata, stderrdata = p.communicate(text)
if stderrdata: if stderrdata:
if verbose: if verbose:
formattername = vim.eval('b:formatters[s:index]') formattername = vim.eval('b:formatters[s:index]')
vim.command('echom "Formatter {} has errors: {}. Skipping."'.format(formattername, print('Formatter {} has errors: {}. Skipping.'.format(formattername, stderrdata))
stderrdata)) print('Failing config: {} '.format(repr(formatprg), stderrdata))
vim.command('echom "Failing config: {} "'.format(repr(formatprg), stderrdata))
vim.command('return 0') vim.command('return 0')
else: else:
#print(stdoutdata)
#print('asdf')
vim.current.buffer[:] = stdoutdata.split('\n') vim.current.buffer[:] = stdoutdata.split('\n')
EOF EOF
@ -124,7 +136,7 @@ endfunction
" Create a command for formatting the entire buffer " Create a command for formatting the entire buffer
command! -nargs=? -complete=filetype Autoformat call s:TryAllFormatters(<f-args>) command! -nargs=? -range=% -complete=filetype Autoformat <line1>,<line2>call s:TryAllFormatters(<f-args>)
" Functions for iterating through list of available formatters " Functions for iterating through list of available formatters

View File

@ -5,14 +5,16 @@
" Python " Python
let g:formatdef_autopep8 = '"autopep8 - ".(&textwidth ? "--max-line-length=".&textwidth : "")' let g:formatdef_autopep8 = '"autopep8 - ".(&textwidth ? "--max-line-length=".&textwidth : "")'
"let g:formatdef_ranged_autopep8 = "'autopep8 - --range '.line(\"'<\").' '.line(\"'>\").' '.(&textwidth ? '--max-line-length='.&textwidth : '')"
let g:formatdef_ranged_autopep8 = "'autopep8 - --range '.a:firstline.' '.a:lastline.' '.(&textwidth ? '--max-line-length='.&textwidth : '')"
let g:formatdef_test = '"asdf"' let g:formatdef_test = '"asdf"'
let g:formatdef_another_autopep8 = '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")' let g:formatdef_another_autopep8 = '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")'
if !exists('g:formatters_python') if !exists('g:formatters_python')
let g:formatters_python = [ let g:formatters_python = [
\ 'test',
\ 'another_autopep8',
\ 'autopep8', \ 'autopep8',
\ ] \ ]
"\ 'test',
"\ 'another_autopep8',
endif endif
@ -23,27 +25,29 @@ if !exists('g:formatters_cs')
endif endif
" Generic C, C++, Objective-C
let g:formatdef_clangformat = '"clang-format --assume-filename=".bufname("%")." -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
let g:formatdef_ranged_clangformat = "'clang-format -lines='.line(\"'<\").':'.line(\"'>\").' --assume-filename='.bufname('%').' -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, '.(&textwidth ? 'ColumnLimit: '.&textwidth.', ' : '').(&expandtab ? 'UseTab: Never, IndentWidth: '.&shiftwidth : 'UseTab: Always').'}\"'"
" C " C
let g:formatdef_clangformat_c = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
let g:formatdef_astyle_c = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")' let g:formatdef_astyle_c = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_c') if !exists('g:formatters_c')
"let g:formatters_c = ['clangformat_c', 'astyle_c'] let g:formatters_c = ['clangformat', 'astyle_c']
let g:formatters_c = ['astyle_c'] "let g:formatters_c = ['astyle_c']
endif endif
" C++ " C++
let g:formatdef_clangformat_cpp = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
let g:formatdef_astyle_cpp = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")' let g:formatdef_astyle_cpp = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_cpp') if !exists('g:formatters_cpp')
let g:formatters_cpp = ['clangformat_cpp', 'astyle_cpp'] let g:formatters_cpp = ['clangformat', 'astyle_cpp']
endif endif
" Objective C " Objective C
let g:formatdef_clangformat_objc = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
if !exists('g:formatters_objc') if !exists('g:formatters_objc')
let g:formatters_objc = ['clangformat_objc'] let g:formatters_objc = ['clangformat']
endif endif