From 9d98e6db0c5ebc5e7166f7ad9cffb115e9b9fcc5 Mon Sep 17 00:00:00 2001 From: Govind KP Date: Sun, 1 Jul 2018 18:25:41 +0530 Subject: [PATCH] Added dartfmt fixer (#1683) * Added dartfmt to Fixers * Added dartfmt specific documentation --- README.md | 2 +- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/dartfmt.vim | 18 +++++++++ doc/ale-dart.txt | 33 +++++++++++++++ doc/ale.txt | 3 +- test/dart_files/testfile.dart | 0 test/fixers/test_dartfmt_fixer_callback.vader | 40 +++++++++++++++++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 autoload/ale/fixers/dartfmt.vim create mode 100644 test/dart_files/testfile.dart create mode 100644 test/fixers/test_dartfmt_fixer_callback.vader diff --git a/README.md b/README.md index 8b833b40..f0b40527 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ formatting. | Cython (pyrex filetype) | [cython](http://cython.org/) | | D | [dmd](https://dlang.org/dmd-linux.html) | | Dafny | [dafny](https://rise4fun.com/Dafny) !! | -| Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) !!, [language_server](https://github.com/natebosch/dart_language_server) | +| Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) !!, [language_server](https://github.com/natebosch/dart_language_server), [dartfmt](https://github.com/dart-lang/sdk/tree/master/utils/dartfmt) | | Dockerfile | [hadolint](https://github.com/hadolint/hadolint) | | Elixir | [credo](https://github.com/rrrene/credo), [dialyxir](https://github.com/jeremyjh/dialyxir), [dogma](https://github.com/lpil/dogma), [mix](https://hexdocs.pm/mix/Mix.html) !!| | Elm | [elm-format](https://github.com/avh4/elm-format), [elm-make](https://github.com/elm-lang/elm-make) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 60d7d7a8..539234c0 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -210,6 +210,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['qml'], \ 'description': 'Fix QML files with qmlfmt.', \ }, +\ 'dartfmt': { +\ 'function': 'ale#fixers#dartfmt#Fix', +\ 'suggested_filetypes': ['dart'], +\ 'description': 'Fix Dart files with dartfmt.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/dartfmt.vim b/autoload/ale/fixers/dartfmt.vim new file mode 100644 index 00000000..0687d6d1 --- /dev/null +++ b/autoload/ale/fixers/dartfmt.vim @@ -0,0 +1,18 @@ +" Author: reisub0 +" Description: Integration of dartfmt with ALE. + +call ale#Set('dart_dartfmt_executable', 'dartfmt') +call ale#Set('dart_dartfmt_options', '') + +function! ale#fixers#dartfmt#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'dart_dartfmt_executable') + let l:options = ale#Var(a:buffer, 'dart_dartfmt_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' -w' + \ . (empty(l:options) ? '' : ' ' . l:options) + \ . ' %t', + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/doc/ale-dart.txt b/doc/ale-dart.txt index c6faa5c2..a6d88dd8 100644 --- a/doc/ale-dart.txt +++ b/doc/ale-dart.txt @@ -35,4 +35,37 @@ g:ale_dart_dartanalyzer_executable *g:ale_dart_dartanalyzer_executable* =============================================================================== +dartfmt *ale-dart-dartfmt* + +Installation +------------------------------------------------------------------------------- + +Installing Dart should probably ensure that `dartfmt` is in your `$PATH`. + +In case it is not, try to set the executable option to its absolute path. : > + " Set the executable path for dartfmt to the absolute path to it. + let g:ale_dart_dartfmt_executable = '/usr/lib/dart/bin/dartfmt' + > + +Options +------------------------------------------------------------------------------- + +g:ale_dart_dartfmt_executable *g:ale_dart_dartfmt_executable* + *b:ale_dart_dartfmt_executable* + Type: |String| + Default: `''` + + This variable can be set to specify an absolute path to the + dartfmt executable (or to specify an alternate executable). + + +g:ale_dart_dartfmt_options *g:ale_dart_dartfmt_options* + *b:ale_dart_dartfmt_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the dartfmt fixer. + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index cba164f9..f8b3141c 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -59,6 +59,7 @@ CONTENTS *ale-contents* nvcc................................|ale-cuda-nvcc| dart..................................|ale-dart-options| dartanalyzer........................|ale-dart-dartanalyzer| + dartfmt.............................|ale-dart-dartfmt| dockerfile............................|ale-dockerfile-options| hadolint............................|ale-dockerfile-hadolint| elixir................................|ale-elixir-options| @@ -342,7 +343,7 @@ Notes: * Cython (pyrex filetype): `cython` * D: `dmd` * Dafny: `dafny`!! -* Dart: `dartanalyzer`!!, `language_server` +* Dart: `dartanalyzer`!!, `language_server`, dartfmt!! * Dockerfile: `hadolint` * Elixir: `credo`, `dialyxir`, `dogma`, `mix`!! * Elm: `elm-format, elm-make` diff --git a/test/dart_files/testfile.dart b/test/dart_files/testfile.dart new file mode 100644 index 00000000..e69de29b diff --git a/test/fixers/test_dartfmt_fixer_callback.vader b/test/fixers/test_dartfmt_fixer_callback.vader new file mode 100644 index 00000000..79c40eff --- /dev/null +++ b/test/fixers/test_dartfmt_fixer_callback.vader @@ -0,0 +1,40 @@ +Before: + Save g:ale_dart_dartfmt_executable + Save g:ale_dart_dartfmt_options + + " Use an invalid global executable, so we don't match it. + let g:ale_dart_dartfmt_executable = 'xxxinvalid' + let g:ale_dart_dartfmt_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The dartfmt callback should return the correct default values): + call ale#test#SetFilename('../dart_files/testfile.dart') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape('xxxinvalid') + \ . ' -w' + \ . ' %t', + \ }, + \ ale#fixers#dartfmt#Fix(bufnr('')) + +Execute(The dartfmt callback should include custom dartfmt options): + let g:ale_dart_dartfmt_options = "-l 80" + call ale#test#SetFilename('../dart_files/testfile.dart') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape('xxxinvalid') + \ . ' -w' + \ . ' ' . g:ale_dart_dartfmt_options + \ . ' %t', + \ }, + \ ale#fixers#dartfmt#Fix(bufnr(''))