From 48fab99a0ab793e1b9607795c21659f12bd6947f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Gagn=C3=A9?= Date: Sun, 28 Feb 2021 08:31:44 -0500 Subject: [PATCH] Add support for `erlfmt` fixer (#3602) * Add support for `erlfmt` * Add missing entry to table of contents * Fix warnings * Add missing tools to supported tools --- autoload/ale/fixers/erlfmt.vim | 21 ++++++++++++++++ doc/ale-erlang.txt | 20 ++++++++++++++++ doc/ale-supported-languages-and-tools.txt | 2 ++ doc/ale.txt | 1 + supported-tools.md | 2 ++ test/fixers/test_erlfmt_fixer_callback.vader | 25 ++++++++++++++++++++ 6 files changed, 71 insertions(+) create mode 100644 autoload/ale/fixers/erlfmt.vim create mode 100644 test/fixers/test_erlfmt_fixer_callback.vader diff --git a/autoload/ale/fixers/erlfmt.vim b/autoload/ale/fixers/erlfmt.vim new file mode 100644 index 00000000..e44046f1 --- /dev/null +++ b/autoload/ale/fixers/erlfmt.vim @@ -0,0 +1,21 @@ +" Author: AntoineGagne - https://github.com/AntoineGagne +" Description: Integration of erlfmt with ALE. + +call ale#Set('erlang_erlfmt_executable', 'erlfmt') +call ale#Set('erlang_erlfmt_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('erlang_erlfmt_options', '') + +function! ale#fixers#erlfmt#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'erlang_erlfmt', ['erlfmt']) +endfunction + +function! ale#fixers#erlfmt#Fix(buffer) abort + let l:options = ale#Var(a:buffer, 'erlang_erlfmt_options') + let l:executable = ale#fixers#erlfmt#GetExecutable(a:buffer) + + let l:command = ale#Escape(l:executable) . (empty(l:options) ? '' : ' ' . l:options) . ' %s' + + return { + \ 'command': l:command + \} +endfunction diff --git a/doc/ale-erlang.txt b/doc/ale-erlang.txt index 93ac7915..ede179d1 100644 --- a/doc/ale-erlang.txt +++ b/doc/ale-erlang.txt @@ -71,6 +71,26 @@ g:ale_erlang_erlc_options *g:ale_erlang_erlc_options* or `-pa`. +------------------------------------------------------------------------------- +erlfmt *ale-erlang-erlfmt* + +g:ale_erlang_erlfmt_executable *g:ale_erlang_erlfmt_executable* + *b:ale_erlang_erlfmt_executable* + Type: |String| + Default: `'erlfmt'` + + This variable can be changed to specify the erlfmt executable. + + +g:ale_erlang_erlfmt_options *g:ale_erlang_erlfmt_options* + *b:ale_erlang_erlfmt_options* + Type: |String| + Default: `''` + + This variable controls additional parameters passed to `erlfmt`, such as + `--insert-pragma` or `--print-width`. + + ------------------------------------------------------------------------------- syntaxerl *ale-erlang-syntaxerl* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index cb90de4c..e6764144 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -150,8 +150,10 @@ Notes: * `ruumba` * Erlang * `SyntaxErl` + * `dialyzer` * `elvis`!! * `erlc` + * `erlfmt` * Fish * `fish` (-n flag) * `fish_indent` diff --git a/doc/ale.txt b/doc/ale.txt index 113e29c5..1f7dbe76 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2697,6 +2697,7 @@ documented in additional help files. dialyzer..............................|ale-erlang-dialyzer| elvis.................................|ale-erlang-elvis| erlc..................................|ale-erlang-erlc| + erlfmt................................|ale-erlang-erlfmt| syntaxerl.............................|ale-erlang-syntaxerl| eruby...................................|ale-eruby-options| ruumba................................|ale-eruby-ruumba| diff --git a/supported-tools.md b/supported-tools.md index e9c88e95..a522d748 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -159,8 +159,10 @@ formatting. * [ruumba](https://github.com/ericqweinstein/ruumba) * Erlang * [SyntaxErl](https://github.com/ten0s/syntaxerl) + * [dialyzer](http://erlang.org/doc/man/dialyzer.html) * [elvis](https://github.com/inaka/elvis) :floppy_disk: * [erlc](http://erlang.org/doc/man/erlc.html) + * [erlfmt](https://github.com/WhatsApp/erlfmt) * Fish * fish [-n flag](https://linux.die.net/man/1/fish) * [fish_indent](https://fishshell.com/docs/current/cmds/fish_indent.html) diff --git a/test/fixers/test_erlfmt_fixer_callback.vader b/test/fixers/test_erlfmt_fixer_callback.vader new file mode 100644 index 00000000..132cd6ee --- /dev/null +++ b/test/fixers/test_erlfmt_fixer_callback.vader @@ -0,0 +1,25 @@ +Before: + Save b:ale_elm_format_executable + Save b:ale_elm_format_options + + let b:ale_elm_format_executable = 'erlfmt' + let b:ale_elm_format_options = '' + +After: + Restore + +Execute(The erlfmt command should handle empty options): + AssertEqual + \ { + \ 'command': ale#Escape('erlfmt') . ' %s' + \ }, + \ ale#fixers#erlfmt#Fix(bufnr('')) + +Execute(The erlfmt command should handle custom options): + let b:ale_erlang_erlfmt_options = '--insert-pragma' + + AssertEqual + \ { + \ 'command': ale#Escape('erlfmt') . ' --insert-pragma %s' + \ }, + \ ale#fixers#erlfmt#Fix(bufnr(''))