From 716b22d524b80941eee6538e988a963f923901f3 Mon Sep 17 00:00:00 2001 From: Simon Bugert Date: Sun, 5 Nov 2017 22:24:41 +0100 Subject: [PATCH] Add shfmt fixer for sh files (#1083) * Add shfmt fixer for sh files * Add tests for shfmt fixer --- README.md | 4 ++-- autoload/ale/fix/registry.vim | 5 +++++ autoload/ale/fixers/shfmt.vim | 17 +++++++++++++++ doc/ale-sh.txt | 11 ++++++++++ doc/ale.txt | 5 +++-- test/fixers/test_shfmt_fixer_callback.vader | 24 +++++++++++++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 autoload/ale/fixers/shfmt.vim create mode 100644 test/fixers/test_shfmt_fixer_callback.vader diff --git a/README.md b/README.md index f10aa5d..3ff0597 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ formatting. | Ansible | [ansible-lint](https://github.com/willthames/ansible-lint) | | AsciiDoc | [proselint](http://proselint.com/), [write-good](https://github.com/btford/write-good)| | Awk | [gawk](https://www.gnu.org/software/gawk/)| -| Bash | shell [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set), [shellcheck](https://www.shellcheck.net/) | -| Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/) | +| Bash | shell [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | +| Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | | C | [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [gcc](https://gcc.gnu.org/), [clang](http://clang.llvm.org/), [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [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)| | CUDA | [nvcc](http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 37bbee9..24166da 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -132,6 +132,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['reason'], \ 'description': 'Fix ReasonML files with refmt.', \ }, +\ 'shfmt': { +\ 'function': 'ale#fixers#shfmt#Fix', +\ 'suggested_filetypes': ['sh'], +\ 'description': 'Fix sh files with shfmt.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/shfmt.vim b/autoload/ale/fixers/shfmt.vim new file mode 100644 index 0000000..882cf3a --- /dev/null +++ b/autoload/ale/fixers/shfmt.vim @@ -0,0 +1,17 @@ +scriptencoding utf-8 +" Author: Simon Bugert +" Description: Fix sh files with shfmt. + +call ale#Set('sh_shfmt_executable', 'shfmt') +call ale#Set('sh_shfmt_options', '') + +function! ale#fixers#shfmt#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'sh_shfmt_executable') + let l:options = ale#Var(a:buffer, 'sh_shfmt_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . (empty(l:options) ? '' : ' ' . l:options) + \} + +endfunction diff --git a/doc/ale-sh.txt b/doc/ale-sh.txt index 6fbc9fe..941dc59 100644 --- a/doc/ale-sh.txt +++ b/doc/ale-sh.txt @@ -57,5 +57,16 @@ g:ale_sh_shellcheck_exclusions *g:ale_sh_shellcheck_exclusions* \ let b:ale_sh_shellcheck_exclusions = 'SC2034,SC2154,SC2164' < +=============================================================================== +shfmt *ale-sh-shfmt* + +g:ale_sh_shfmt_options *g:ale_sh_shfmt_options* + *b:ale_sh_shfmt_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the shfmt fixer. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index bedf0cf..f00e1ac 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -173,6 +173,7 @@ CONTENTS *ale-contents* sh....................................|ale-sh-options| shell...............................|ale-sh-shell| shellcheck..........................|ale-sh-shellcheck| + shfmt...............................|ale-sh-shfmt| sml...................................|ale-sml-options| smlnj...............................|ale-sml-smlnj| solidity..............................|ale-solidity-options| @@ -256,8 +257,8 @@ Notes: * Ansible: `ansible-lint` * AsciiDoc: `proselint`, `write-good` * Awk: `gawk` -* Bash: `shell` (-n flag), `shellcheck` -* Bourne Shell: `shell` (-n flag), `shellcheck` +* Bash: `shell` (-n flag), `shellcheck`, `shfmt` +* Bourne Shell: `shell` (-n flag), `shellcheck`, `shfmt` * C: `cppcheck`, `cpplint`!!, `gcc`, `clang`, `clangtidy`!!, `clang-format` * C++ (filetype cpp): `clang`, `clangcheck`!!, `clangtidy`!!, `cppcheck`, `cpplint`!!, `gcc`, `clang-format` * CUDA: `nvcc`!! diff --git a/test/fixers/test_shfmt_fixer_callback.vader b/test/fixers/test_shfmt_fixer_callback.vader new file mode 100644 index 0000000..dcdf66b --- /dev/null +++ b/test/fixers/test_shfmt_fixer_callback.vader @@ -0,0 +1,24 @@ +Before: + Save g:ale_sh_shfmt_executable + Save g:ale_sh_shfmt_options + + " Use an invalid global executable, so we don't match it. + let g:ale_sh_shfmt_executable = 'xxxinvalid' + let g:ale_sh_shfmt_options = '' + +Execute(The shfmt callback should return the correct default values): + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid'), + \ }, + \ ale#fixers#shfmt#Fix(bufnr('')) + +Execute(The shfmt callback should include custom shfmt options): + let g:ale_sh_shfmt_options = '--some-option' + + AssertEqual + \ { + \ 'command': ale#Escape(g:ale_sh_shfmt_executable) + \ . ' --some-option', + \ }, + \ ale#fixers#shfmt#Fix(bufnr(''))