Allow ALEFix functions to be defined with only the buffer argument
This commit is contained in:
parent
e4d886d4a7
commit
f30652a98f
@ -232,7 +232,10 @@ function! s:RunFixer(options) abort
|
|||||||
let l:index = a:options.callback_index
|
let l:index = a:options.callback_index
|
||||||
|
|
||||||
while len(a:options.callback_list) > l:index
|
while len(a:options.callback_list) > l:index
|
||||||
let l:result = call(a:options.callback_list[l:index], [l:buffer, copy(l:input)])
|
let l:Function = a:options.callback_list[l:index]
|
||||||
|
let l:result = ale#util#FunctionArgCount(l:Function) == 1
|
||||||
|
\ ? call(l:Function, [l:buffer])
|
||||||
|
\ : call(l:Function, [l:buffer, copy(l:input)])
|
||||||
|
|
||||||
if type(l:result) == type(0) && l:result == 0
|
if type(l:result) == type(0) && l:result == 0
|
||||||
" When `0` is returned, skip this item.
|
" When `0` is returned, skip this item.
|
||||||
|
14
doc/ale.txt
14
doc/ale.txt
@ -770,10 +770,16 @@ The values for `g:ale_fixers` can be a list of |String|, |Funcref|, or
|
|||||||
|lambda| values. String values must either name a function, or a short name
|
|lambda| values. String values must either name a function, or a short name
|
||||||
for a function set in the ALE fixer registry.
|
for a function set in the ALE fixer registry.
|
||||||
|
|
||||||
Each function for fixing errors must accept two arguments `(buffer, lines)`,
|
Each function for fixing errors must accept either one argument `(buffer)` or
|
||||||
representing the buffer being fixed and the lines to fix. The functions must
|
two arguments `(buffer, lines)`, representing the buffer being fixed and the
|
||||||
return either `0`, for changing nothing, a |List| for new lines to set, or a
|
lines to fix. The functions must return either `0`, for changing nothing, a
|
||||||
|Dictionary| for describing a command to be run in the background.
|
|List| for new lines to set, or a |Dictionary| for describing a command to be
|
||||||
|
run in the background.
|
||||||
|
|
||||||
|
Functions receiving a variable number of arguments will not receive the second
|
||||||
|
argument `lines`. Functions should name two arguments if the `lines` argument
|
||||||
|
is desired. This is required to avoid unnecessary copying of the lines of
|
||||||
|
the buffers being checked.
|
||||||
|
|
||||||
When a |Dictionary| is returned for an |ALEFix| callback, the following keys
|
When a |Dictionary| is returned for an |ALEFix| callback, the following keys
|
||||||
are supported for running the commands.
|
are supported for running the commands.
|
||||||
|
@ -35,6 +35,10 @@ Before:
|
|||||||
return {'command': 'cat - <(echo d)'}
|
return {'command': 'cat - <(echo d)'}
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function CatLineOneArg(buffer) abort
|
||||||
|
return {'command': 'cat - <(echo d)'}
|
||||||
|
endfunction
|
||||||
|
|
||||||
function ReplaceWithTempFile(buffer, lines) abort
|
function ReplaceWithTempFile(buffer, lines) abort
|
||||||
return {'command': 'echo x > %t', 'read_temporary_file': 1}
|
return {'command': 'echo x > %t', 'read_temporary_file': 1}
|
||||||
endfunction
|
endfunction
|
||||||
@ -43,6 +47,10 @@ Before:
|
|||||||
return ['a', 'b']
|
return ['a', 'b']
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function RemoveLastLineOneArg(buffer) abort
|
||||||
|
return ['a', 'b']
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! TestCallback(buffer, output)
|
function! TestCallback(buffer, output)
|
||||||
return [{'lnum': 1, 'col': 1, 'text': 'xxx'}]
|
return [{'lnum': 1, 'col': 1, 'text': 'xxx'}]
|
||||||
endfunction
|
endfunction
|
||||||
@ -65,8 +73,10 @@ After:
|
|||||||
delfunction AddDollars
|
delfunction AddDollars
|
||||||
delfunction DoNothing
|
delfunction DoNothing
|
||||||
delfunction CatLine
|
delfunction CatLine
|
||||||
|
delfunction CatLineOneArg
|
||||||
delfunction ReplaceWithTempFile
|
delfunction ReplaceWithTempFile
|
||||||
delfunction RemoveLastLine
|
delfunction RemoveLastLine
|
||||||
|
delfunction RemoveLastLineOneArg
|
||||||
delfunction TestCallback
|
delfunction TestCallback
|
||||||
delfunction SetUpLinters
|
delfunction SetUpLinters
|
||||||
call ale#fix#registry#ResetToDefaults()
|
call ale#fix#registry#ResetToDefaults()
|
||||||
@ -315,3 +325,21 @@ Execute(ale#fix#InitBufferData() should set up the correct data):
|
|||||||
\ 'should_save': 1,
|
\ 'should_save': 1,
|
||||||
\ },
|
\ },
|
||||||
\}, g:ale_fix_buffer_data
|
\}, g:ale_fix_buffer_data
|
||||||
|
|
||||||
|
Execute(ALEFix simple functions should be able to accept one argument, the buffer):
|
||||||
|
let g:ale_fixers.testft = ['RemoveLastLineOneArg']
|
||||||
|
ALEFix
|
||||||
|
|
||||||
|
Expect(There should be only two lines):
|
||||||
|
a
|
||||||
|
b
|
||||||
|
|
||||||
|
Execute(ALEFix functions returning jobs should be able to accept one argument):
|
||||||
|
let g:ale_fixers.testft = ['CatLine']
|
||||||
|
ALEFix
|
||||||
|
|
||||||
|
Expect(An extra line should be added):
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
d
|
||||||
|
Loading…
Reference in New Issue
Block a user