Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Haskell ormolu fixer #4654

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions autoload/ale/fixers/ormolu.vim
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
call ale#Set('haskell_ormolu_executable', 'ormolu')
call ale#Set('haskell_ormolu_options', '')

function! ale#fixers#ormolu#Fix(buffer) abort
function! ale#fixers#ormolu#GetExecutable(buffer) abort
let l:executable = ale#Var(a:buffer, 'haskell_ormolu_executable')

return ale#handlers#haskell_stack#EscapeExecutable(l:executable, 'ormolu')
endfunction

function! ale#fixers#ormolu#Fix(buffer) abort
let l:executable = ale#fixers#ormolu#GetExecutable(a:buffer)
let l:options = ale#Var(a:buffer, 'haskell_ormolu_options')

return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options),
\ 'command': l:executable
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' --stdin-input-file '
\ . ale#Escape(@%),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@% may not be the current file at the time the linter runs, as this code could be executed from a different buffer. Instead get the filename using the a:buffer variable. ALE has a shorthand for this where you can use %s in the command string. See :help ale-command-format-strings.

Have a look at changelogs and make sure the --stdin-input-file argument is available in old enough ormolu versions. If it's not available in older versions, you can make this fixer function still compatible with older versions with the ale#semver functions and a version check. You can grep the codebase for other fixers that run version checks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being slow to respond but thanks for the feedback!

@% may not be the current file at the time the linter runs, as this code could be executed from a different buffer. Instead get the filename using the a:buffer variable. ALE has a shorthand for this where you can use %s in the command string. See :help ale-command-format-strings.

👍

Have a look at changelogs and make sure the --stdin-input-file argument is available in old enough ormolu versions. If it's not available in older versions, you can make this fixer function still compatible with older versions with the ale#semver functions and a version check. You can grep the codebase for other fixers that run version checks.

ormolu is a Haskell program which uses Haskell PVP which AFAIU means the ale#semver functions won't work. I couldn't find usage of PVP version checks in the codebase, but I guess one option would be to create a similar ale#pvp module. Does that sound reasonable or do you know of a better way to solve this?

\}
endfunction
12 changes: 8 additions & 4 deletions test/fixers/test_ormolu_fixer_callback.vader
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ Execute(The ormolu callback should return the correct default values):
AssertEqual
\ {
\ 'command': ale#Escape('ormolu')
\ . ' --stdin-input-file '
\ . ale#Escape(@%)
\ },
\ ale#fixers#ormolu#Fix(bufnr(''))

Execute(The ormolu executable and options should be configurable):
let g:ale_nix_nixpkgsfmt_executable = '/path/to/ormolu'
let g:ale_nix_nixpkgsfmt_options = '-h'
let g:ale_haskell_ormolu_executable = '/path/to/ormolu'
let g:ale_haskell_ormolu_options = '-h'

AssertEqual
\ {
\ 'command': ale#Escape('/path/to/ormolu')
\ . ' -h',
\ . ' -h'
\ . ' --stdin-input-file '
\ . ale#Escape(@%)
\ },
\ ale#fixers#nixpkgsfmt#Fix(bufnr(''))
\ ale#fixers#ormolu#Fix(bufnr(''))