From 4bea50b82f53a351d218cc3af120b460b9d1639f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Renstr=C3=B6m?= Date: Thu, 24 Aug 2017 23:49:43 +0200 Subject: [PATCH] Add clang-format fixer for C/C++ (#873) * Add clang-format fixer for C/C++ * Document clang-format options * Refer ale-cpp-clangformat to ale-c-clangformat --- README.md | 4 +-- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/clangformat.vim | 21 +++++++++++ doc/ale-c.txt | 19 ++++++++++ doc/ale-cpp.txt | 7 ++++ doc/ale.txt | 6 ++-- test/command_callback/c_paths/dummy.c | 0 .../test_clangformat_fixer_callback.vader | 36 +++++++++++++++++++ 8 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 autoload/ale/fixers/clangformat.vim create mode 100644 test/command_callback/c_paths/dummy.c create mode 100644 test/fixers/test_clangformat_fixer_callback.vader diff --git a/README.md b/README.md index 22866120..24024d3d 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ name. That seems to be the fairest way to arrange this table. | Awk | [gawk](https://www.gnu.org/software/gawk/)| | Bash | [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set), [shellcheck](https://www.shellcheck.net/) | | Bourne Shell | [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/) | -| C | [cppcheck](http://cppcheck.sourceforge.net), [gcc](https://gcc.gnu.org/), [clang](http://clang.llvm.org/)| -| C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html), [clangtidy](http://clang.llvm.org/extra/clang-tidy/), [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [gcc](https://gcc.gnu.org/)| +| C | [cppcheck](http://cppcheck.sourceforge.net), [gcc](https://gcc.gnu.org/), [clang](http://clang.llvm.org/), [clang-format](https://clang.llvm.org/docs/ClangFormat.html)| +| C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html), [clangtidy](http://clang.llvm.org/extra/clang-tidy/), [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [gcc](https://gcc.gnu.org/), [clang-format](https://clang.llvm.org/docs/ClangFormat.html)| | C# | [mcs](http://www.mono-project.com/docs/about-mono/languages/csharp/) | | Chef | [foodcritic](http://www.foodcritic.io/) | | CMake | [cmakelint](https://github.com/richq/cmake-lint) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 5c3b8d94..b77ac031 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -82,6 +82,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['php'], \ 'description': 'Fix PHP files with phpcbf.', \ }, +\ 'clang-format': { +\ 'function': 'ale#fixers#clangformat#Fix', +\ 'suggested_filetypes': ['c', 'cpp'], +\ 'description': 'Fix C/C++ files with clang-format.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/clangformat.vim b/autoload/ale/fixers/clangformat.vim new file mode 100644 index 00000000..b2ed7daa --- /dev/null +++ b/autoload/ale/fixers/clangformat.vim @@ -0,0 +1,21 @@ +" Author: Peter Renström +" Description: Fixing C/C++ files with clang-format. + +call ale#Set('c_clangformat_executable', 'clang-format') +call ale#Set('c_clangformat_use_global', 0) +call ale#Set('c_clangformat_options', '') + +function! ale#fixers#clangformat#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'c_clangformat', [ + \ 'clang-format', + \]) +endfunction + +function! ale#fixers#clangformat#Fix(buffer) abort + let l:options = ale#Var(a:buffer, 'c_clangformat_options') + + return { + \ 'command': ale#Escape(ale#fixers#clangformat#GetExecutable(a:buffer)) + \ . ' ' . l:options, + \} +endfunction diff --git a/doc/ale-c.txt b/doc/ale-c.txt index 2572d885..1e1322bc 100644 --- a/doc/ale-c.txt +++ b/doc/ale-c.txt @@ -59,5 +59,24 @@ g:ale_c_gcc_options *g:ale_c_gcc_options* This variable can be change to modify flags given to gcc. +=============================================================================== +clang-format *ale-c-clangformat* + +g:ale_c_clangformat_executable *g:ale_c_clangformat_executable* + *b:ale_c_clangformat_executable* + Type: |String| + Default: `'clang-format'` + + This variable can be changed to use a different executable for clang-format. + + +g:ale_c_clangformat_options *g:ale_c_clangformat_options* + *b:ale_c_clangformat_options* + Type: |String| + Default: `''` + + This variable can be change to modify flags given to clang-format. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-cpp.txt b/doc/ale-cpp.txt index 854e9b4e..2ece6843 100644 --- a/doc/ale-cpp.txt +++ b/doc/ale-cpp.txt @@ -189,5 +189,12 @@ g:ale_cpp_gcc_options *g:ale_cpp_gcc_options* This variable can be changed to modify flags given to gcc. +=============================================================================== +clang-format *ale-cpp-clangformat* + +See |ale-c-clangformat| for information about the available options. +Note that the C options are also used for C++. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 18ea367c..56c5946e 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -19,6 +19,7 @@ CONTENTS *ale-contents* clang...............................|ale-c-clang| cppcheck............................|ale-c-cppcheck| gcc.................................|ale-c-gcc| + clang-format........................|ale-c-clangformat| chef..................................|ale-chef-options| foodcritic..........................|ale-chef-foodcritic| cpp...................................|ale-cpp-options| @@ -28,6 +29,7 @@ CONTENTS *ale-contents* cppcheck............................|ale-cpp-cppcheck| cpplint.............................|ale-cpp-cpplint| gcc.................................|ale-cpp-gcc| + clang-format........................|ale-cpp-clangformat| css...................................|ale-css-options| stylelint...........................|ale-css-stylelint| cmake.................................|ale-cmake-options| @@ -176,8 +178,8 @@ The following languages and tools are supported. * Asciidoc: 'proselint' * Bash: 'shell' (-n flag), 'shellcheck' * Bourne Shell: 'shell' (-n flag), 'shellcheck' -* C: 'cppcheck', 'gcc', 'clang' -* C++ (filetype cpp): 'clang', 'clangtidy', 'cppcheck', 'cpplint', 'gcc' +* C: 'cppcheck', 'gcc', 'clang', 'clang-format' +* C++ (filetype cpp): 'clang', 'clangtidy', 'cppcheck', 'cpplint', 'gcc', 'clang-format' * C#: 'mcs' * Chef: 'foodcritic' * CMake: 'cmakelint' diff --git a/test/command_callback/c_paths/dummy.c b/test/command_callback/c_paths/dummy.c new file mode 100644 index 00000000..e69de29b diff --git a/test/fixers/test_clangformat_fixer_callback.vader b/test/fixers/test_clangformat_fixer_callback.vader new file mode 100644 index 00000000..a55576bf --- /dev/null +++ b/test/fixers/test_clangformat_fixer_callback.vader @@ -0,0 +1,36 @@ +Before: + Save g:ale_c_clangformat_executable + + " Use an invalid global executable, so we don't match it. + let g:ale_c_clangformat_executable = 'xxxinvalid' + + call ale#test#SetDirectory('/testplugin/test/fixers') + silent cd .. + silent cd command_callback + let g:dir = getcwd() + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The clang-format callback should return the correct default values): + call ale#test#SetFilename('c_paths/dummy.c') + + AssertEqual + \ { + \ 'command': ale#Escape(g:ale_c_clangformat_executable) + \ . ' ' + \ }, + \ ale#fixers#clangformat#Fix(bufnr('')) + +Execute(The clangformat callback should include any additional options): + call ale#test#SetFilename('c_paths/dummy.c') + let g:ale_c_clangformat_options = '--some-option' + + AssertEqual + \ { + \ 'command': ale#Escape(g:ale_c_clangformat_executable) + \ . ' --some-option', + \ }, + \ ale#fixers#clangformat#Fix(bufnr(''))