Skip to content

Commit

Permalink
Optional file type argument to Autoformat command
Browse files Browse the repository at this point in the history
  • Loading branch information
shanesmith committed Jun 29, 2014
1 parent 492cad1 commit 5a17715
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions plugin/autoformat.vim
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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>)

0 comments on commit 5a17715

Please sign in to comment.