Add support for post-processing fixer output

This commit is contained in:
w0rp 2017-11-22 22:32:53 +00:00
parent fbfde6968a
commit d07b5b71a4
3 changed files with 50 additions and 0 deletions

View File

@ -109,6 +109,15 @@ function! s:HandleExit(job_id, exit_code) abort
endif
let l:chain_callback = get(l:job_info, 'chain_with', v:null)
let l:ProcessWith = get(l:job_info, 'process_with', v:null)
" Post-process the output with a function if we have one.
if l:ProcessWith isnot v:null
let l:job_info.output = call(
\ ale#util#GetFunction(l:ProcessWith),
\ [l:buffer, l:job_info.output]
\)
endif
" Use the output of the job for changing the file if it isn't empty,
" otherwise skip this job and use the input from before.
@ -226,6 +235,7 @@ function! s:RunJob(options) abort
\ 'chain_with': l:chain_with,
\ 'callback_index': a:options.callback_index,
\ 'callback_list': a:options.callback_list,
\ 'process_with': a:options.process_with,
\}
if l:read_temporary_file
@ -329,6 +339,7 @@ function! s:RunFixer(options) abort
\ 'chain_with': l:Chain_with,
\ 'callback_list': a:options.callback_list,
\ 'callback_index': l:index,
\ 'process_with': get(l:result, 'process_with', v:null),
\})
if !l:job_ran

View File

@ -459,6 +459,15 @@ are supported for running the commands.
for commands which need to modify some file on disk in
order to fix files.
`process_with` An optional callback for post-processing.
The callback must accept two arguments,
`(buffer, output)`, which can be used for converting
the output from a command into lines to replace the
buffer's contents with.
A |List| of |String|s must be returned.
`chain_with` An optional key for defining a callback to call next.
The callback must accept two or three arguments,

View File

@ -133,6 +133,25 @@ Before:
return empty(l:lines) ? '' : l:lines[-1]
endfunction
function! FixWithJSONPostProcessing(buffer) abort
let l:ProcessWith = 'JSONPostProcessor'
" Test with lambdas where support is available.
if has('lambda')
let l:ProcessWith = {buffer, output -> JSONPostProcessor(buffer, output)}
endif
return {
\ 'command': 'echo ' . ale#Escape('{"output": ["x", "y", "z"]}'),
\ 'read_buffer': 0,
\ 'process_with': l:ProcessWith,
\}
endfunction
function! JSONPostProcessor(buffer, output) abort
return json_decode(a:output[0]).output
endfunction
After:
Restore
unlet! g:ale_run_synchronously
@ -160,6 +179,8 @@ After:
delfunction GetLastMessage
delfunction IgnoredEmptyOutput
delfunction EchoLineNoPipe
delfunction FixWithJSONPostProcessing
delfunction JSONPostProcessor
call ale#test#RestoreDirectory()
@ -612,3 +633,12 @@ Execute(A temporary file shouldn't be piped into the command when disabled):
Expect(The new line should be used):
new line
Execute(Post-processing should work):
let g:ale_fixers.testft = ['FixWithJSONPostProcessing']
ALEFix
Expect(The lines in the JSON should be used):
x
y
z