Allow clangformat to use a local style file (#3587)

* Allow clangformat to use a local style file.

* Add tests.

* Fix Vint issue.

* Improve explanation of feature in documentation.

* Fix failing test.

The test was checking the wrong directory.
This commit is contained in:
Mukund Mauji 2021-02-14 09:03:04 -05:00 committed by GitHub
parent 8cb9f5ef51
commit b30c5c9b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 1 deletions

View File

@ -5,6 +5,8 @@ scriptencoding utf-8
call ale#Set('c_clangformat_executable', 'clang-format')
call ale#Set('c_clangformat_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('c_clangformat_options', '')
call ale#Set('c_clangformat_style_option', '')
call ale#Set('c_clangformat_use_local_file', 0)
function! ale#fixers#clangformat#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'c_clangformat', [
@ -16,6 +18,24 @@ function! ale#fixers#clangformat#Fix(buffer) abort
let l:executable = ale#Escape(ale#fixers#clangformat#GetExecutable(a:buffer))
let l:filename = ale#Escape(bufname(a:buffer))
let l:options = ale#Var(a:buffer, 'c_clangformat_options')
let l:style_option = ale#Var(a:buffer, 'c_clangformat_style_option')
let l:use_local_file = ale#Var(a:buffer, 'c_clangformat_use_local_file')
if l:style_option isnot# ''
let l:style_option = '-style=' . "'" . l:style_option . "'"
endif
if l:use_local_file
let l:config = ale#path#FindNearestFile(a:buffer, '.clang-format')
if !empty(l:config)
let l:style_option = '-style=file'
endif
endif
if l:style_option isnot# ''
let l:options .= ' ' . l:style_option
endif
let l:command = l:executable . ' --assume-filename=' . l:filename

View File

@ -202,7 +202,45 @@ g:ale_c_clangformat_options *g:ale_c_clangformat_options*
Type: |String|
Default: `''`
This variable can be change to modify flags given to clang-format.
This variable can be changed to modify flags given to clang-format.
g:ale_c_clangformat_style_option *g:ale_c_clangformat_style_option*
*b:ale_c_clangformat_style_option*
Type: |String|
Default: `''`
This variable can be changed to modify only the style flag given to
clang-format. The contents of the variable are passed directly to the -style
flag of clang-format.
Example: >
{
\ BasedOnStyle: Microsoft,
\ ColumnLimit: 80,
\ AllowShortBlocksOnASingleLine: Always,
\ AllowShortFunctionsOnASingleLine: Inline,
\ }
<
If you set this variable, ensure you don't modify -style in
|g:ale_c_clangformat_options|, as this will cause clang-format to error.
g:ale_c_clangformat_use_local_file *g:ale_c_clangformat_use_local_file*
*b:ale_c_clangformat_use_local_file*
Type: |Number|
Default: `0`
This variable can be changed to modify whether to use a local .clang-format
file. If the file is found, the flag '-style=file' is passed to clang-format
and any options configured via |g:ale_c_clangformat_style_option| are not
passed.
If this option is enabled but no .clang-format file is found, default back to
|g:ale_c_clangformat_style_option|, if it set.
If you set this variable, ensure you don't modify -style in
|g:ale_c_clangformat_options|, as this will cause clang-format to error.
===============================================================================

View File

@ -1,5 +1,7 @@
Before:
Save g:ale_c_clangformat_executable
Save g:c_clangformat_style_option
Save g:c_clangformat_use_local_file
" Use an invalid global executable, so we don't match it.
let g:ale_c_clangformat_executable = 'xxxinvalid'
@ -35,3 +37,30 @@ Execute(The clangformat callback should include any additional options):
\ . ' --some-option',
\ },
\ ale#fixers#clangformat#Fix(bufnr(''))
Execute(The clangformat callback should include style options as well):
call ale#test#SetFilename('c_paths/dummy.c')
let g:ale_c_clangformat_options = '--some-option'
let g:ale_c_clangformat_style_option = '{BasedOnStyle: Microsoft, ColumnLimit:80,}'
AssertEqual
\ {
\ 'command': ale#Escape(g:ale_c_clangformat_executable)
\ . ' --assume-filename=' . ale#Escape(bufname(bufnr('')))
\ . ' --some-option' . " -style='{BasedOnStyle: Microsoft, ColumnLimit:80,}'",
\ },
\ ale#fixers#clangformat#Fix(bufnr(''))
Execute(The clangformat callback should use local file instead of style options):
call ale#test#SetFilename('clangformat_paths/with_clangformat/dummy.c')
let g:ale_c_clangformat_options = '--some-option'
let g:ale_c_clangformat_style_option = '{BasedOnStyle: Microsoft, ColumnLimit:80,}'
let g:ale_c_clangformat_use_local_file = 1
AssertEqual
\ {
\ 'command': ale#Escape(g:ale_c_clangformat_executable)
\ . ' --assume-filename=' . ale#Escape(bufname(bufnr('')))
\ . ' --some-option' . ' -style=file',
\ },
\ ale#fixers#clangformat#Fix(bufnr(''))