From 4dcfdd43e11b1b000d4e27444cf2c0dbd3f22111 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 11 Sep 2016 16:49:55 +0100 Subject: [PATCH] Add support for defining linters where the command that is run is determined by a callback. --- plugin/ale/zmain.vim | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/plugin/ale/zmain.vim b/plugin/ale/zmain.vim index 994422f..abfc4af 100644 --- a/plugin/ale/zmain.vim +++ b/plugin/ale/zmain.vim @@ -13,6 +13,14 @@ let s:linters = {} let s:job_linter_map = {} let s:job_output_map = {} +function! s:GetFunction(string_or_ref) + if type(a:string_or_ref) == type('') + return function(a:string_or_ref) + endif + + return a:string_or_ref +endfunction + function! s:ClearJob(job) let linter = s:job_linter_map[a:job] @@ -73,7 +81,7 @@ function! s:HandleExit(job) call s:ClearJob(a:job) - let linter_loclist = function(linter.callback)(output) + let linter_loclist = s:GetFunction(linter.callback)(output) if b:ale_should_reset_loclist let b:ale_should_reset_loclist = 0 @@ -114,20 +122,29 @@ function! s:ApplyLinter(linter) call s:ClearJob(a:linter.job) endif + let buffer = bufnr('%') + + if has_key(a:linter, 'command_callback') + " If there is a callback for generating a command, call that instead. + let command = s:GetFunction(a:linter.command_callback)(buffer) + else + let command = a:linter.command + endif + if has('nvim') - let a:linter.job = jobstart(a:linter.command, { + let a:linter.job = jobstart(command, { \ 'on_stdout': 's:GatherOutputNeoVim', \ 'on_exit': 's:HandleExitNeoVim', \}) else " Vim 8 will read the stdin from the file's buffer. - let a:linter.job = job_start(a:linter.command, { + let a:linter.job = job_start(command, { \ 'out_mode': 'nl', \ 'err_mode': 'nl', \ 'out_cb': function('s:GatherOutputVim'), \ 'close_cb': function('s:HandleExitVim'), \ 'in_io': 'buffer', - \ 'in_buf': bufnr('%'), + \ 'in_buf': buffer, \}) call ch_close_in(job_getchannel(a:linter.job)) @@ -165,10 +182,15 @@ function! ALEAddLinter(filetype, linter) let s:linters[a:filetype] = [] endif - call add(s:linters[a:filetype], { - \ 'command': a:linter.command, - \ 'callback': a:linter.callback, - \}) + let new_linter = {'callback': a:linter.callback} + + if has_key(a:linter, 'command_callback') + let new_linter.command_callback = a:linter.command_callback + else + let new_linter.command = a:linter.command + endif + + call add(s:linters[a:filetype], new_linter) endfunction function! ALEGetLinters(filetype)