shfmt: Use Vim's indent config as default indent width

This commit is contained in:
rhysd 2018-09-18 15:41:33 +09:00
parent ddb3e6d57a
commit 532686102e
2 changed files with 52 additions and 2 deletions

View File

@ -5,12 +5,27 @@ scriptencoding utf-8
call ale#Set('sh_shfmt_executable', 'shfmt') call ale#Set('sh_shfmt_executable', 'shfmt')
call ale#Set('sh_shfmt_options', '') call ale#Set('sh_shfmt_options', '')
function! s:DefaultOption(buffer) abort
if getbufvar(a:buffer, '&expandtab') == 0
" Tab is used by default
return ''
endif
let l:tabsize = getbufvar(a:buffer, '&shiftwidth')
if l:tabsize == 0
let l:tabsize = getbufvar(a:buffer, '&tabstop')
endif
return ' -i ' . l:tabsize
endfunction
function! ale#fixers#shfmt#Fix(buffer) abort function! ale#fixers#shfmt#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'sh_shfmt_executable') let l:executable = ale#Var(a:buffer, 'sh_shfmt_executable')
let l:options = ale#Var(a:buffer, 'sh_shfmt_options') let l:options = ale#Var(a:buffer, 'sh_shfmt_options')
return { return {
\ 'command': ale#Escape(l:executable) \ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options) \ . (empty(l:options) ? s:DefaultOption(a:buffer) : ' ' . l:options)
\} \}
endfunction endfunction

View File

@ -1,17 +1,52 @@
Before: Before:
Save g:ale_sh_shfmt_executable Save g:ale_sh_shfmt_executable
Save g:ale_sh_shfmt_options Save g:ale_sh_shfmt_options
Save &l:expandtab
Save &l:shiftwidth
Save &l:tabstop
After: After:
Restore Restore
Execute(The shfmt callback should return the correct default values): Execute(The shfmt callback should return 'shfmt' as default command):
setlocal noexpandtab
Assert
\ ale#fixers#shfmt#Fix(bufnr('')).command =~# '^' . ale#Escape('shfmt'),
\ "Default command name is expected to be 'shfmt'"
Execute(The shfmt callback should return the command with no option as default when noexpandtab is set):
let g:ale_sh_shfmt_executable = 'shfmt'
let g:ale_sh_shfmt_options = ''
setlocal noexpandtab
AssertEqual AssertEqual
\ { \ {
\ 'command': ale#Escape('shfmt'), \ 'command': ale#Escape('shfmt'),
\ }, \ },
\ ale#fixers#shfmt#Fix(bufnr('')) \ ale#fixers#shfmt#Fix(bufnr(''))
Execute(The shfmt callback should return the command specifying indent width by looking shiftwidth as default):
let g:ale_sh_shfmt_executable = 'shfmt'
let g:ale_sh_shfmt_options = ''
setlocal expandtab
setlocal shiftwidth=4
AssertEqual
\ {
\ 'command': ale#Escape('shfmt') . ' -i 4',
\ },
\ ale#fixers#shfmt#Fix(bufnr(''))
Execute(The shfmt callback should return the command specifying indent width by looking tabstop when shiftwidth is 0 as default):
let g:ale_sh_shfmt_executable = 'shfmt'
let g:ale_sh_shfmt_options = ''
setlocal expandtab
setlocal shiftwidth=0
setlocal tabstop=8
AssertEqual
\ {
\ 'command': ale#Escape('shfmt') . ' -i 8',
\ },
\ ale#fixers#shfmt#Fix(bufnr(''))
Execute(The shfmt executable and options should be configurable): Execute(The shfmt executable and options should be configurable):
let g:ale_sh_shfmt_executable = 'foobar' let g:ale_sh_shfmt_executable = 'foobar'
let g:ale_sh_shfmt_options = '--some-option' let g:ale_sh_shfmt_options = '--some-option'