From f3da8f45c15fdd335593be060c0c6aaea751e81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sat, 9 Sep 2017 22:30:20 +0200 Subject: [PATCH] Add elm-format as a new fixer (#916) * Add elm-format as a new fixer --- README.md | 2 +- autoload/ale/fix/registry.vim | 5 ++ autoload/ale/fixers/format.vim | 23 ++++++ doc/ale-elm.txt | 32 ++++++++ doc/ale.txt | 2 +- .../node_modules/.bin/elm-format | 0 test/elm-test-files/src/subdir/testfile.elm | 0 .../test_elm_format_fixer_callback.vader | 75 +++++++++++++++++++ 8 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 autoload/ale/fixers/format.vim create mode 100644 doc/ale-elm.txt create mode 100644 test/elm-test-files/node_modules/.bin/elm-format create mode 100644 test/elm-test-files/src/subdir/testfile.elm create mode 100644 test/fixers/test_elm_format_fixer_callback.vader diff --git a/README.md b/README.md index e808d82..4919564 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ formatting. | Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) | | Dockerfile | [hadolint](https://github.com/lukasmartinelli/hadolint) | | Elixir | [credo](https://github.com/rrrene/credo), [dogma](https://github.com/lpil/dogma) !! | -| Elm | [elm-make](https://github.com/elm-lang/elm-make) | +| Elm | [elm-make](https://github.com/elm-lang/elm-make), [elm-format](https://github.com/avh4/elm-format) | | Erb | [erb](https://github.com/jeremyevans/erubi), [erubis](https://github.com/kwatch/erubis) | | Erlang | [erlc](http://erlang.org/doc/man/erlc.html), [SyntaxErl](https://github.com/ten0s/syntaxerl) | | Fortran | [gcc](https://gcc.gnu.org/) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 5b1030d..e87b02f 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -27,6 +27,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['javascript', 'typescript'], \ 'description': 'Apply eslint --fix to a file.', \ }, +\ 'format': { +\ 'function': 'ale#fixers#format#Fix', +\ 'suggested_filetypes': ['elm'], +\ 'description': 'Apply elm-format to a file.', +\ }, \ 'isort': { \ 'function': 'ale#fixers#isort#Fix', \ 'suggested_filetypes': ['python'], diff --git a/autoload/ale/fixers/format.vim b/autoload/ale/fixers/format.vim new file mode 100644 index 0000000..be130f0 --- /dev/null +++ b/autoload/ale/fixers/format.vim @@ -0,0 +1,23 @@ +" Author: soywod +" Description: Integration of elm-format with ALE. + +call ale#Set('elm_format_executable', 'elm-format') +call ale#Set('elm_format_use_global', 0) +call ale#Set('elm_format_options', '--yes') + +function! ale#fixers#format#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'elm_format', [ + \ 'node_modules/.bin/elm-format', + \]) +endfunction + +function! ale#fixers#format#Fix(buffer) abort + let l:options = ale#Var(a:buffer, 'elm_format_options') + + return { + \ 'command': ale#Escape(ale#fixers#format#GetExecutable(a:buffer)) + \ . ' %t' + \ . (empty(l:options) ? '' : ' ' . l:options), + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/doc/ale-elm.txt b/doc/ale-elm.txt new file mode 100644 index 0000000..9331575 --- /dev/null +++ b/doc/ale-elm.txt @@ -0,0 +1,32 @@ +=============================================================================== +ALE Elm Integration *ale-elm-options* + + +=============================================================================== +elm-format *ale-elm-format* + +g:ale_elm_format_executable *g:ale_elm_format_executable* + *b:ale_elm_format_executable* + Type: |String| + Default: `'elm-format'` + + See |ale-integrations-local-executables| + + +g:ale_elm_format_use_global *g:ale_elm_format_use_global* + *b:ale_elm_format_use_global* + Type: |Number| + Default: `0` + + See |ale-integrations-local-executables| + + +g:ale_elm_format_options *g:ale_elm_format_options* + *b:ale_elm_format_options* + Type: |String| + Default: `'--yes'` + + This variable can be set to pass additional options to elm-format. + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 319c109..ff02f7a 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -217,7 +217,7 @@ Notes: * Dart: `dartanalyzer` * Dockerfile: `hadolint` * Elixir: `credo`, `dogma`!! -* Elm: `elm-make` +* Elm: `elm-make, elm-format` * Erb: `erb`, `erubis` * Erlang: `erlc`, `SyntaxErl` * Fortran: `gcc` diff --git a/test/elm-test-files/node_modules/.bin/elm-format b/test/elm-test-files/node_modules/.bin/elm-format new file mode 100644 index 0000000..e69de29 diff --git a/test/elm-test-files/src/subdir/testfile.elm b/test/elm-test-files/src/subdir/testfile.elm new file mode 100644 index 0000000..e69de29 diff --git a/test/fixers/test_elm_format_fixer_callback.vader b/test/fixers/test_elm_format_fixer_callback.vader new file mode 100644 index 0000000..b8b9ed0 --- /dev/null +++ b/test/fixers/test_elm_format_fixer_callback.vader @@ -0,0 +1,75 @@ +Before: + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + unlet! b:ale_elm_format_executable + unlet! b:ale_elm_format_use_global + unlet! b:ale_elm_format_options + + call ale#test#RestoreDirectory() + +Execute(The elm-format command should have default params): + call ale#test#SetFilename('../elm-test-files/src/subdir/testfile.elm') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape(simplify(g:dir . '/../elm-test-files/node_modules/.bin/elm-format')) + \ . ' %t --yes', + \ }, + \ ale#fixers#format#Fix(bufnr('')) + +Execute(The elm-format command should manage use_global = 1 param): + call ale#test#SetFilename('../elm-test-files/src/subdir/testfile.elm') + let b:ale_elm_format_use_global = 1 + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape('elm-format') + \ . ' %t --yes', + \ }, + \ ale#fixers#format#Fix(bufnr('')) + +Execute(The elm-format command should manage executable param): + call ale#test#SetFilename('../elm-test-files/src/subdir/testfile.elm') + let b:ale_elm_format_use_global = 1 + let b:ale_elm_format_executable = 'elmformat' + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape('elmformat') + \ . ' %t --yes', + \ }, + \ ale#fixers#format#Fix(bufnr('')) + +Execute(The elm-format command should manage empty options): + call ale#test#SetFilename('../elm-test-files/src/subdir/testfile.elm') + let b:ale_elm_format_options = '' + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape(simplify(g:dir . '/../elm-test-files/node_modules/.bin/elm-format')) + \ . ' %t', + \ }, + \ ale#fixers#format#Fix(bufnr('')) + +Execute(The elm-format command should manage custom options): + call ale#test#SetFilename('../elm-test-files/src/subdir/testfile.elm') + let b:ale_elm_format_options = '--param1 --param2' + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape(simplify(g:dir . '/../elm-test-files/node_modules/.bin/elm-format')) + \ . ' %t --param1 --param2', + \ }, + \ ale#fixers#format#Fix(bufnr('')) +