Merge pull request #1510 from fenuks/issue-1471

Support passing fixers names to ALEFix as arguments
This commit is contained in:
w0rp 2018-04-20 21:38:11 +01:00 committed by GitHub
commit 63bbb38c0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 10 deletions

View file

@ -356,8 +356,10 @@ function! s:RunFixer(options) abort
call ale#fix#ApplyFixes(l:buffer, l:input) call ale#fix#ApplyFixes(l:buffer, l:input)
endfunction endfunction
function! s:GetCallbacks(buffer) abort function! s:GetCallbacks(buffer, linters) abort
if type(get(b:, 'ale_fixers')) is type([]) if len(a:linters)
let l:callback_list = a:linters
elseif type(get(b:, 'ale_fixers')) is type([])
" Lists can be used for buffer-local variables only " Lists can be used for buffer-local variables only
let l:callback_list = b:ale_fixers let l:callback_list = b:ale_fixers
else else
@ -422,13 +424,13 @@ endfunction
" Accepts an optional argument for what to do when fixing. " Accepts an optional argument for what to do when fixing.
" "
" Returns 0 if no fixes can be applied, and 1 if fixing can be done. " Returns 0 if no fixes can be applied, and 1 if fixing can be done.
function! ale#fix#Fix(buffer, fixing_flag) abort function! ale#fix#Fix(buffer, fixing_flag, ...) abort
if a:fixing_flag isnot# '' && a:fixing_flag isnot# 'save_file' if a:fixing_flag isnot# '' && a:fixing_flag isnot# 'save_file'
throw "fixing_flag must be either '' or 'save_file'" throw "fixing_flag must be either '' or 'save_file'"
endif endif
try try
let l:callback_list = s:GetCallbacks(a:buffer) let l:callback_list = s:GetCallbacks(a:buffer, a:000)
catch /E700\|BADNAME/ catch /E700\|BADNAME/
let l:function_name = join(split(split(v:exception, ':')[3])) let l:function_name = join(split(split(v:exception, ':')[3]))
let l:echo_message = printf( let l:echo_message = printf(

View file

@ -277,6 +277,14 @@ function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort
return 0 return 0
endfunction endfunction
function! s:IsGenericFixer(suggested_filetypes) abort
if empty(a:suggested_filetypes)
return 1
endif
return 0
endfunction
function! s:FormatEntry(key, entry) abort function! s:FormatEntry(key, entry) abort
let l:aliases_str = '' let l:aliases_str = ''
@ -296,6 +304,27 @@ function! s:FormatEntry(key, entry) abort
\) \)
endfunction endfunction
" Get list of applicable fixers for filetype, including generic fixers
function! ale#fix#registry#GetApplicableFixers(filetype) abort
let l:type_list = split(a:filetype, '\.')
let l:fixer_name_list = []
for l:key in sort(keys(s:entries))
let l:suggested_filetypes = s:entries[l:key].suggested_filetypes
if s:IsGenericFixer(l:suggested_filetypes) || s:ShouldSuggestForType(l:suggested_filetypes, l:type_list)
call add(l:fixer_name_list, l:key)
endif
endfor
return l:fixer_name_list
endfunction
" Function that returns autocomplete candidates for ALEFix command
function! ale#fix#registry#CompleteFixers(ArgLead, CmdLine, CursorPos) abort
return ale#fix#registry#GetApplicableFixers(&filetype)
endfunction
" Suggest functions to use from the registry. " Suggest functions to use from the registry.
function! ale#fix#registry#Suggest(filetype) abort function! ale#fix#registry#Suggest(filetype) abort
let l:type_list = split(a:filetype, '\.') let l:type_list = split(a:filetype, '\.')
@ -315,7 +344,7 @@ function! ale#fix#registry#Suggest(filetype) abort
let l:generic_fixer_list = [] let l:generic_fixer_list = []
for l:key in sort(keys(s:entries)) for l:key in sort(keys(s:entries))
if empty(s:entries[l:key].suggested_filetypes) if s:IsGenericFixer(s:entries[l:key].suggested_filetypes)
call add( call add(
\ l:generic_fixer_list, \ l:generic_fixer_list,
\ s:FormatEntry(l:key, s:entries[l:key]), \ s:FormatEntry(l:key, s:entries[l:key]),

View file

@ -473,10 +473,12 @@ ftplugin files for different filetypes.
=============================================================================== ===============================================================================
4. Fixing Problems *ale-fix* 4. Fixing Problems *ale-fix*
ALE can fix problems with files with the |ALEFix| command. When |ALEFix| is ALE can fix problems with files with the |ALEFix| command. |ALEFix|
run, the variable |g:ale_fixers| will be read for getting a |List| of commands accepts names of fixers to be applied as arguments. Alternatively,
for filetypes, split on `.`, and the functions named in |g:ale_fixers| will be when no arguments are provided, the variable |g:ale_fixers| will be
executed for fixing the errors. read for getting a |List| of commands for filetypes, split on `.`, and
the functions named in |g:ale_fixers| will be executed for fixing the
errors.
The |ALEFixSuggest| command can be used to suggest tools that be used to The |ALEFixSuggest| command can be used to suggest tools that be used to
fix problems for the current buffer. fix problems for the current buffer.

View file

@ -263,7 +263,7 @@ command! -bar ALEInfoToClipboard :call ale#debugging#InfoToClipboard()
command! -bar -nargs=1 ALEInfoToFile :call ale#debugging#InfoToFile(<f-args>) command! -bar -nargs=1 ALEInfoToFile :call ale#debugging#InfoToFile(<f-args>)
" Fix problems in files. " Fix problems in files.
command! -bar ALEFix :call ale#fix#Fix(bufnr(''), '') command! -bar -nargs=* -complete=customlist,ale#fix#registry#CompleteFixers ALEFix :call ale#fix#Fix(bufnr(''), '', <f-args>)
" Suggest registered functions to use for fixing problems. " Suggest registered functions to use for fixing problems.
command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype) command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype)

View file

@ -272,6 +272,15 @@ Expect(An extra line should be added):
c c
d d
Execute(ALEFix should use fixers passed in commandline when provided):
let g:ale_fixers.testft = ['RemoveLastLine']
ALEFix AddCarets AddDollars
Expect(Only fixers passed via command line should be run):
$^a
$^b
$^c
Execute(ALEFix should allow temporary files to be read): Execute(ALEFix should allow temporary files to be read):
if has('win32') if has('win32')
" Just skip this test on Windows, we can't run it. " Just skip this test on Windows, we can't run it.

View file

@ -0,0 +1,23 @@
Execute (List of available fixers is empty):
call ale#fix#registry#Clear()
Then (List of applicable fixers for python file is empty):
AssertEqual [], ale#fix#registry#GetApplicableFixers('python')
Execute (Add ruby fixer):
call ale#fix#registry#Add('ruby_fixer', 'fixer_fun', ['ruby'], 'ruby fixer')
Then (List of applicable fixers for python file is still empty):
AssertEqual [], ale#fix#registry#GetApplicableFixers('python')
Execute (Add generic fixer):
call ale#fix#registry#Add('generic_fixer', 'fixer_fun', [], 'generic fixer')
Then (Generic fixer should be returned as applicable for python file):
AssertEqual ['generic_fixer'], ale#fix#registry#GetApplicableFixers('python')
Execute (Add python fixer):
call ale#fix#registry#Add('python_fixer', 'fixer_func', ['python'], 'python fixer')
Then (List of fixers should contain both generic and python fixers):
AssertEqual ['generic_fixer', 'python_fixer'], ale#fix#registry#GetApplicableFixers('python')