summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-05-18 23:50:06 +0100
committerw0rp <devw0rp@gmail.com>2017-05-20 19:02:36 +0100
commit1f4d1800e0040d7d36d1c19e15c5f0e570122273 (patch)
tree145c0783768883c2814bcb9984d3e07299e66e7a
parentea1627f5ce5620806644a525f5dc8523187fd69f (diff)
downloadale-1f4d1800e0040d7d36d1c19e15c5f0e570122273.zip
Allow function aliases to be registered for fixing problems, and add some more argument checking for fixing problems
-rw-r--r--autoload/ale/fix.vim38
-rw-r--r--autoload/ale/fix/registry.vim54
-rw-r--r--test/test_ale_fix.vader16
3 files changed, 106 insertions, 2 deletions
diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim
index 288919aa..89778a16 100644
--- a/autoload/ale/fix.vim
+++ b/autoload/ale/fix.vim
@@ -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
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
new file mode 100644
index 00000000..b0f87ddc
--- /dev/null
+++ b/autoload/ale/fix/registry.vim
@@ -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
diff --git a/test/test_ale_fix.vader b/test/test_ale_fix.vader
index 8ec7896d..a872f38e 100644
--- a/test/test_ale_fix.vader
+++ b/test/test_ale_fix.vader
@@ -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