Add config file detection for clang-format.

Also
 - add CurrentFormatter command.
 - more verbosity.
 - update readme.
This commit is contained in:
Chiel ten Brinke 2015-12-04 09:59:23 +01:00
parent 29e088848a
commit d704fd1647
3 changed files with 43 additions and 9 deletions

View File

@ -67,7 +67,9 @@ au BufWrite * :Autoformat
For each filetype, vim-autoformat has a list of applicable formatters. For each filetype, vim-autoformat has a list of applicable formatters.
If you have multiple formatters installed that are supported for some filetype, vim-autoformat just uses the first that occurs in this list of applicable formatters. If you have multiple formatters installed that are supported for some filetype, vim-autoformat just uses the first that occurs in this list of applicable formatters.
You can either set this list manually in your vimrc (see section *How can I change the behaviour of formatters, or add one myself?*, or change the formatter with the highest priority by the commands `:NextFormatter` and `:PreviousFormatter`. You can either set this list manually in your vimrc (see section *How can I change the behaviour of formatters, or add one myself?*,
or change the formatter with the highest priority by the commands `:NextFormatter` and `:PreviousFormatter`.
To print the currently selected formatter use `:CurrentFormatter`.
If you have a composite filetype with dots (like `django.python` or `php.wordpress`), vim-autoformat first tries to detect and use formatters for the exact original filetype, and then tries the same for all supertypes occuring from left to right in the original filetype separated by dots (`.`). If you have a composite filetype with dots (like `django.python` or `php.wordpress`), vim-autoformat first tries to detect and use formatters for the exact original filetype, and then tries the same for all supertypes occuring from left to right in the original filetype separated by dots (`.`).
Default formatprograms Default formatprograms
@ -75,9 +77,11 @@ Default formatprograms
Here is a list of formatprograms that are supported by default, and thus will be detected and used by vim when they are installed properly. Here is a list of formatprograms that are supported by default, and thus will be detected and used by vim when they are installed properly.
* `clang-format` for __C__, __C++__, __Objective-C__ (supports formatting ranges). * `clang-format` for __C__, __C++__, __Objective-C__ (supports formatting ranges).
clang-format is a product of LLVM source builds. Clang-format is a product of LLVM source builds.
If you `brew install llvm`, clang-format can be found in /usr/local/Cellar/llvm/bin/. If you `brew install llvm`, clang-format can be found in /usr/local/Cellar/llvm/bin/.
To load style configuration from a .clang-format file, add to your .vimrc: `let g:formatdef_clangformat_objc = '"clang-format -style=file"'`. Vim-autoformat checks whether there exists a `.clang-format` or a `_clang-format` file up in
the current directory's ancestry. Based on that it eithers uses that file or tries to match
vim options as much as possible.
Details: http://clang.llvm.org/docs/ClangFormat.html. Details: http://clang.llvm.org/docs/ClangFormat.html.
* `astyle` for __C#__, __C++__, __C__ and __Java__. * `astyle` for __C#__, __C++__, __C__ and __Java__.
@ -203,7 +207,9 @@ would then only format the selected part.
If you're struggling with getting a formatter to work, it may help to set vim-autoformat in If you're struggling with getting a formatter to work, it may help to set vim-autoformat in
verbose-mode. Vim-autoformat will then output errors on formatters that failed. verbose-mode. Vim-autoformat will then output errors on formatters that failed.
```vim ```vim
let g:autoformat_verbosemode = 1 let g:autoformat_verbosemode=1
" OR:
let verbose=1
``` ```
To read all messages in a vim session type `:messages`. To read all messages in a vim session type `:messages`.

View File

@ -58,6 +58,9 @@ 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(...) range function! s:TryAllFormatters(...) range
" Detect verbosity
let verbose = &verbose || exists("g:autoformat_verbosemode")
" 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)
" No formatters defined, so autoindent code " No formatters defined, so autoindent code
@ -101,13 +104,22 @@ function! s:TryAllFormatters(...) range
let success = s:TryFormatterPython3() let success = s:TryFormatterPython3()
endif endif
if success if success
if verbose
echo "Definition in '".formatdef_var."' was successful."
endif
return 1 return 1
else else
if verbose
echo "Definition in '".formatdef_var."' was unsuccessful."
endif
let s:index = (s:index + 1) % len(b:formatters) let s:index = (s:index + 1) % len(b:formatters)
endif endif
if s:index == b:current_formatter_index if s:index == b:current_formatter_index
" Tried all formatters, none worked so autoindent code " Tried all formatters, none worked so autoindent code
if verbose
echo "No format definitions were successful. Trying to autoindent code."
endif
exe "normal gg=G" exe "normal gg=G"
return 0 return 0
endif endif
@ -142,8 +154,8 @@ 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]')
print('Formatter {} has errors: {}. Skipping.'.format(formattername, stderrdata)) print('Formatter {} has errors: {} Skipping.'.format(formattername, stderrdata))
print('Failing config: {} '.format(repr(formatprg), stderrdata)) print('Failing config: {}'.format(repr(formatprg), stderrdata))
vim.command('return 0') vim.command('return 0')
else: else:
# It is not certain what kind of line endings are being used by the format program. # It is not certain what kind of line endings are being used by the format program.
@ -190,8 +202,8 @@ 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]')
print('Formatter {} has errors: {}. Skipping.'.format(formattername, stderrdata)) print('Formatter {} has errors: {} Skipping.'.format(formattername, stderrdata))
print('Failing config: {} '.format(repr(formatprg), stderrdata)) print('Failing config: {}'.format(repr(formatprg), stderrdata))
vim.command('return 0') vim.command('return 0')
else: else:
# It is not certain what kind of line endings are being used by the format program. # It is not certain what kind of line endings are being used by the format program.
@ -244,6 +256,15 @@ function! s:PreviousFormatter()
echom 'Selected formatter: '.b:formatters[b:current_formatter_index] echom 'Selected formatter: '.b:formatters[b:current_formatter_index]
endfunction endfunction
function! s:CurrentFormatter()
call s:find_formatters()
if !exists('b:current_formatter_index')
let b:current_formatter_index = 0
endif
echom 'Selected formatter: '.b:formatters[b:current_formatter_index]
endfunction
" Create commands for iterating through formatter list " Create commands for iterating through formatter list
command! NextFormatter call s:NextFormatter() command! NextFormatter call s:NextFormatter()
command! PreviousFormatter call s:PreviousFormatter() command! PreviousFormatter call s:PreviousFormatter()
command! CurrentFormatter call s:CurrentFormatter()

