Skip to content

Commit 6c337ad

Browse files
authored
fix: stylua should have cwd and --stdin-filepath (dense-analysis#4873)
1 parent 83ec182 commit 6c337ad

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

autoload/ale/fixers/stylua.vim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
call ale#Set('lua_stylua_executable', 'stylua')
55
call ale#Set('lua_stylua_options', '')
66

7+
function! ale#fixers#stylua#GetCwd(buffer) abort
8+
for l:possible_configfile in ['stylua.toml', '.stylua.toml']
9+
let l:config = ale#path#FindNearestFile(a:buffer, l:possible_configfile)
10+
11+
return !empty(l:config) ? fnamemodify(l:config, ':h') : '%s:h'
12+
endfor
13+
14+
return ''
15+
endfunction
16+
717
function! ale#fixers#stylua#Fix(buffer) abort
818
let l:executable = ale#Var(a:buffer, 'lua_stylua_executable')
919
let l:options = ale#Var(a:buffer, 'lua_stylua_options')
1020

1121
return {
12-
\ 'command': ale#Escape(l:executable) . ale#Pad(l:options) . ' -',
22+
\ 'cwd': ale#fixers#stylua#GetCwd(a:buffer),
23+
\ 'command': ale#Escape(l:executable) . ale#Pad(l:options) . ' --stdin-filepath %s -',
1324
\}
1425
endfunction

test/fixers/test_stylua_fixer_callback.vader

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ After:
55
call ale#assert#TearDownFixerTest()
66

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

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

1414
AssertFixer
1515
\ {
16+
\ 'cwd': '%s:h',
1617
\ 'command': ale#Escape('xxxinvalid')
1718
\ . ' ' . g:ale_lua_stylua_options
18-
\ . ' -',
19+
\ . ' --stdin-filepath %s -',
1920
\ }

0 commit comments

Comments
 (0)