Optional file type argument to Autoformat command

This commit is contained in:
Shane Smith 2014-06-29 15:30:05 -04:00
parent 492cad1620
commit 5a17715cfc
2 changed files with 13 additions and 9 deletions

View File

@ -40,6 +40,8 @@ Remember that when no formatprogram exists for a certain filetype, vim-autoforma
This will fix at least the indentation of your code, according to vim's indentfile for that filetype.
When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat`.
You can provide the command with a file type such as `:Autoformat json`, otherwise the buffer's filetype will be used.
For convenience it is recommended that you assign a key for this, like so:
```vim

View File

@ -1,14 +1,16 @@
"Function for finding and setting the formatter with the given name
function! s:set_formatprg()
"Get formatprg config for current filetype
let s:formatprg_var = "g:formatprg_".&filetype
let s:formatprg_args_var = "g:formatprg_args_".&filetype
let s:formatprg_args_expr_var = "g:formatprg_args_expr_".&filetype
function! s:set_formatprg(...)
let type = a:0 ? a:1 : &filetype
"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
if !exists(s:formatprg_var)
"No formatprg defined
if exists("g:autoformat_verbosemode")
echoerr "No formatter defined for filetype '".&filetype."'."
echoerr "No formatter defined for filetype '".type."'."
endif
return 0
endif
@ -38,11 +40,11 @@ endfunction
noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq'
"Function for formatting the entire buffer
function! s:Autoformat()
function! s:Autoformat(...)
"Save window state
let winview=winsaveview()
if <SID>set_formatprg()
if call('<SID>set_formatprg', a:000)
"Autoformat code
exe "1,$!".&formatprg
else
@ -55,4 +57,4 @@ function! s:Autoformat()
endfunction
"Create a command for formatting the entire buffer
command! Autoformat call s:Autoformat()
command! -nargs=? -complete=filetype Autoformat call s:Autoformat(<f-args>)