diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 3657c5f..77e9fd4 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -40,7 +40,11 @@ endfunction " Try all formatters, starting with the currently selected one, until one " 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 if !call('find_formatters', a:000) return 0 @@ -65,6 +69,12 @@ function! s:TryAllFormatters(...) return 0 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, " once for getting the final expression let &formatprg = eval(eval(formatdef_var)) @@ -104,15 +114,17 @@ text = '\n'.join(vim.current.buffer[:]) formatprg = vim.eval('&formatprg') verbose = vim.eval('verbose') p = subprocess.Popen(formatprg, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) +#print(text) stdoutdata, stderrdata = p.communicate(text) if stderrdata: if verbose: formattername = vim.eval('b:formatters[s:index]') - vim.command('echom "Formatter {} has errors: {}. Skipping."'.format(formattername, - stderrdata)) - vim.command('echom "Failing config: {} "'.format(repr(formatprg), stderrdata)) + print('Formatter {} has errors: {}. Skipping.'.format(formattername, stderrdata)) + print('Failing config: {} '.format(repr(formatprg), stderrdata)) vim.command('return 0') else: + #print(stdoutdata) + #print('asdf') vim.current.buffer[:] = stdoutdata.split('\n') EOF @@ -124,7 +136,7 @@ endfunction " Create a command for formatting the entire buffer -command! -nargs=? -complete=filetype Autoformat call s:TryAllFormatters() +command! -nargs=? -range=% -complete=filetype Autoformat ,call s:TryAllFormatters() " Functions for iterating through list of available formatters diff --git a/plugin/defaults.vim b/plugin/defaults.vim index a06e623..373b7d9 100644 --- a/plugin/defaults.vim +++ b/plugin/defaults.vim @@ -5,14 +5,16 @@ " Python 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_another_autopep8 = '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")' if !exists('g:formatters_python') let g:formatters_python = [ - \ 'test', - \ 'another_autopep8', \ 'autopep8', \ ] + "\ 'test', + "\ 'another_autopep8', endif @@ -23,27 +25,29 @@ if !exists('g:formatters_cs') 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 -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")' if !exists('g:formatters_c') - "let g:formatters_c = ['clangformat_c', 'astyle_c'] - let g:formatters_c = ['astyle_c'] + let g:formatters_c = ['clangformat', 'astyle_c'] + "let g:formatters_c = ['astyle_c'] endif " 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")' if !exists('g:formatters_cpp') - let g:formatters_cpp = ['clangformat_cpp', 'astyle_cpp'] + let g:formatters_cpp = ['clangformat', 'astyle_cpp'] endif " 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') - let g:formatters_objc = ['clangformat_objc'] + let g:formatters_objc = ['clangformat'] endif