From 61248e1453dc6373160154e1f6855ffc510a7dfc Mon Sep 17 00:00:00 2001 From: Pat Brisbin Date: Sat, 22 Apr 2023 09:05:50 -0400 Subject: [PATCH] Add fourmolu fixer (#4501) * Add fourmolu fixer Fourmolu is aversion of Ormolu that supports configuration. This fixer was modeled after the Ormolu one, but using the "stack executable" approach of the Brittany and Stylish Haskell fixers. * Sort supported-tools.md --- autoload/ale/fix/registry.vim | 5 ++++ autoload/ale/fixers/fourmolu.vim | 20 +++++++++++++ doc/ale-haskell.txt | 20 +++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + .../fixers/test_fourmolu_fixer_callback.vader | 29 +++++++++++++++++++ 7 files changed, 77 insertions(+) create mode 100644 autoload/ale/fixers/fourmolu.vim create mode 100644 test/fixers/test_fourmolu_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index f7fff6a7..66cdc707 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -561,6 +561,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['haskell'], \ 'description': 'A formatter for Haskell source code.', \ }, +\ 'fourmolu': { +\ 'function': 'ale#fixers#fourmolu#Fix', +\ 'suggested_filetypes': ['haskell'], +\ 'description': 'A formatter for Haskell source code.', +\ }, \ 'jsonnetfmt': { \ 'function': 'ale#fixers#jsonnetfmt#Fix', \ 'suggested_filetypes': ['jsonnet'], diff --git a/autoload/ale/fixers/fourmolu.vim b/autoload/ale/fixers/fourmolu.vim new file mode 100644 index 00000000..399ec0f4 --- /dev/null +++ b/autoload/ale/fixers/fourmolu.vim @@ -0,0 +1,20 @@ +call ale#Set('haskell_fourmolu_executable', 'fourmolu') +call ale#Set('haskell_fourmolu_options', '') + +function! ale#fixers#fourmolu#GetExecutable(buffer) abort + let l:executable = ale#Var(a:buffer, 'haskell_fourmolu_executable') + + return ale#handlers#haskell_stack#EscapeExecutable(l:executable, 'fourmolu') +endfunction + +function! ale#fixers#fourmolu#Fix(buffer) abort + let l:executable = ale#fixers#fourmolu#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'haskell_fourmolu_options') + + return { + \ 'command': l:executable + \ . (empty(l:options) ? '' : ' ' . l:options) + \ . ' --stdin-input-file ' + \ . ale#Escape(@%), + \} +endfunction diff --git a/doc/ale-haskell.txt b/doc/ale-haskell.txt index bd5a5edf..bcd15724 100644 --- a/doc/ale-haskell.txt +++ b/doc/ale-haskell.txt @@ -224,5 +224,25 @@ g:ale_haskell_ormolu_options *g:ale_haskell_ormolu_options* executable. +=============================================================================== +fourmolu *ale-haskell-fourmolu* + +g:ale_haskell_fourmolu_executable *g:ale_haskell_fourmolu_executable* + *b:ale_haskell_fourmolu_executable* + Type: |String| + Default: `'fourmolu'` + + This variable can be changed to use a different executable for fourmolu. + + +g:ale_haskell_fourmolu_options *g:ale_haskell_fourmolu_options* + *b:ale_haskell_fourmolu_options* + Type: |String| + Default: `''` + + This variable can be used to pass extra options to the underlying fourmolu + executable. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index d56d0a76..6369224a 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -236,6 +236,7 @@ Notes: * `cabal-ghc` * `cspell` * `floskell` + * `fourmolu` * `ghc` * `ghc-mod` * `hdevtools` diff --git a/doc/ale.txt b/doc/ale.txt index ffdde04c..76f00c43 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3019,6 +3019,7 @@ documented in additional help files. stylish-haskell.......................|ale-haskell-stylish-haskell| hie...................................|ale-haskell-hie| ormolu................................|ale-haskell-ormolu| + fourmolu..............................|ale-haskell-fourmolu| hcl.....................................|ale-hcl-options| packer-fmt............................|ale-hcl-packer-fmt| terraform-fmt.........................|ale-hcl-terraform-fmt| diff --git a/supported-tools.md b/supported-tools.md index 0767cc35..a69e8a76 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -245,6 +245,7 @@ formatting. * [cabal-ghc](https://www.haskell.org/cabal/) * [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell) * [floskell](https://github.com/ennocramer/floskell) + * [fourmolu](https://github.com/fourmolu/fourmolu) * [ghc](https://www.haskell.org/ghc/) * [ghc-mod](https://github.com/DanielG/ghc-mod) * [hdevtools](https://hackage.haskell.org/package/hdevtools) diff --git a/test/fixers/test_fourmolu_fixer_callback.vader b/test/fixers/test_fourmolu_fixer_callback.vader new file mode 100644 index 00000000..728c02ed --- /dev/null +++ b/test/fixers/test_fourmolu_fixer_callback.vader @@ -0,0 +1,29 @@ +Before: + Save g:ale_haskell_fourmolu_executable + Save g:ale_haskell_fourmolu_options + +After: + Restore + +Execute(The fourmolu callback should return the correct default values): + AssertEqual + \ { + \ 'command': ale#Escape('fourmolu') + \ . ' --stdin-input-file ' + \ . ale#Escape(@%) + \ }, + \ ale#fixers#fourmolu#Fix(bufnr('')) + +Execute(The fourmolu executable and options should be configurable): + let g:ale_haskell_fourmolu_executable = '/path/to/fourmolu' + let g:ale_haskell_fourmolu_options = '-h' + + AssertEqual + \ { + \ 'command': ale#Escape('/path/to/fourmolu') + \ . ' -h' + \ . ' --stdin-input-file ' + \ . ale#Escape(@%) + \ }, + \ ale#fixers#fourmolu#Fix(bufnr('')) +