Skip to content

Commit

Permalink
fix: stylua should have cwd and --stdin-filepath (#4873)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcai authored Jan 10, 2025
1 parent 83ec182 commit 6c337ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion autoload/ale/fixers/stylua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
call ale#Set('lua_stylua_executable', 'stylua')
call ale#Set('lua_stylua_options', '')

function! ale#fixers#stylua#GetCwd(buffer) abort
for l:possible_configfile in ['stylua.toml', '.stylua.toml']
let l:config = ale#path#FindNearestFile(a:buffer, l:possible_configfile)

return !empty(l:config) ? fnamemodify(l:config, ':h') : '%s:h'
endfor

return ''
endfunction

function! ale#fixers#stylua#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'lua_stylua_executable')
let l:options = ale#Var(a:buffer, 'lua_stylua_options')

return {
\ 'command': ale#Escape(l:executable) . ale#Pad(l:options) . ' -',
\ 'cwd': ale#fixers#stylua#GetCwd(a:buffer),
\ 'command': ale#Escape(l:executable) . ale#Pad(l:options) . ' --stdin-filepath %s -',
\}
endfunction
5 changes: 3 additions & 2 deletions test/fixers/test_stylua_fixer_callback.vader
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ After:
call ale#assert#TearDownFixerTest()

Execute(The default command should be correct):
AssertFixer {'command': ale#Escape('stylua') . ' -'}
AssertFixer {'cwd': '%s:h', 'command': ale#Escape('stylua') . ' --stdin-filepath %s -'}

Execute(The stylua callback should include custom stylua options):
let g:ale_lua_stylua_executable = 'xxxinvalid'
let g:ale_lua_stylua_options = '--search-parent-directories'

AssertFixer
\ {
\ 'cwd': '%s:h',
\ 'command': ale#Escape('xxxinvalid')
\ . ' ' . g:ale_lua_stylua_options
\ . ' -',
\ . ' --stdin-filepath %s -',
\ }

0 comments on commit 6c337ad

Please sign in to comment.