From d3b2d5c3376e7a698e080e08daaf9235499ff19b Mon Sep 17 00:00:00 2001 From: Alex Piechowski Date: Sat, 18 Dec 2021 20:44:34 -0600 Subject: [PATCH] Add auto-fixer for crystal --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/crystal.vim | 14 ++++++++ .../test_crystal_format_fixer_callback.vader | 33 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 autoload/ale/fixers/crystal.vim create mode 100644 test/fixers/test_crystal_format_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index c6764daf27..9eefcc69ca 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -421,6 +421,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['hcl', 'terraform'], \ 'description': 'Fix tf and hcl files with terraform fmt.', \ }, +\ 'crystal': { +\ 'function': 'ale#fixers#crystal#Fix', +\ 'suggested_filetypes': ['cr'], +\ 'description': 'Fix cr (crystal).', +\ }, \ 'ktlint': { \ 'function': 'ale#fixers#ktlint#Fix', \ 'suggested_filetypes': ['kt', 'kotlin'], diff --git a/autoload/ale/fixers/crystal.vim b/autoload/ale/fixers/crystal.vim new file mode 100644 index 0000000000..96868650c6 --- /dev/null +++ b/autoload/ale/fixers/crystal.vim @@ -0,0 +1,14 @@ +call ale#Set('crystal_format_executable', 'crystal') +call ale#Set('crystal_format_options', '') + +function! ale#fixers#crystal#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'crystal_format_executable') + let l:options = ale#Var(a:buffer, 'crystal_format_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' tool format' + \ . (empty(l:options) ? '' : ' ' . l:options) + \ . ' -' + \} +endfunction diff --git a/test/fixers/test_crystal_format_fixer_callback.vader b/test/fixers/test_crystal_format_fixer_callback.vader new file mode 100644 index 0000000000..8da6124d63 --- /dev/null +++ b/test/fixers/test_crystal_format_fixer_callback.vader @@ -0,0 +1,33 @@ +Before: + Save g:ale_crystal_format_executable + Save g:ale_crystal_format_options + + " Use an invalid global executable, so we don't match it. + let g:ale_crystal_format_executable = 'xxxinvalid' + let g:ale_crystal_format_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The crystal format callback should return the correct default values): + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') . ' format -', + \ }, + \ ale#fixers#crystal#Fix(bufnr('')) + +Execute(The crystal format callback should include custom options): + let g:ale_crystal_format_options = "-list=true" + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' ' . g:ale_crystal_format_options + \ . ' -', + \ }, + \ ale#fixers#crystal#Fix(bufnr(''))