View File

@ -25,9 +25,16 @@ endif
" Generic C, C++, Objective-C " Generic C, C++, Objective-C
if !exists('g:formatdef_clangformat') if !exists('g:formatdef_clangformat')
let g:formatdef_clangformat = "'clang-format -lines='.a:firstline.':'.a:lastline.' --assume-filename='.bufname('%').' -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, '.(&textwidth ? 'ColumnLimit: '.&textwidth.', ' : '').(&expandtab ? 'UseTab: Never, IndentWidth: '.shiftwidth() : 'UseTab: Always').'}\"'" let s:configfile_def = "'clang-format -lines='.a:firstline.':'.a:lastline.' --assume-filename='.bufname('%').' -style=file'"
let s:noconfigfile_def = "'clang-format -lines='.a:firstline.':'.a:lastline.' --assume-filename='.bufname('%').' -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, '.(&textwidth ? 'ColumnLimit: '.&textwidth.', ' : '').(&expandtab ? 'UseTab: Never, IndentWidth: '.shiftwidth() : 'UseTab: Always').'}\"'"
let g:formatdef_clangformat = "g:ClangConfigFileExists() ? (" . s:configfile_def . ") : (" . s:noconfigfile_def . ")"
endif endif
function! g:ClangConfigFileExists()
return len(findfile(".clang-format", ".;")) || len(findfile("_clang-format", ".;"))
endfunction
" C " C
if !exists('g:formatdef_astyle_c') if !exists('g:formatdef_astyle_c')