2016-10-03 14:41:02 -04:00
|
|
|
" Author: w0rp <devw0rp@gmail.com>
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
" Description: Backend execution and job management
|
|
|
|
" Executes linters in the background, using NeoVim or Vim 8 jobs
|
2016-09-17 07:06:53 -04:00
|
|
|
|
2016-09-14 06:47:52 -04:00
|
|
|
" Stores information for each job including:
|
|
|
|
"
|
|
|
|
" linter: The linter dictionary for the job.
|
|
|
|
" buffer: The buffer number for the job.
|
2016-10-08 12:27:59 -04:00
|
|
|
" output: The array of lines for the output of the job.
|
2016-09-14 06:47:52 -04:00
|
|
|
let s:job_info_map = {}
|
|
|
|
|
2017-02-17 05:19:44 -05:00
|
|
|
function! ale#engine#ParseVim8ProcessID(job_string) abort
|
|
|
|
return matchstr(a:job_string, '\d\+') + 0
|
|
|
|
endfunction
|
|
|
|
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
function! s:GetJobID(job) abort
|
|
|
|
if has('nvim')
|
|
|
|
"In NeoVim, job values are just IDs.
|
|
|
|
return a:job
|
2016-09-11 11:49:55 -04:00
|
|
|
endif
|
|
|
|
|
2017-02-17 05:19:44 -05:00
|
|
|
" For Vim 8, the job is a different variable type, and we can parse the
|
|
|
|
" process ID from the string.
|
|
|
|
return ale#engine#ParseVim8ProcessID(string(a:job))
|
2016-09-11 11:49:55 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-10-23 17:41:00 -04:00
|
|
|
function! ale#engine#InitBufferInfo(buffer) abort
|
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
2016-10-31 09:45:22 -04:00
|
|
|
" job_list will hold the list of jobs
|
|
|
|
" loclist holds the loclist items after all jobs have completed.
|
2017-03-07 19:23:14 -05:00
|
|
|
" lint_file_loclist holds items from the last run including linters
|
|
|
|
" which use the lint_file option.
|
2016-10-31 09:45:22 -04:00
|
|
|
" new_loclist holds loclist items while jobs are being run.
|
2017-02-11 10:16:08 -05:00
|
|
|
" temporary_file_list holds temporary files to be cleaned up
|
|
|
|
" temporary_directory_list holds temporary directories to be cleaned up
|
2017-02-14 18:44:37 -05:00
|
|
|
" history holds a list of previously run commands for this buffer
|
2016-10-23 17:41:00 -04:00
|
|
|
let g:ale_buffer_info[a:buffer] = {
|
|
|
|
\ 'job_list': [],
|
2016-10-24 15:21:32 -04:00
|
|
|
\ 'loclist': [],
|
2017-03-07 19:23:14 -05:00
|
|
|
\ 'lint_file_loclist': [],
|
2016-10-31 09:45:22 -04:00
|
|
|
\ 'new_loclist': [],
|
2017-02-11 10:16:08 -05:00
|
|
|
\ 'temporary_file_list': [],
|
|
|
|
\ 'temporary_directory_list': [],
|
2017-02-14 18:44:37 -05:00
|
|
|
\ 'history': [],
|
2016-10-23 17:41:00 -04:00
|
|
|
\}
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-02-10 19:08:05 -05:00
|
|
|
" A map from timer IDs to Vim 8 jobs, for tracking jobs that need to be killed
|
|
|
|
" with SIGKILL if they don't terminate right away.
|
|
|
|
let s:job_kill_timers = {}
|
|
|
|
|
2017-04-03 14:50:57 -04:00
|
|
|
" Check if a job is still running, in either Vim version.
|
|
|
|
function! s:IsJobRunning(job) abort
|
|
|
|
if has('nvim')
|
|
|
|
try
|
|
|
|
" In NeoVim, if the job isn't running, jobpid() will throw.
|
|
|
|
call jobpid(a:job)
|
|
|
|
return 1
|
|
|
|
catch
|
|
|
|
endtry
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
return job_status(a:job) ==# 'run'
|
|
|
|
endfunction
|
|
|
|
|
2017-02-10 19:08:05 -05:00
|
|
|
function! s:KillHandler(timer) abort
|
2017-04-03 14:50:57 -04:00
|
|
|
let l:job = remove(s:job_kill_timers, a:timer)
|
|
|
|
|
|
|
|
" For NeoVim, we have to send SIGKILL ourselves manually, as NeoVim
|
|
|
|
" doesn't do it properly.
|
|
|
|
if has('nvim')
|
|
|
|
let l:pid = 0
|
|
|
|
|
|
|
|
" We can fail to get the PID here if the job manages to stop already.
|
|
|
|
try
|
|
|
|
let l:pid = jobpid(l:job)
|
|
|
|
catch
|
|
|
|
endtry
|
|
|
|
|
|
|
|
if l:pid > 0
|
|
|
|
if has('win32')
|
|
|
|
" Windows
|
|
|
|
call system('taskkill /pid ' . l:pid . ' /f')
|
|
|
|
else
|
|
|
|
" Linux, Mac OSX, etc.
|
|
|
|
call system('kill -9 ' . l:pid)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
call job_stop(l:job, 'kill')
|
|
|
|
endif
|
2017-02-10 19:08:05 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-10-23 17:41:00 -04:00
|
|
|
function! ale#engine#ClearJob(job) abort
|
2017-04-03 17:24:30 -04:00
|
|
|
if get(g:, 'ale_run_synchronously') == 1
|
|
|
|
call remove(s:job_info_map, a:job)
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_id = s:GetJobID(a:job)
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2016-09-09 20:37:40 -04:00
|
|
|
if has('nvim')
|
2016-09-08 19:23:26 -04:00
|
|
|
call jobstop(a:job)
|
2016-09-09 20:37:40 -04:00
|
|
|
else
|
2016-09-27 10:06:26 -04:00
|
|
|
" We must close the channel for reading the buffer if it is open
|
|
|
|
" when stopping a job. Otherwise, we will get errors in the status line.
|
|
|
|
if ch_status(job_getchannel(a:job)) ==# 'open'
|
|
|
|
call ch_close_in(job_getchannel(a:job))
|
|
|
|
endif
|
|
|
|
|
2017-02-10 19:08:05 -05:00
|
|
|
" Ask nicely for the job to stop.
|
2016-09-09 20:37:40 -04:00
|
|
|
call job_stop(a:job)
|
2017-04-03 14:50:57 -04:00
|
|
|
endif
|
2017-02-10 19:08:05 -05:00
|
|
|
|
2017-04-03 14:50:57 -04:00
|
|
|
" If a job doesn't stop immediately, queue a timer which will
|
|
|
|
" send SIGKILL to the job, if it's alive by the time the timer ticks.
|
|
|
|
if s:IsJobRunning(a:job)
|
|
|
|
let s:job_kill_timers[timer_start(100, function('s:KillHandler'))] = a:job
|
2016-09-08 19:23:26 -04:00
|
|
|
endif
|
2016-09-09 20:37:40 -04:00
|
|
|
|
2017-02-10 19:08:05 -05:00
|
|
|
if has_key(s:job_info_map, l:job_id)
|
|
|
|
call remove(s:job_info_map, l:job_id)
|
|
|
|
endif
|
2016-10-23 17:41:00 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:StopPreviousJobs(buffer, linter) abort
|
2017-02-04 13:30:30 -05:00
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
|
|
|
" Do nothing if we didn't run anything for the buffer.
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-23 17:41:00 -04:00
|
|
|
let l:new_job_list = []
|
|
|
|
|
|
|
|
for l:job in g:ale_buffer_info[a:buffer].job_list
|
|
|
|
let l:job_id = s:GetJobID(l:job)
|
|
|
|
|
|
|
|
if has_key(s:job_info_map, l:job_id)
|
|
|
|
\&& s:job_info_map[l:job_id].linter.name ==# a:linter.name
|
|
|
|
" Stop jobs which match the buffer and linter.
|
|
|
|
call ale#engine#ClearJob(l:job)
|
|
|
|
else
|
|
|
|
" Keep other jobs in the list.
|
|
|
|
call add(l:new_job_list, l:job)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" Update the list, removing the previously run job.
|
|
|
|
let g:ale_buffer_info[a:buffer].job_list = l:new_job_list
|
2016-09-08 19:23:26 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-01-19 15:01:51 -05:00
|
|
|
function! s:GatherOutputVim(channel, data) abort
|
|
|
|
let l:job_id = s:GetJobID(ch_getjob(a:channel))
|
2016-10-08 12:27:59 -04:00
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
if !has_key(s:job_info_map, l:job_id)
|
2016-09-08 19:23:26 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-01-19 15:01:51 -05:00
|
|
|
call add(s:job_info_map[l:job_id].output, a:data)
|
2016-09-08 19:23:26 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-01-19 15:01:51 -05:00
|
|
|
function! s:GatherOutputNeoVim(job, data, event) abort
|
|
|
|
let l:job_id = s:GetJobID(a:job)
|
|
|
|
|
|
|
|
if !has_key(s:job_info_map, l:job_id)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Join the lines passed to ale, because Neovim splits them up.
|
|
|
|
" a:data is a list of strings, where every item is a new line, except the
|
|
|
|
" first one, which is the continuation of the last item passed last time.
|
|
|
|
call ale#engine#JoinNeovimOutput(s:job_info_map[l:job_id].output, a:data)
|
2016-09-09 20:37:40 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-01-19 15:01:51 -05:00
|
|
|
function! ale#engine#JoinNeovimOutput(output, data) abort
|
|
|
|
if empty(a:output)
|
|
|
|
call extend(a:output, a:data)
|
|
|
|
else
|
|
|
|
" Extend the previous line, which can be continued.
|
|
|
|
let a:output[-1] .= get(a:data, 0, '')
|
|
|
|
|
|
|
|
" Add the new lines.
|
|
|
|
call extend(a:output, a:data[1:])
|
|
|
|
endif
|
2016-10-04 13:17:02 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-02-11 10:16:08 -05:00
|
|
|
" Register a temporary file to be managed with the ALE engine for
|
|
|
|
" a current job run.
|
|
|
|
function! ale#engine#ManageFile(buffer, filename) abort
|
|
|
|
call add(g:ale_buffer_info[a:buffer].temporary_file_list, a:filename)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Same as the above, but manage an entire directory.
|
|
|
|
function! ale#engine#ManageDirectory(buffer, directory) abort
|
|
|
|
call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory)
|
|
|
|
endfunction
|
|
|
|
|
2017-03-31 15:14:53 -04:00
|
|
|
" Create a new temporary directory and manage it in one go.
|
|
|
|
function! ale#engine#CreateDirectory(buffer) abort
|
|
|
|
let l:temporary_directory = tempname()
|
|
|
|
" Create the temporary directory for the file, unreadable by 'other'
|
|
|
|
" users.
|
|
|
|
call mkdir(l:temporary_directory, '', 0750)
|
|
|
|
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
|
|
|
|
|
|
|
|
return l:temporary_directory
|
|
|
|
endfunction
|
|
|
|
|
2017-02-11 10:16:08 -05:00
|
|
|
function! ale#engine#RemoveManagedFiles(buffer) abort
|
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-02-14 16:02:49 -05:00
|
|
|
" We can't delete anything in a sandbox, so wait until we escape from
|
|
|
|
" it to delete temporary files and directories.
|
|
|
|
if ale#util#InSandbox()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-02-11 10:16:08 -05:00
|
|
|
" Delete files with a call akin to a plan `rm` command.
|
|
|
|
for l:filename in g:ale_buffer_info[a:buffer].temporary_file_list
|
|
|
|
call delete(l:filename)
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let g:ale_buffer_info[a:buffer].temporary_file_list = []
|
|
|
|
|
|
|
|
" Delete directories like `rm -rf`.
|
|
|
|
" Directories are handled differently from files, so paths that are
|
|
|
|
" intended to be single files can be set up for automatic deletion without
|
|
|
|
" accidentally deleting entire directories.
|
|
|
|
for l:directory in g:ale_buffer_info[a:buffer].temporary_directory_list
|
|
|
|
call delete(l:directory, 'rf')
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let g:ale_buffer_info[a:buffer].temporary_directory_list = []
|
|
|
|
endfunction
|
|
|
|
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
function! s:HandleExit(job) abort
|
2016-10-08 18:55:58 -04:00
|
|
|
if a:job ==# 'no process'
|
2016-10-08 16:04:42 -04:00
|
|
|
" Stop right away when the job is not valid in Vim 8.
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_id = s:GetJobID(a:job)
|
2016-10-08 12:27:59 -04:00
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
if !has_key(s:job_info_map, l:job_id)
|
2016-09-08 19:23:26 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_info = s:job_info_map[l:job_id]
|
|
|
|
let l:linter = l:job_info.linter
|
|
|
|
let l:output = l:job_info.output
|
|
|
|
let l:buffer = l:job_info.buffer
|
2016-10-25 15:25:23 -04:00
|
|
|
let l:next_chain_index = l:job_info.next_chain_index
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2016-10-24 04:58:45 -04:00
|
|
|
" Call the same function for stopping jobs again to clean up the job
|
|
|
|
" which just closed.
|
2016-10-23 17:41:00 -04:00
|
|
|
call s:StopPreviousJobs(l:buffer, l:linter)
|
2016-10-13 10:13:11 -04:00
|
|
|
|
2017-02-14 16:02:49 -05:00
|
|
|
" Stop here if we land in the handle for a job completing if we're in
|
|
|
|
" a sandbox.
|
|
|
|
if ale#util#InSandbox()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-25 15:25:23 -04:00
|
|
|
if l:next_chain_index < len(get(l:linter, 'command_chain', []))
|
|
|
|
call s:InvokeChain(l:buffer, l:linter, l:next_chain_index, l:output)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-02-16 18:18:57 -05:00
|
|
|
" Log the output of the command for ALEInfo if we should.
|
|
|
|
if g:ale_history_enabled && g:ale_history_log_output
|
|
|
|
call ale#history#RememberOutput(l:buffer, l:job_id, l:output[:])
|
|
|
|
endif
|
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:linter_loclist = ale#util#GetFunction(l:linter.callback)(l:buffer, l:output)
|
2016-09-14 06:47:52 -04:00
|
|
|
|
2017-02-26 09:51:22 -05:00
|
|
|
" Make some adjustments to the loclists to fix common problems, and also
|
|
|
|
" to set default values for loclist items.
|
|
|
|
let l:linter_loclist = ale#engine#FixLocList(l:buffer, l:linter, l:linter_loclist)
|
2016-10-10 14:56:05 -04:00
|
|
|
|
2016-09-08 19:23:26 -04:00
|
|
|
" Add the loclist items from the linter.
|
2017-03-21 10:52:02 -04:00
|
|
|
" loclist items for files which are checked go into a different list,
|
|
|
|
" and are kept between runs.
|
|
|
|
if l:linter.lint_file
|
|
|
|
call extend(g:ale_buffer_info[l:buffer].lint_file_loclist, l:linter_loclist)
|
|
|
|
else
|
|
|
|
call extend(g:ale_buffer_info[l:buffer].new_loclist, l:linter_loclist)
|
|
|
|
endif
|
2016-10-31 09:45:22 -04:00
|
|
|
|
|
|
|
if !empty(g:ale_buffer_info[l:buffer].job_list)
|
|
|
|
" Wait for all jobs to complete before doing anything else.
|
|
|
|
return
|
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2017-02-11 10:16:08 -05:00
|
|
|
" Automatically remove all managed temporary files and directories
|
|
|
|
" now that all jobs have completed.
|
|
|
|
call ale#engine#RemoveManagedFiles(l:buffer)
|
|
|
|
|
2017-03-21 10:52:02 -04:00
|
|
|
" Combine the lint_file List and the List for everything else.
|
|
|
|
let l:combined_list = g:ale_buffer_info[l:buffer].lint_file_loclist
|
|
|
|
\ + g:ale_buffer_info[l:buffer].new_loclist
|
|
|
|
|
2016-09-08 19:23:26 -04:00
|
|
|
" Sort the loclist again.
|
|
|
|
" We need a sorted list so we can run a binary search against it
|
|
|
|
" for efficient lookup of the messages in the cursor handler.
|
2017-03-21 10:52:02 -04:00
|
|
|
call sort(l:combined_list, 'ale#util#LocItemCompare')
|
2016-10-31 09:45:22 -04:00
|
|
|
|
|
|
|
" Now swap the old and new loclists, after we have collected everything
|
|
|
|
" and sorted the list again.
|
2017-03-21 10:52:02 -04:00
|
|
|
let g:ale_buffer_info[l:buffer].loclist = l:combined_list
|
2016-10-31 09:45:22 -04:00
|
|
|
let g:ale_buffer_info[l:buffer].new_loclist = []
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2017-02-09 13:47:14 -05:00
|
|
|
call ale#engine#SetResults(l:buffer, g:ale_buffer_info[l:buffer].loclist)
|
|
|
|
|
|
|
|
" Call user autocommands. This allows users to hook into ALE's lint cycle.
|
|
|
|
silent doautocmd User ALELint
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#engine#SetResults(buffer, loclist) abort
|
2017-03-14 19:04:25 -04:00
|
|
|
" Set signs first. This could potentially fix some line numbers.
|
2017-03-21 10:52:02 -04:00
|
|
|
" The List could be sorted again here by SetSigns.
|
2016-09-08 19:23:26 -04:00
|
|
|
if g:ale_set_signs
|
2017-02-09 13:47:14 -05:00
|
|
|
call ale#sign#SetSigns(a:buffer, a:loclist)
|
2016-09-08 19:23:26 -04:00
|
|
|
endif
|
|
|
|
|
2017-03-14 19:04:25 -04:00
|
|
|
if g:ale_set_quickfix || g:ale_set_loclist
|
|
|
|
call ale#list#SetLists(a:buffer, a:loclist)
|
|
|
|
endif
|
|
|
|
|
Implement a more efficient statusbar
The statusbar now keeps its state in a separate variable, in order to
avoid excess iterations. The engine now updates said variable on run,
and a new function is made available for external statusbars to call (to
avoid dependencies on internal implementation details of ale).
To keep things light, the status bar code is not loaded unless invoked
by the user or an external plugin. On the first load it will update
itself from the global loclist, after that, the engine will handle all
updates.
The external integration function, `ale#statusline#Count()`, will return
a tuple in the format [E, W] (where E is errors, W is warnings), unless
no data exists (ie, the plugin doesn't have a linter for a file or has
not run yet), in which case it returns 0/false.
2016-10-11 17:51:01 -04:00
|
|
|
if exists('*ale#statusline#Update')
|
|
|
|
" Don't load/run if not already loaded.
|
2017-02-09 13:47:14 -05:00
|
|
|
call ale#statusline#Update(a:buffer, a:loclist)
|
Implement a more efficient statusbar
The statusbar now keeps its state in a separate variable, in order to
avoid excess iterations. The engine now updates said variable on run,
and a new function is made available for external statusbars to call (to
avoid dependencies on internal implementation details of ale).
To keep things light, the status bar code is not loaded unless invoked
by the user or an external plugin. On the first load it will update
itself from the global loclist, after that, the engine will handle all
updates.
The external integration function, `ale#statusline#Count()`, will return
a tuple in the format [E, W] (where E is errors, W is warnings), unless
no data exists (ie, the plugin doesn't have a linter for a file or has
not run yet), in which case it returns 0/false.
2016-10-11 17:51:01 -04:00
|
|
|
endif
|
2017-02-12 19:18:51 -05:00
|
|
|
|
|
|
|
if g:ale_set_highlights
|
|
|
|
call ale#highlight#SetHighlights(a:buffer, a:loclist)
|
|
|
|
endif
|
2017-03-02 18:36:31 -05:00
|
|
|
|
|
|
|
if g:ale_echo_cursor
|
|
|
|
" Try and echo the warning now.
|
|
|
|
" This will only do something meaningful if we're in normal mode.
|
|
|
|
call ale#cursor#EchoCursorWarning()
|
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-02-16 17:19:53 -05:00
|
|
|
function! s:SetExitCode(job, exit_code) abort
|
|
|
|
let l:job_id = s:GetJobID(a:job)
|
|
|
|
|
|
|
|
if !has_key(s:job_info_map, l:job_id)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:buffer = s:job_info_map[l:job_id].buffer
|
|
|
|
|
|
|
|
call ale#history#SetExitCode(l:buffer, l:job_id, a:exit_code)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:HandleExitNeoVim(job, exit_code, event) abort
|
|
|
|
if g:ale_history_enabled
|
|
|
|
call s:SetExitCode(a:job, a:exit_code)
|
|
|
|
endif
|
|
|
|
|
2016-09-09 20:37:40 -04:00
|
|
|
call s:HandleExit(a:job)
|
|
|
|
endfunction
|
|
|
|
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
function! s:HandleExitVim(channel) abort
|
2016-09-09 20:37:40 -04:00
|
|
|
call s:HandleExit(ch_getjob(a:channel))
|
|
|
|
endfunction
|
|
|
|
|
2017-02-16 17:19:53 -05:00
|
|
|
" Vim returns the exit status with one callback,
|
|
|
|
" and the channel will close later in another callback.
|
|
|
|
function! s:HandleExitStatusVim(job, exit_code) abort
|
|
|
|
call s:SetExitCode(a:job, a:exit_code)
|
|
|
|
endfunction
|
|
|
|
|
2017-02-26 09:51:22 -05:00
|
|
|
function! ale#engine#FixLocList(buffer, linter, loclist) abort
|
|
|
|
let l:new_loclist = []
|
|
|
|
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
" Some errors have line numbers beyond the end of the file,
|
|
|
|
" so we need to adjust them so they set the error at the last line
|
|
|
|
" of the file instead.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:last_line_number = ale#util#GetLineCount(a:buffer)
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
|
2017-02-26 09:51:22 -05:00
|
|
|
for l:old_item in a:loclist
|
|
|
|
" Copy the loclist item with some default values and corrections.
|
|
|
|
"
|
|
|
|
" line and column numbers will be converted to numbers.
|
|
|
|
" The buffer will default to the buffer being checked.
|
|
|
|
" The vcol setting will default to 0, a byte index.
|
|
|
|
" The error type will default to 'E' for errors.
|
|
|
|
" The error number will default to -1.
|
|
|
|
"
|
|
|
|
" The line number and text are the only required keys.
|
|
|
|
"
|
|
|
|
" The linter_name will be set on the errors so it can be used in
|
|
|
|
" output, filtering, etc..
|
|
|
|
let l:item = {
|
|
|
|
\ 'text': l:old_item.text,
|
|
|
|
\ 'lnum': str2nr(l:old_item.lnum),
|
|
|
|
\ 'col': str2nr(get(l:old_item, 'col', 0)),
|
|
|
|
\ 'bufnr': get(l:old_item, 'bufnr', a:buffer),
|
|
|
|
\ 'vcol': get(l:old_item, 'vcol', 0),
|
|
|
|
\ 'type': get(l:old_item, 'type', 'E'),
|
|
|
|
\ 'nr': get(l:old_item, 'nr', -1),
|
|
|
|
\ 'linter_name': a:linter.name,
|
|
|
|
\}
|
|
|
|
|
2017-03-02 02:14:30 -05:00
|
|
|
if has_key(l:old_item, 'detail')
|
|
|
|
let l:item.detail = l:old_item.detail
|
|
|
|
endif
|
|
|
|
|
2016-10-10 19:00:09 -04:00
|
|
|
if l:item.lnum == 0
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
" When errors appear at line 0, put them at line 1 instead.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:item.lnum = 1
|
|
|
|
elseif l:item.lnum > l:last_line_number
|
2017-02-26 09:51:22 -05:00
|
|
|
" When errors go beyond the end of the file, put them at the end.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:item.lnum = l:last_line_number
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
endif
|
2017-02-26 09:51:22 -05:00
|
|
|
|
|
|
|
call add(l:new_loclist, l:item)
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
endfor
|
2017-02-26 09:51:22 -05:00
|
|
|
|
|
|
|
return l:new_loclist
|
First pass at optimizing ale to autoload (#80)
* First pass at optimizing ale to autoload
First off, the structure/function names should be revised a bit,
but I will wait for @w0rp's input before unifying the naming style.
Second off, the docs probably need some more work, I just did some
simple find-and-replace work.
With that said, this pull brings major performance gains for ale. On my
slowest system, fully loading ale and all its code takes around 150ms.
I have moved all of ale's autoload-able code to autoload/, and in
addition, implemented lazy-loading of linters. This brings load time on
that same system down to 5ms.
The only downside of lazy loading is that `g:ale_linters` cannot be
changed at runtime; however, it also speeds up performance at runtime by
simplfying the logic greatly.
Please let me know what you think!
Closes #59
* Address Travis/Vint errors
For some reason, ale isn't running vint for me...
* Incorporate feedback, make fixes
Lazy-loading logic is much improved.
* Add header comments; remove incorrect workaround
* Remove unneeded plugin guards
* Fix lazy-loading linter logic
Set the wrong variable....
* Fix capitialization
2016-10-10 14:51:29 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-02-11 13:14:18 -05:00
|
|
|
" Given part of a command, replace any % with %%, so that no characters in
|
|
|
|
" the string will be replaced with filenames, etc.
|
|
|
|
function! ale#engine#EscapeCommandPart(command_part) abort
|
|
|
|
return substitute(a:command_part, '%', '%%', 'g')
|
|
|
|
endfunction
|
|
|
|
|
2017-02-11 17:43:13 -05:00
|
|
|
function! s:TemporaryFilename(buffer) abort
|
2017-02-13 05:36:38 -05:00
|
|
|
let l:filename = fnamemodify(bufname(a:buffer), ':t')
|
|
|
|
|
|
|
|
if empty(l:filename)
|
|
|
|
" If the buffer's filename is empty, create a dummy filename.
|
|
|
|
let l:ft = getbufvar(a:buffer, '&filetype')
|
|
|
|
let l:filename = 'file' . ale#filetypes#GuessExtension(l:ft)
|
|
|
|
endif
|
|
|
|
|
2017-02-11 17:43:13 -05:00
|
|
|
" Create a temporary filename, <temp_dir>/<original_basename>
|
|
|
|
" The file itself will not be created by this function.
|
2017-02-13 05:36:38 -05:00
|
|
|
return tempname() . (has('win32') ? '\' : '/') . l:filename
|
2017-02-11 17:43:13 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-02-11 13:14:18 -05:00
|
|
|
" Given a command string, replace every...
|
|
|
|
" %s -> with the current filename
|
|
|
|
" %t -> with the name of an unused file in a temporary directory
|
|
|
|
" %% -> with a literal %
|
|
|
|
function! ale#engine#FormatCommand(buffer, command) abort
|
|
|
|
let l:temporary_file = ''
|
|
|
|
let l:command = a:command
|
|
|
|
|
|
|
|
" First replace all uses of %%, used for literal percent characters,
|
|
|
|
" with an ugly string.
|
|
|
|
let l:command = substitute(l:command, '%%', '<<PERCENTS>>', 'g')
|
|
|
|
|
|
|
|
" Replace all %s occurences in the string with the name of the current
|
|
|
|
" file.
|
|
|
|
if l:command =~# '%s'
|
|
|
|
let l:filename = fnamemodify(bufname(a:buffer), ':p')
|
2017-02-11 15:34:14 -05:00
|
|
|
let l:command = substitute(l:command, '%s', '\=fnameescape(l:filename)', 'g')
|
2017-02-11 13:14:18 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
if l:command =~# '%t'
|
|
|
|
" Create a temporary filename, <temp_dir>/<original_basename>
|
|
|
|
" The file itself will not be created by this function.
|
2017-02-11 17:43:13 -05:00
|
|
|
let l:temporary_file = s:TemporaryFilename(a:buffer)
|
2017-02-11 15:34:14 -05:00
|
|
|
let l:command = substitute(l:command, '%t', '\=fnameescape(l:temporary_file)', 'g')
|
2017-02-11 13:14:18 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Finish formatting so %% becomes %.
|
|
|
|
let l:command = substitute(l:command, '<<PERCENTS>>', '%', 'g')
|
|
|
|
|
|
|
|
return [l:temporary_file, l:command]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:CreateTemporaryFileForJob(buffer, temporary_file) abort
|
|
|
|
if empty(a:temporary_file)
|
|
|
|
" There is no file, so we didn't create anything.
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:temporary_directory = fnamemodify(a:temporary_file, ':h')
|
|
|
|
" Create the temporary directory for the file, unreadable by 'other'
|
|
|
|
" users.
|
|
|
|
call mkdir(l:temporary_directory, '', 0750)
|
|
|
|
" Automatically delete the directory later.
|
|
|
|
call ale#engine#ManageDirectory(a:buffer, l:temporary_directory)
|
|
|
|
" Write the buffer out to a file.
|
2017-02-11 13:19:01 -05:00
|
|
|
call writefile(getbufline(a:buffer, 1, '$'), a:temporary_file)
|
2017-02-11 13:14:18 -05:00
|
|
|
|
|
|
|
return 1
|
|
|
|
endfunction
|
|
|
|
|
2017-02-09 18:32:57 -05:00
|
|
|
function! s:RunJob(options) abort
|
|
|
|
let l:command = a:options.command
|
|
|
|
let l:buffer = a:options.buffer
|
|
|
|
let l:linter = a:options.linter
|
|
|
|
let l:output_stream = a:options.output_stream
|
|
|
|
let l:next_chain_index = a:options.next_chain_index
|
|
|
|
let l:read_buffer = a:options.read_buffer
|
2016-09-11 11:49:55 -04:00
|
|
|
|
2017-02-11 13:14:18 -05:00
|
|
|
let [l:temporary_file, l:command] = ale#engine#FormatCommand(l:buffer, l:command)
|
|
|
|
|
2017-02-11 17:43:13 -05:00
|
|
|
if l:read_buffer && empty(l:temporary_file)
|
|
|
|
" If we are to send the Vim buffer to a command, we'll do it
|
|
|
|
" in the shell. We'll write out the file to a temporary file,
|
|
|
|
" and then read it back in, in the shell.
|
|
|
|
let l:temporary_file = s:TemporaryFilename(l:buffer)
|
|
|
|
let l:command = l:command . ' < ' . fnameescape(l:temporary_file)
|
|
|
|
endif
|
|
|
|
|
2017-02-11 13:14:18 -05:00
|
|
|
if s:CreateTemporaryFileForJob(l:buffer, l:temporary_file)
|
|
|
|
" If a temporary filename has been formatted in to the command, then
|
|
|
|
" we do not need to send the Vim buffer to the command.
|
|
|
|
let l:read_buffer = 0
|
2016-10-04 08:50:44 -04:00
|
|
|
endif
|
|
|
|
|
2017-04-03 17:24:30 -04:00
|
|
|
if !has('nvim')
|
|
|
|
" The command will be executed in a subshell. This fixes a number of
|
|
|
|
" issues, including reading the PATH variables correctly, %PATHEXT%
|
|
|
|
" expansion on Windows, etc.
|
|
|
|
"
|
|
|
|
" NeoVim handles this issue automatically if the command is a String.
|
|
|
|
let l:command = has('win32')
|
|
|
|
\ ? 'cmd /c ' . l:command
|
|
|
|
\ : split(&shell) + split(&shellcmdflag) + [l:command]
|
|
|
|
endif
|
|
|
|
|
|
|
|
if get(g:, 'ale_run_synchronously') == 1
|
|
|
|
" Find a unique Job value to use, which will be the same as the ID for
|
|
|
|
" running commands synchronously. This is only for test code.
|
|
|
|
let l:job = len(s:job_info_map) + 1
|
|
|
|
|
|
|
|
while has_key(s:job_info_map, l:job)
|
|
|
|
let l:job += 1
|
|
|
|
endwhile
|
|
|
|
elseif has('nvim')
|
2016-10-25 15:25:23 -04:00
|
|
|
if l:output_stream ==# 'stderr'
|
2016-09-15 15:20:41 -04:00
|
|
|
" Read from stderr instead of stdout.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job = jobstart(l:command, {
|
2016-12-14 11:40:45 -05:00
|
|
|
\ 'on_stderr': function('s:GatherOutputNeoVim'),
|
|
|
|
\ 'on_exit': function('s:HandleExitNeoVim'),
|
2016-09-15 15:20:41 -04:00
|
|
|
\})
|
2016-10-25 15:25:23 -04:00
|
|
|
elseif l:output_stream ==# 'both'
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job = jobstart(l:command, {
|
2016-12-14 11:40:45 -05:00
|
|
|
\ 'on_stdout': function('s:GatherOutputNeoVim'),
|
|
|
|
\ 'on_stderr': function('s:GatherOutputNeoVim'),
|
|
|
|
\ 'on_exit': function('s:HandleExitNeoVim'),
|
2016-10-07 12:08:11 -04:00
|
|
|
\})
|
2016-09-15 15:20:41 -04:00
|
|
|
else
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job = jobstart(l:command, {
|
2016-12-14 11:40:45 -05:00
|
|
|
\ 'on_stdout': function('s:GatherOutputNeoVim'),
|
|
|
|
\ 'on_exit': function('s:HandleExitNeoVim'),
|
2016-09-15 15:20:41 -04:00
|
|
|
\})
|
|
|
|
endif
|
2016-09-09 20:37:40 -04:00
|
|
|
else
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_options = {
|
2016-10-08 16:52:41 -04:00
|
|
|
\ 'in_mode': 'nl',
|
2016-09-09 20:37:40 -04:00
|
|
|
\ 'out_mode': 'nl',
|
|
|
|
\ 'err_mode': 'nl',
|
|
|
|
\ 'close_cb': function('s:HandleExitVim'),
|
2016-09-15 15:20:41 -04:00
|
|
|
\}
|
|
|
|
|
2017-02-16 17:19:53 -05:00
|
|
|
if g:ale_history_enabled
|
|
|
|
" We only need to capture the exit status if we are going to
|
|
|
|
" save it in the history. Otherwise, we don't care.
|
|
|
|
let l:job_options.exit_cb = function('s:HandleExitStatusVim')
|
|
|
|
endif
|
|
|
|
|
2016-10-25 15:25:23 -04:00
|
|
|
if l:output_stream ==# 'stderr'
|
2016-09-15 15:20:41 -04:00
|
|
|
" Read from stderr instead of stdout.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_options.err_cb = function('s:GatherOutputVim')
|
2016-10-25 15:25:23 -04:00
|
|
|
elseif l:output_stream ==# 'both'
|
2016-10-07 12:08:11 -04:00
|
|
|
" Read from both streams.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_options.out_cb = function('s:GatherOutputVim')
|
|
|
|
let l:job_options.err_cb = function('s:GatherOutputVim')
|
2016-09-15 15:20:41 -04:00
|
|
|
else
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job_options.out_cb = function('s:GatherOutputVim')
|
2016-09-15 15:20:41 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Vim 8 will read the stdin from the file's buffer.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:job = job_start(l:command, l:job_options)
|
2016-09-09 20:37:40 -04:00
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2017-02-14 18:44:37 -05:00
|
|
|
let l:status = 'failed'
|
|
|
|
let l:job_id = 0
|
|
|
|
|
2016-10-08 16:52:41 -04:00
|
|
|
" Only proceed if the job is being run.
|
2017-04-03 17:24:30 -04:00
|
|
|
if has('nvim')
|
|
|
|
\ || get(g:, 'ale_run_synchronously') == 1
|
|
|
|
\ || (l:job !=# 'no process' && job_status(l:job) ==# 'run')
|
2016-10-23 17:41:00 -04:00
|
|
|
" Add the job to the list of jobs, so we can track them.
|
2016-10-25 15:25:23 -04:00
|
|
|
call add(g:ale_buffer_info[l:buffer].job_list, l:job)
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2017-02-16 16:18:03 -05:00
|
|
|
let l:status = 'started'
|
2017-02-14 18:44:37 -05:00
|
|
|
let l:job_id = s:GetJobID(l:job)
|
2016-10-08 16:04:42 -04:00
|
|
|
" Store the ID for the job in the map to read back again.
|
2017-02-14 18:44:37 -05:00
|
|
|
let s:job_info_map[l:job_id] = {
|
2016-10-25 15:25:23 -04:00
|
|
|
\ 'linter': l:linter,
|
|
|
|
\ 'buffer': l:buffer,
|
2016-10-08 16:04:42 -04:00
|
|
|
\ 'output': [],
|
2016-10-25 15:25:23 -04:00
|
|
|
\ 'next_chain_index': l:next_chain_index,
|
2016-10-08 16:04:42 -04:00
|
|
|
\}
|
2016-09-09 20:37:40 -04:00
|
|
|
endif
|
2017-02-14 18:44:37 -05:00
|
|
|
|
2017-02-16 16:33:44 -05:00
|
|
|
if g:ale_history_enabled
|
|
|
|
call ale#history#Add(l:buffer, l:status, l:job_id, l:command)
|
|
|
|
else
|
|
|
|
let g:ale_buffer_info[l:buffer].history = []
|
|
|
|
endif
|
2017-04-03 17:24:30 -04:00
|
|
|
|
|
|
|
if get(g:, 'ale_run_synchronously') == 1
|
|
|
|
" Run a command synchronously if this test option is set.
|
|
|
|
let s:job_info_map[l:job_id].output = systemlist(
|
|
|
|
\ type(l:command) == type([])
|
|
|
|
\ ? join(l:command[0:1]) . ' ' . shellescape(l:command[2])
|
|
|
|
\ : l:command
|
|
|
|
\)
|
|
|
|
call s:HandleExit(l:job)
|
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
endfunction
|
2016-10-17 18:26:19 -04:00
|
|
|
|
2017-02-09 18:32:57 -05:00
|
|
|
" Determine which commands to run for a link in a command chain, or
|
|
|
|
" just a regular command.
|
|
|
|
function! ale#engine#ProcessChain(buffer, linter, chain_index, input) abort
|
2016-10-25 15:25:23 -04:00
|
|
|
let l:output_stream = get(a:linter, 'output_stream', 'stdout')
|
2017-02-09 18:32:57 -05:00
|
|
|
let l:read_buffer = a:linter.read_buffer
|
2017-02-04 13:30:30 -05:00
|
|
|
let l:chain_index = a:chain_index
|
|
|
|
let l:input = a:input
|
2016-10-25 15:25:23 -04:00
|
|
|
|
|
|
|
if has_key(a:linter, 'command_chain')
|
2017-02-04 13:30:30 -05:00
|
|
|
while l:chain_index < len(a:linter.command_chain)
|
|
|
|
" Run a chain of commands, one asychronous command after the other,
|
|
|
|
" so that many programs can be run in a sequence.
|
|
|
|
let l:chain_item = a:linter.command_chain[l:chain_index]
|
|
|
|
|
|
|
|
if l:chain_index == 0
|
|
|
|
" The first callback in the chain takes only a buffer number.
|
|
|
|
let l:command = ale#util#GetFunction(l:chain_item.callback)(
|
|
|
|
\ a:buffer
|
|
|
|
\)
|
|
|
|
else
|
|
|
|
" The second callback in the chain takes some input too.
|
|
|
|
let l:command = ale#util#GetFunction(l:chain_item.callback)(
|
|
|
|
\ a:buffer,
|
|
|
|
\ l:input
|
|
|
|
\)
|
|
|
|
endif
|
2016-10-25 15:25:23 -04:00
|
|
|
|
2017-02-04 13:30:30 -05:00
|
|
|
if !empty(l:command)
|
|
|
|
" We hit a command to run, so we'll execute that
|
2017-02-09 18:32:57 -05:00
|
|
|
|
|
|
|
" The chain item can override the output_stream option.
|
|
|
|
if has_key(l:chain_item, 'output_stream')
|
|
|
|
let l:output_stream = l:chain_item.output_stream
|
|
|
|
endif
|
|
|
|
|
|
|
|
" The chain item can override the read_buffer option.
|
|
|
|
if has_key(l:chain_item, 'read_buffer')
|
|
|
|
let l:read_buffer = l:chain_item.read_buffer
|
|
|
|
elseif l:chain_index != len(a:linter.command_chain) - 1
|
|
|
|
" Don't read the buffer for commands besides the last one
|
|
|
|
" in the chain by default.
|
|
|
|
let l:read_buffer = 0
|
|
|
|
endif
|
|
|
|
|
2017-02-04 13:30:30 -05:00
|
|
|
break
|
|
|
|
endif
|
2016-10-25 15:25:23 -04:00
|
|
|
|
2017-02-04 13:30:30 -05:00
|
|
|
" Command chain items can return an empty string to indicate that
|
|
|
|
" a command should be skipped, so we should try the next item
|
|
|
|
" with no input.
|
|
|
|
let l:input = []
|
|
|
|
let l:chain_index += 1
|
|
|
|
endwhile
|
2016-10-25 15:25:23 -04:00
|
|
|
elseif has_key(a:linter, 'command_callback')
|
|
|
|
" If there is a callback for generating a command, call that instead.
|
|
|
|
let l:command = ale#util#GetFunction(a:linter.command_callback)(a:buffer)
|
|
|
|
else
|
|
|
|
let l:command = a:linter.command
|
|
|
|
endif
|
|
|
|
|
2017-02-09 18:32:57 -05:00
|
|
|
if empty(l:command)
|
|
|
|
" Don't run any jobs if the command is an empty string.
|
|
|
|
return {}
|
|
|
|
endif
|
2017-02-08 04:37:12 -05:00
|
|
|
|
2017-02-09 18:32:57 -05:00
|
|
|
return {
|
|
|
|
\ 'command': l:command,
|
2016-10-25 15:25:23 -04:00
|
|
|
\ 'buffer': a:buffer,
|
|
|
|
\ 'linter': a:linter,
|
|
|
|
\ 'output_stream': l:output_stream,
|
2017-02-04 13:30:30 -05:00
|
|
|
\ 'next_chain_index': l:chain_index + 1,
|
2017-02-09 18:32:57 -05:00
|
|
|
\ 'read_buffer': l:read_buffer,
|
|
|
|
\}
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:InvokeChain(buffer, linter, chain_index, input) abort
|
|
|
|
let l:options = ale#engine#ProcessChain(a:buffer, a:linter, a:chain_index, a:input)
|
|
|
|
|
|
|
|
if !empty(l:options)
|
|
|
|
call s:RunJob(l:options)
|
2017-02-11 10:16:08 -05:00
|
|
|
elseif empty(g:ale_buffer_info[a:buffer].job_list)
|
|
|
|
" If we cancelled running a command, and we have no jobs in progress,
|
|
|
|
" then delete the managed temporary files now.
|
|
|
|
call ale#engine#RemoveManagedFiles(a:buffer)
|
2017-02-09 18:32:57 -05:00
|
|
|
endif
|
2016-10-25 15:25:23 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#engine#Invoke(buffer, linter) abort
|
|
|
|
" Stop previous jobs for the same linter.
|
|
|
|
call s:StopPreviousJobs(a:buffer, a:linter)
|
2017-03-14 19:51:57 -04:00
|
|
|
|
|
|
|
let l:executable = has_key(a:linter, 'executable_callback')
|
|
|
|
\ ? ale#util#GetFunction(a:linter.executable_callback)(a:buffer)
|
|
|
|
\ : a:linter.executable
|
|
|
|
|
|
|
|
" Run this program if it can be executed.
|
|
|
|
if executable(l:executable)
|
|
|
|
call s:InvokeChain(a:buffer, a:linter, 0, [])
|
|
|
|
endif
|
2016-10-25 15:25:23 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-10-24 15:21:32 -04:00
|
|
|
" Given a buffer number, return the warnings and errors for a given buffer.
|
|
|
|
function! ale#engine#GetLoclist(buffer) abort
|
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
|
|
|
return g:ale_buffer_info[a:buffer].loclist
|
|
|
|
endfunction
|
|
|
|
|
2016-10-17 18:26:19 -04:00
|
|
|
" This function can be called with a timeout to wait for all jobs to finish.
|
|
|
|
" If the jobs to not finish in the given number of milliseconds,
|
|
|
|
" an exception will be thrown.
|
|
|
|
"
|
|
|
|
" The time taken will be a very rough approximation, and more time may be
|
|
|
|
" permitted than is specified.
|
|
|
|
function! ale#engine#WaitForJobs(deadline) abort
|
2017-03-09 15:22:02 -05:00
|
|
|
let l:start_time = ale#util#ClockMilliseconds()
|
2016-10-17 18:43:31 -04:00
|
|
|
|
|
|
|
if l:start_time == 0
|
|
|
|
throw 'Failed to read milliseconds from the clock!'
|
|
|
|
endif
|
|
|
|
|
2016-10-17 18:26:19 -04:00
|
|
|
let l:job_list = []
|
|
|
|
|
2016-10-24 04:58:45 -04:00
|
|
|
" Gather all of the jobs from every buffer.
|
2016-10-23 17:41:00 -04:00
|
|
|
for l:info in values(g:ale_buffer_info)
|
|
|
|
call extend(l:job_list, l:info.job_list)
|
2016-10-17 18:26:19 -04:00
|
|
|
endfor
|
|
|
|
|
|
|
|
let l:should_wait_more = 1
|
|
|
|
|
|
|
|
while l:should_wait_more
|
|
|
|
let l:should_wait_more = 0
|
|
|
|
|
|
|
|
for l:job in l:job_list
|
|
|
|
if job_status(l:job) ==# 'run'
|
2017-03-09 15:22:02 -05:00
|
|
|
let l:now = ale#util#ClockMilliseconds()
|
2016-10-17 18:43:31 -04:00
|
|
|
|
|
|
|
if l:now - l:start_time > a:deadline
|
2016-10-17 18:26:19 -04:00
|
|
|
" Stop waiting after a timeout, so we don't wait forever.
|
|
|
|
throw 'Jobs did not complete on time!'
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Wait another 10 milliseconds
|
|
|
|
let l:should_wait_more = 1
|
|
|
|
sleep 10ms
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endwhile
|
2016-10-17 18:43:31 -04:00
|
|
|
|
|
|
|
" Sleep for a small amount of time after all jobs finish.
|
|
|
|
" This seems to be enough to let handlers after jobs end run, and
|
|
|
|
" prevents the occasional failure where this function exits after jobs
|
|
|
|
" end, but before handlers are run.
|
|
|
|
sleep 10ms
|
2017-02-04 13:30:30 -05:00
|
|
|
|
|
|
|
" We must check the buffer data again to see if new jobs started
|
|
|
|
" for command_chain linters.
|
|
|
|
let l:has_new_jobs = 0
|
|
|
|
|
2017-03-21 10:52:02 -04:00
|
|
|
" Check again to see if any jobs are running.
|
2017-02-04 13:30:30 -05:00
|
|
|
for l:info in values(g:ale_buffer_info)
|
2017-03-21 10:52:02 -04:00
|
|
|
for l:job in l:info.job_list
|
|
|
|
if job_status(l:job) ==# 'run'
|
|
|
|
let l:has_new_jobs = 1
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
2017-02-04 13:30:30 -05:00
|
|
|
endfor
|
|
|
|
|
|
|
|
if l:has_new_jobs
|
|
|
|
" We have to wait more. Offset the timeout by the time taken so far.
|
2017-03-09 15:22:02 -05:00
|
|
|
let l:now = ale#util#ClockMilliseconds()
|
2017-02-04 13:30:30 -05:00
|
|
|
let l:new_deadline = a:deadline - (l:now - l:start_time)
|
|
|
|
|
|
|
|
if l:new_deadline <= 0
|
|
|
|
" Enough time passed already, so stop immediately.
|
|
|
|
throw 'Jobs did not complete on time!'
|
|
|
|
endif
|
|
|
|
|
|
|
|
call ale#engine#WaitForJobs(l:new_deadline)
|
|
|
|
endif
|
2016-10-17 18:26:19 -04:00
|
|
|
endfunction
|