Allow function aliases to be registered for fixing problems, and add some more argument checking for fixing problems
This commit is contained in:
parent
ea1627f5ce
commit
1f4d1800e0
@ -191,7 +191,7 @@ function! s:RunFixer(options) abort
|
||||
let l:index = a:options.callback_index
|
||||
|
||||
while len(a:options.callback_list) > l:index
|
||||
let l:result = function(a:options.callback_list[l:index])(l:buffer, copy(l:input))
|
||||
let l:result = call(a:options.callback_list[l:index], [l:buffer, copy(l:input)])
|
||||
|
||||
if type(l:result) == type(0) && l:result == 0
|
||||
" When `0` is returned, skip this item.
|
||||
@ -223,7 +223,7 @@ function! s:RunFixer(options) abort
|
||||
call s:ApplyFixes(l:buffer, l:input)
|
||||
endfunction
|
||||
|
||||
function! ale#fix#Fix() abort
|
||||
function! s:GetCallbacks() abort
|
||||
let l:callback_list = []
|
||||
|
||||
for l:sub_type in split(&filetype, '\.')
|
||||
@ -238,6 +238,40 @@ function! ale#fix#Fix() abort
|
||||
|
||||
if empty(l:callback_list)
|
||||
echoerr 'No fixers have been defined for filetype: ' . &filetype
|
||||
return []
|
||||
endif
|
||||
|
||||
let l:problem_list = []
|
||||
let l:corrected_list = []
|
||||
|
||||
for l:item in l:callback_list
|
||||
if type(l:item) == type('')
|
||||
if exists('*' . l:item)
|
||||
call add(l:corrected_list, function(l:item))
|
||||
else
|
||||
let l:func = ale#fix#registry#GetFunc(l:item)
|
||||
|
||||
if !empty(l:func) && exists('*' . l:func)
|
||||
call add(l:corrected_list, function(l:func))
|
||||
else
|
||||
call add(l:problem_list, l:item)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
|
||||
if !empty(l:problem_list)
|
||||
echoerr 'Invalid fixers used: ' . string(l:problem_list)
|
||||
return []
|
||||
endif
|
||||
|
||||
return l:corrected_list
|
||||
endfunction
|
||||
|
||||
function! ale#fix#Fix() abort
|
||||
let l:callback_list = s:GetCallbacks()
|
||||
|
||||
if empty(l:callback_list)
|
||||
return
|
||||
endif
|
||||
|
||||
|
54
autoload/ale/fix/registry.vim
Normal file
54
autoload/ale/fix/registry.vim
Normal file
@ -0,0 +1,54 @@
|
||||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" Description: A registry of functions for fixing things.
|
||||
|
||||
let s:default_registry = {
|
||||
\ 'eslint': {
|
||||
\ 'function': 'ale#handlers#eslint#Fix',
|
||||
\ 'suggested_filetypes': ['javascript'],
|
||||
\ 'description': '',
|
||||
\ },
|
||||
\}
|
||||
|
||||
" Reset the function registry to the default entries.
|
||||
function! ale#fix#registry#ResetToDefaults() abort
|
||||
let s:entries = deepcopy(s:default_registry)
|
||||
endfunction
|
||||
|
||||
" Set up entries now.
|
||||
call ale#fix#registry#ResetToDefaults()
|
||||
|
||||
" Add a function for fixing problems to the registry.
|
||||
function! ale#fix#registry#Add(name, func, filetypes, desc) abort
|
||||
if type(a:name) != type('')
|
||||
throw '''name'' must be a String'
|
||||
endif
|
||||
|
||||
if type(a:func) != type('')
|
||||
throw '''func'' must be a String'
|
||||
endif
|
||||
|
||||
if type(a:filetypes) != type([])
|
||||
throw '''filetypes'' must be a List'
|
||||
endif
|
||||
|
||||
for l:type in a:filetypes
|
||||
if type(l:type) != type('')
|
||||
throw 'Each entry of ''filetypes'' must be a String'
|
||||
endif
|
||||
endfor
|
||||
|
||||
if type(a:desc) != type('')
|
||||
throw '''desc'' must be a String'
|
||||
endif
|
||||
|
||||
let s:entries[a:name] = {
|
||||
\ 'function': a:func,
|
||||
\ 'suggested_filetypes': a:filetypes,
|
||||
\ 'description': a:desc,
|
||||
\}
|
||||
endfunction
|
||||
|
||||
" Get a function from the registry by its short name.
|
||||
function! ale#fix#registry#GetFunc(name) abort
|
||||
return get(s:entries, a:name, {'function': ''}).function
|
||||
endfunction
|
@ -37,6 +37,7 @@ After:
|
||||
delfunction DoNothing
|
||||
delfunction CatLine
|
||||
delfunction ReplaceWithTempFile
|
||||
call ale#fix#registry#ResetToDefaults()
|
||||
|
||||
Given testft (A file with three lines):
|
||||
a
|
||||
@ -126,3 +127,18 @@ Expect(The first function should be used):
|
||||
^a
|
||||
^b
|
||||
^c
|
||||
|
||||
Execute(ALEFix should complain for missing functions):
|
||||
let g:ale_fixers.testft = ['XXX', 'YYY']
|
||||
AssertThrows ALEFix
|
||||
AssertEqual 'Vim(echoerr):Invalid fixers used: [''XXX'', ''YYY'']', g:vader_exception
|
||||
|
||||
Execute(ALEFix should use functions from the registry):
|
||||
call ale#fix#registry#Add('add_carets', 'AddCarets', [], 'Add some carets')
|
||||
let g:ale_fixers.testft = ['add_carets']
|
||||
ALEFix
|
||||
|
||||
Expect(The registry function should be used):
|
||||
^a
|
||||
^b
|
||||
^c
|
||||
|
Loading…
Reference in New Issue
Block a user