From 854d6063331c686ded959ecb17830fb8c34f2db3 Mon Sep 17 00:00:00 2001 From: Albert Peschar Date: Tue, 26 Jul 2022 11:55:22 +0300 Subject: [PATCH] Add support for Dune (#4263) * Add support for dune * Add test * Undo reformatting of Markdown file * Refer to ale-ocaml-dune from ale.txt --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/dune.vim | 16 ++++++++++ doc/ale-ocaml.txt | 20 ++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + test/fixers/test_dune_fixer_callback.vader | 36 ++++++++++++++++++++++ 7 files changed, 80 insertions(+) create mode 100644 autoload/ale/fixers/dune.vim create mode 100644 test/fixers/test_dune_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index e9b289c6..85fef81d 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -78,6 +78,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['dhall'], \ 'description': 'Standard code formatter for the Dhall language and removing dead code', \ }, +\ 'dune': { +\ 'function': 'ale#fixers#dune#Fix', +\ 'suggested_filetypes': ['dune'], +\ 'description': 'Fix dune files with dune format', +\ }, \ 'fecs': { \ 'function': 'ale#fixers#fecs#Fix', \ 'suggested_filetypes': ['javascript', 'css', 'html'], diff --git a/autoload/ale/fixers/dune.vim b/autoload/ale/fixers/dune.vim new file mode 100644 index 00000000..6ef7ec9f --- /dev/null +++ b/autoload/ale/fixers/dune.vim @@ -0,0 +1,16 @@ +" Author: Albert Peschar +" Description: Fix files with dune format. + +call ale#Set('ocaml_dune_executable', 'dune') +call ale#Set('ocaml_dune_options', '') + +function! ale#fixers#dune#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'ocaml_dune_executable') + let l:options = ale#Var(a:buffer, 'ocaml_dune_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' format' + \ . (empty(l:options) ? '' : ' ' . l:options), + \} +endfunction diff --git a/doc/ale-ocaml.txt b/doc/ale-ocaml.txt index afbc2386..a361a7b4 100644 --- a/doc/ale-ocaml.txt +++ b/doc/ale-ocaml.txt @@ -2,6 +2,26 @@ ALE OCaml Integration *ale-ocaml-options* +=============================================================================== +dune *ale-ocaml-dune* + + Dune is a build system for OCaml projects. The `dune format` command is + supported for automatically formatting `dune` and `dune-project` files. + +g:ale_ocaml_dune_executable *g:ale_ocaml_dune_executable* + *b:ale_ocaml_dune_executable* + Type: |String| + Default: `'dune'` + + This variable can be set to pass the path to dune. + +g:ale_ocaml_dune_options *g:ale_ocaml_dune_options* + *b:ale_ocaml_dune_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the dune fixer. + =============================================================================== merlin *ale-ocaml-merlin* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index d66b9a02..a0a7fbd5 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -386,6 +386,7 @@ Notes: * `clangd` * `uncrustify` * OCaml + * `dune` * `merlin` (see |ale-ocaml-merlin|) * `ocamlformat` * `ocamllsp` diff --git a/doc/ale.txt b/doc/ale.txt index cabca300..828d5e2d 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3041,6 +3041,7 @@ documented in additional help files. clangd................................|ale-objcpp-clangd| uncrustify............................|ale-objcpp-uncrustify| ocaml...................................|ale-ocaml-options| + dune..................................|ale-ocaml-dune| merlin................................|ale-ocaml-merlin| ocamllsp..............................|ale-ocaml-ocamllsp| ols...................................|ale-ocaml-ols| diff --git a/supported-tools.md b/supported-tools.md index 235f211e..7a71c14c 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -395,6 +395,7 @@ formatting. * [clangd](https://clang.llvm.org/extra/clangd.html) * [uncrustify](https://github.com/uncrustify/uncrustify) * OCaml + * [dune](https://dune.build/) * [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-ocaml-merlin` for configuration instructions * [ocamlformat](https://github.com/ocaml-ppx/ocamlformat) * [ocamllsp](https://github.com/ocaml/ocaml-lsp) diff --git a/test/fixers/test_dune_fixer_callback.vader b/test/fixers/test_dune_fixer_callback.vader new file mode 100644 index 00000000..7fc0733c --- /dev/null +++ b/test/fixers/test_dune_fixer_callback.vader @@ -0,0 +1,36 @@ +Before: + Save g:ale_ocaml_dune_executable + Save g:ale_ocaml_dune_options + + " Use an invalid global executable, so we don't match it. + let g:ale_ocaml_dune_executable = 'xxxinvalid' + let g:ale_ocaml_dune_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The dune callback should return the correct default values): + call ale#test#SetFilename('../test-files/ocaml/testfile.re') + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' format', + \ }, + \ ale#fixers#dune#Fix(bufnr('')) + +Execute(The dune callback should include custom dune options): + let g:ale_ocaml_dune_options = "--random-option" + call ale#test#SetFilename('../test-files/ocaml/testfile.re') + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' format' + \ . ' ' . g:ale_ocaml_dune_options, + \ }, + \ ale#fixers#dune#Fix(bufnr(''))