diff options
-rw-r--r-- | autoload/ale/fixers/clangformat.vim | 20 | ||||
-rw-r--r-- | doc/ale-c.txt | 40 | ||||
-rw-r--r-- | test/command_callback/clangformat_paths/with_clangformat/.clang-format | 0 | ||||
-rw-r--r-- | test/fixers/test_clangformat_fixer_callback.vader | 29 |
4 files changed, 88 insertions, 1 deletions
diff --git a/autoload/ale/fixers/clangformat.vim b/autoload/ale/fixers/clangformat.vim index ea5743a5..ecff080a 100644 --- a/autoload/ale/fixers/clangformat.vim +++ b/autoload/ale/fixers/clangformat.vim @@ -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 diff --git a/doc/ale-c.txt b/doc/ale-c.txt index b0d94b8e..3b9fbc44 100644 --- a/doc/ale-c.txt +++ b/doc/ale-c.txt @@ -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. =============================================================================== diff --git a/test/command_callback/clangformat_paths/with_clangformat/.clang-format b/test/command_callback/clangformat_paths/with_clangformat/.clang-format new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/command_callback/clangformat_paths/with_clangformat/.clang-format diff --git a/test/fixers/test_clangformat_fixer_callback.vader b/test/fixers/test_clangformat_fixer_callback.vader index ae7db4cf..b9b74ee8 100644 --- a/test/fixers/test_clangformat_fixer_callback.vader +++ b/test/fixers/test_clangformat_fixer_callback.vader @@ -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('')) |