Multiple formatters. Don't write errors to buffer.

Using python we can check for formatter errors instead of writing the
errors to the buffer. Multiple formatters can be supplied by lists now.

Backward compatibility breaks:
 - Variable names have changed
 - Formatter is specified as one expression
 - gq has been removed, Autoformat should accept ranges
 - python has to be compiled in

To be done:
 - Find easy way to selected default formatter out of available formatter
 - Make autoformat accept ranges
This commit is contained in:
Chiel92 2015-05-18 19:27:00 +02:00
parent 060ee66c08
commit dfd9461f23
2 changed files with 141 additions and 88 deletions

View File

@ -1,57 +1,87 @@
" Function for finding and setting the formatter with the given name
function! s:set_formatprg(...)
" Function for finding the formatters for this filetype
function! s:find_formatters(...)
" Detect verbosity
let s:verbose = &verbose || exists("g:autoformat_verbosemode")
" Extract filetype to be used
let type = a:0 ? a:1 : &filetype
" Support composite filetypes by replacing dots with underscores
let type = substitute(type, "[.]", "_", "g")
" Detect verbosity
let s:verbose = &verbose || exists("g:autoformat_verbosemode")
" Get formatprg config
let s:formatprg_var = "g:formatprg_".type
let s:formatprg_args_var = "g:formatprg_args_".type
let s:formatprg_args_expr_var = "g:formatprg_args_expr_".type
let s:formatters_var = "g:formatters_".type
let b:formatters = []
if !exists(s:formatprg_var)
" No formatprg defined
if !exists(s:formatters_var)
" No formatters defined
if s:verbose
echoerr "No formatter defined for filetype '".type."'."
echoerr "No formatters defined for filetype '".type."'."
endif
return 0
endif
let s:formatprg = eval(s:formatprg_var)
let s:formatprg_args = ""
if exists(s:formatprg_args_expr_var)
let s:formatprg_args = eval(eval(s:formatprg_args_expr_var))
elseif exists(s:formatprg_args_var)
let s:formatprg_args = eval(s:formatprg_args_var)
endif
" Set correct formatprg path, if it is installed
if !executable(s:formatprg)
" Configured formatprg not installed
let s:formatters = eval(s:formatters_var)
if len(s:formatters) == 0
" No formatters defined
if s:verbose
echoerr "Defined formatter ".eval(s:formatprg_var)." is not executable."
echoerr "No formatters defined for filetype '".type."'."
endif
return 0
endif
let &formatprg = s:formatprg." ".s:formatprg_args
let b:formatters = s:formatters
return 1
endfunction
" Set right formatprg before formatting
noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq'
" Function for finding and setting the currently selected formatter
function! s:set_formatter(...)
" Detect verbosity
let s:verbose = &verbose || exists("g:autoformat_verbosemode")
" Make sure formatters are defined and detected
if !call('<SID>find_formatters', a:000)
return 0
endif
" Make sure index exist and is valid
if !exists('b:current_formatter_index')
let b:current_formatter_index = 0
endif
if b:current_formatter_index >= len(b:formatters)
let b:current_formatter_index = 0
endif
let &formatprg = eval(s:formatters[b:current_formatter_index])
return 1
endfunction
" Function for formatting the entire buffer
function! s:Autoformat(...)
" Save window state
let winview=winsaveview()
if call('<SID>set_formatprg', a:000)
if call('<SID>set_formatter', a:000)
" Autoformat code
exe "1,$!".&formatprg
python << EOF
import vim, subprocess
from subprocess import Popen, PIPE
text = '\n'.join(vim.current.buffer[:])
formatprg = vim.eval('&formatprg')
p = subprocess.Popen(formatprg, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = p.communicate(text)
if stderrdata:
vim.command('echoerr "Error communicating with formatter: {}"'.format(stderrdata))
else:
vim.current.buffer[:] = stdoutdata.split('\n')
EOF
else
" Autoindent code
exe "normal gg=G"
@ -63,3 +93,26 @@ endfunction
" Create a command for formatting the entire buffer
command! -nargs=? -complete=filetype Autoformat call s:Autoformat(<f-args>)
" Functions for iterating through list of available formatters
function! s:NextFormatter()
call s:find_formatters()
if !exists('b:current_formatter_index')
let b:current_formatter_index = 0
endif
let b:current_formatter_index = (b:current_formatter_index + 1) % len(b:formatters)
endfunction
function! s:PreviousFormatter()
call s:find_formatters()
if !exists('b:current_formatter_index')
let b:current_formatter_index = 0
endif
let s:l = len(b:formatters)
let b:current_formatter_index = (b:current_formatter_index - 1 + s:l) % s:l
endfunction
" Create commands for iterating through formatter list
command! NextFormatter call s:NextFormatter()
command! PreviousFormatter call s:PreviousFormatter()

View File

@ -1,74 +1,74 @@
if !exists("g:formatprg_cs") | let g:formatprg_cs = "astyle" | endif
if !exists("g:formatprg_args_expr_cs") && !exists("g:formatprg_args_cs")
let g:formatprg_args_expr_cs = '"--mode=cs --style=ansi --indent-namespaces -pcH".(&expandtab ? "s".&shiftwidth : "t")'
endif
if !exists("g:formatters_python") | let g:formatters_python = [] | endif
let g:formatters_python += [
\ '"autopep8 - ".(&textwidth ? "--max-line-length=".&textwidth : "")',
\ '"asdf"',
\ '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")'
\ ]
if !exists("g:formatprg_c") | let g:formatprg_c = "astyle" | endif
if !exists("g:formatprg_args_expr_c") && !exists("g:formatprg_args_c")
let g:formatprg_args_expr_c = '"--mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
endif
"if !exists("g:formatters_cs")
"let g:formatters_cs = ['"astyle --mode=cs --style=ansi --indent-namespaces -pcH".(&expandtab ? "s".&shiftwidth : "t")']
"endif
if !exists("g:formatprg_cpp") | let g:formatprg_cpp = "astyle" | endif
if !exists("g:formatprg_args_expr_cpp") && !exists("g:formatprg_args_cpp")
let g:formatprg_args_expr_cpp = '"--mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
endif
"if !exists("g:formatters_c")
"let g:formatters_c = ['"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")']
"endif
if !exists("g:formatprg_objc") | let g:formatprg_objc = "clang-format" | endif
if !exists("g:formatprg_args_expr_objc") && !exists("g:formatprg_args_objc")
let g:formatprg_args_expr_objc = '"-style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
endif
"if !exists("g:formatprg_cpp") | let g:formatprg_cpp = "astyle" | endif
"if !exists("g:formatprg_args_expr_cpp") && !exists("g:formatprg_args_cpp")
"let g:formatprg_args_expr_cpp = '"--mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
"endif
if !exists("g:formatprg_java") | let g:formatprg_java = "astyle" | endif
if !exists("g:formatprg_args_expr_java") && !exists("g:formatprg_args_java")
let g:formatprg_args_expr_java = '"--mode=java --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
endif
"if !exists("g:formatprg_objc") | let g:formatprg_objc = "clang-format" | endif
"if !exists("g:formatprg_args_expr_objc") && !exists("g:formatprg_args_objc")
"let g:formatprg_args_expr_objc = '"-style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
"endif
if !exists("g:formatprg_python") | let g:formatprg_python = "autopep8" | endif
if !exists("g:formatprg_args_expr_python") && !exists("g:formatprg_args_python")
let g:formatprg_args_expr_python = '"- ".(&textwidth ? "--max-line-length=".&textwidth : "")'
endif
"if !exists("g:formatprg_java") | let g:formatprg_java = "astyle" | endif
"if !exists("g:formatprg_args_expr_java") && !exists("g:formatprg_args_java")
"let g:formatprg_args_expr_java = '"--mode=java --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
"endif
if !exists("g:formatprg_xml") | let g:formatprg_xml = "tidy" | endif
if !exists("g:formatprg_args_expr_xml") && !exists("g:formatprg_args_xml")
let g:formatprg_args_expr_xml = '"-q -xml --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -wrap ".&textwidth'
endif
"if !exists("g:formatprg_xml") | let g:formatprg_xml = "tidy" | endif
"if !exists("g:formatprg_args_expr_xml") && !exists("g:formatprg_args_xml")
"let g:formatprg_args_expr_xml = '"-q -xml --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -wrap ".&textwidth'
"endif
if !exists("g:formatprg_xhtml") | let g:formatprg_xhtml = "tidy" | endif
if !exists("g:formatprg_args_expr_xhtml") && !exists("g:formatprg_args_xhtml")
let g:formatprg_args_expr_xhtml = '"-q --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -asxhtml -wrap ".&textwidth'
endif
"if !exists("g:formatprg_xhtml") | let g:formatprg_xhtml = "tidy" | endif
"if !exists("g:formatprg_args_expr_xhtml") && !exists("g:formatprg_args_xhtml")
"let g:formatprg_args_expr_xhtml = '"-q --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -asxhtml -wrap ".&textwidth'
"endif
if !exists("g:formatprg_css") | let g:formatprg_css = "css-beautify" | endif
if !exists("g:formatprg_args_expr_css") && !exists("g:formatprg_args_css")
let g:formatprg_args_expr_css = '"-f - -s ".&shiftwidth'
endif
"if !exists("g:formatprg_css") | let g:formatprg_css = "css-beautify" | endif
"if !exists("g:formatprg_args_expr_css") && !exists("g:formatprg_args_css")
"let g:formatprg_args_expr_css = '"-f - -s ".&shiftwidth'
"endif
if !exists("g:formatprg_scss") | let g:formatprg_scss = "sass-convert" | endif
if !exists("g:formatprg_args_expr_scss") && !exists("g:formatprg_args_scss")
let g:formatprg_args_expr_scss = '"-F scss -T scss --indent " . (&expandtab ? &shiftwidth : "t")'
endif
"if !exists("g:formatprg_scss") | let g:formatprg_scss = "sass-convert" | endif
"if !exists("g:formatprg_args_expr_scss") && !exists("g:formatprg_args_scss")
"let g:formatprg_args_expr_scss = '"-F scss -T scss --indent " . (&expandtab ? &shiftwidth : "t")'
"endif
if !exists("g:formatprg_html") | let g:formatprg_html = "html-beautify" | endif
if !exists("g:formatprg_args_expr_html") && !exists("g:formatprg_args_html")
let g:formatprg_args_expr_html = '"-f - -s ".&shiftwidth'
endif
"if !exists("g:formatprg_html") | let g:formatprg_html = "html-beautify" | endif
"if !exists("g:formatprg_args_expr_html") && !exists("g:formatprg_args_html")
"let g:formatprg_args_expr_html = '"-f - -s ".&shiftwidth'
"endif
if !exists("g:formatprg_javascript") | let g:formatprg_javascript = "js-beautify" | endif
if !exists("g:formatprg_args_expr_javascript") && !exists("g:formatprg_args_javascript")
let g:formatprg_args_expr_javascript = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")'
endif
"if !exists("g:formatprg_javascript") | let g:formatprg_javascript = "js-beautify" | endif
"if !exists("g:formatprg_args_expr_javascript") && !exists("g:formatprg_args_javascript")
"let g:formatprg_args_expr_javascript = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")'
"endif
if !exists("g:formatprg_typescript") | let g:formatprg_typescript = "tsfmt" | endif
if !exists("g:formatprg_args_expr_typescript") && !exists("g:formatprg_args_typescript")
let g:formatprg_args_expr_typescript = '"--stdin %"'
endif
"if !exists("g:formatprg_typescript") | let g:formatprg_typescript = "tsfmt" | endif
"if !exists("g:formatprg_args_expr_typescript") && !exists("g:formatprg_args_typescript")
"let g:formatprg_args_expr_typescript = '"--stdin %"'
"endif
if !exists("g:formatprg_json") | let g:formatprg_json = "js-beautify" | endif
if !exists("g:formatprg_args_expr_json") && !exists("g:formatprg_args_json")
let g:formatprg_args_expr_json = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t")'
endif
"if !exists("g:formatprg_json") | let g:formatprg_json = "js-beautify" | endif
"if !exists("g:formatprg_args_expr_json") && !exists("g:formatprg_args_json")
"let g:formatprg_args_expr_json = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t")'
"endif
if !exists("g:formatprg_ruby") | let g:formatprg_ruby = "rbeautify" | endif
if !exists("g:formatprg_args_expr_ruby") && !exists("g:formatprg_args_ruby")
let g:formatprg_args_expr_ruby = '(&expandtab ? "-s -c ".&shiftwidth : "-t")'
endif
"if !exists("g:formatprg_ruby") | let g:formatprg_ruby = "rbeautify" | endif
"if !exists("g:formatprg_args_expr_ruby") && !exists("g:formatprg_args_ruby")
"let g:formatprg_args_expr_ruby = '(&expandtab ? "-s -c ".&shiftwidth : "-t")'
"endif