Skip to content

Commit

Permalink
Add luafmt fixer (dense-analysis#3289)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathlyfrantic authored and ivorpeles committed Nov 22, 2020
1 parent 00efba2 commit 36ab42d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions autoload/ale/fix/registry.vim
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['html', 'htmldjango'],
\ 'description': 'Fix HTML files with html-beautify.',
\ },
\ 'luafmt': {
\ 'function': 'ale#fixers#luafmt#Fix',
\ 'suggested_filetypes': ['lua'],
\ 'description': 'Fix Lua files with luafmt.',
\ },
\ 'dhall': {
\ 'function': 'ale#fixers#dhall#Fix',
\ 'suggested_filetypes': ['dhall'],
Expand Down
13 changes: 13 additions & 0 deletions autoload/ale/fixers/luafmt.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
call ale#Set('lua_luafmt_executable', 'luafmt')
call ale#Set('lua_luafmt_options', '')

function! ale#fixers#luafmt#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'lua_luafmt_executable')
let l:options = ale#Var(a:buffer, 'lua_luafmt_options')

return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' --stdin',
\}
endfunction
16 changes: 16 additions & 0 deletions doc/ale-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@ g:ale_lua_luacheck_options *g:ale_lua_luacheck_options*
This variable can be set to pass additional options to luacheck.


===============================================================================
luafmt *ale-lua-luafmt*

g:ale_lua_luafmt_executable *g:ale_lua_luafmt_executable*
*b:ale_lua_luafmt_executable*
Type: |String|
Default: `'luafmt'`

This variable can be set to use a different executable for luafmt.

g:ale_lua_luafmt_options *g:ale_lua_luafmt_options*
*b:ale_lua_luafmt_options*
Type: |String|
Default: `''`

This variable can be set to pass additional options to the luafmt fixer.
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ Notes:
* Lua
* `luac`
* `luacheck`
* `luafmt`
* Mail
* `alex`!!
* `languagetool`!!
Expand Down
1 change: 1 addition & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,7 @@ documented in additional help files.
lua.....................................|ale-lua-options|
luac..................................|ale-lua-luac|
luacheck..............................|ale-lua-luacheck|
luafmt................................|ale-lua-luafmt|
markdown................................|ale-markdown-options|
markdownlint..........................|ale-markdown-markdownlint|
mdl...................................|ale-markdown-mdl|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ formatting.
* Lua
* [luac](https://www.lua.org/manual/5.1/luac.html)
* [luacheck](https://github.com/mpeterv/luacheck)
* [luafmt](https://github.com/trixnz/lua-fmt)
* Mail
* [alex](https://github.com/wooorm/alex) :floppy_disk:
* [languagetool](https://languagetool.org/) :floppy_disk:
Expand Down
35 changes: 35 additions & 0 deletions test/fixers/test_luafmt_fixer_callback.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Before:
Save g:ale_lua_luafmt_executable
Save g:ale_lua_luafmt_options

" Use an invalid global executable, so we don't match it.
let g:ale_lua_luafmt_executable = 'xxxinvalid'
let g:ale_lua_luafmt_options = ''

call ale#test#SetDirectory('/testplugin/test/fixers')

After:
Restore

call ale#test#RestoreDirectory()

Execute(The luafmt callback should return the correct default values):
call ale#test#SetFilename('../lua_files/testfile.lua')

AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid') . ' --stdin',
\ },
\ ale#fixers#luafmt#Fix(bufnr(''))

Execute(The luafmt callback should include custom luafmt options):
let g:ale_lua_luafmt_options = "--skip-children"
call ale#test#SetFilename('../lua_files/testfile.lua')

AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid')
\ . ' ' . g:ale_lua_luafmt_options
\ . ' --stdin',
\ },
\ ale#fixers#luafmt#Fix(bufnr(''))
Empty file added test/lua_files/testfile.lua
Empty file.

0 comments on commit 36ab42d

Please sign in to comment.