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
|
|
|
|
2018-05-28 14:19:20 -04:00
|
|
|
" Remapping of linter problems.
|
|
|
|
let g:ale_type_map = get(g:, 'ale_type_map', {})
|
|
|
|
|
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.
|
2017-05-21 11:16:06 -04:00
|
|
|
if !has_key(s:, 'job_info_map')
|
|
|
|
let s:job_info_map = {}
|
|
|
|
endif
|
|
|
|
|
2017-08-23 16:41:29 -04:00
|
|
|
if !has_key(s:, 'executable_cache_map')
|
|
|
|
let s:executable_cache_map = {}
|
|
|
|
endif
|
|
|
|
|
|
|
|
function! ale#engine#ResetExecutableCache() abort
|
|
|
|
let s:executable_cache_map = {}
|
|
|
|
endfunction
|
2017-04-29 06:58:43 -04:00
|
|
|
|
|
|
|
" Check if files are executable, and if they are, remember that they are
|
|
|
|
" for subsequent calls. We'll keep checking until programs can be executed.
|
2017-08-23 16:41:29 -04:00
|
|
|
function! ale#engine#IsExecutable(buffer, executable) abort
|
2017-11-21 09:37:01 -05:00
|
|
|
if empty(a:executable)
|
|
|
|
" Don't log the executable check if the executable string is empty.
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2017-11-30 05:34:51 -05:00
|
|
|
" Check for a cached executable() check.
|
|
|
|
let l:result = get(s:executable_cache_map, a:executable, v:null)
|
2017-04-29 06:58:43 -04:00
|
|
|
|
2017-11-30 05:34:51 -05:00
|
|
|
if l:result isnot v:null
|
|
|
|
return l:result
|
|
|
|
endif
|
2017-08-23 16:41:29 -04:00
|
|
|
|
2017-11-30 05:34:51 -05:00
|
|
|
" Check if the file is executable, and convert -1 to 1.
|
|
|
|
let l:result = executable(a:executable) isnot 0
|
2017-04-29 06:58:43 -04:00
|
|
|
|
2017-11-30 05:34:51 -05:00
|
|
|
" Cache the executable check if we found it, or if the option to cache
|
|
|
|
" failing checks is on.
|
2018-05-28 14:19:20 -04:00
|
|
|
if l:result || get(g:, 'ale_cache_executable_check_failures', 0)
|
2017-11-30 05:34:51 -05:00
|
|
|
let s:executable_cache_map[a:executable] = l:result
|
2017-08-23 16:41:29 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:ale_history_enabled
|
|
|
|
call ale#history#Add(a:buffer, l:result, 'executable', a:executable)
|
2017-04-29 06:58:43 -04:00
|
|
|
endif
|
|
|
|
|
2017-08-23 16:41:29 -04:00
|
|
|
return l:result
|
2017-04-29 06:58:43 -04:00
|
|
|
endfunction
|
2016-09-14 06:47:52 -04:00
|
|
|
|
2016-10-23 17:41:00 -04:00
|
|
|
function! ale#engine#InitBufferInfo(buffer) abort
|
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
2017-07-26 05:37:37 -04:00
|
|
|
" job_list will hold the list of job IDs
|
|
|
|
" active_linter_list will hold the list of active linter names
|
2016-10-31 09:45:22 -04:00
|
|
|
" loclist holds the loclist items after all jobs have completed.
|
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
|
2016-10-23 17:41:00 -04:00
|
|
|
let g:ale_buffer_info[a:buffer] = {
|
|
|
|
\ 'job_list': [],
|
2017-07-26 05:37:37 -04:00
|
|
|
\ 'active_linter_list': [],
|
2016-10-24 15:21:32 -04:00
|
|
|
\ 'loclist': [],
|
2017-02-11 10:16:08 -05:00
|
|
|
\ 'temporary_file_list': [],
|
|
|
|
\ 'temporary_directory_list': [],
|
2016-10-23 17:41:00 -04:00
|
|
|
\}
|
2017-08-19 15:15:36 -04:00
|
|
|
|
|
|
|
return 1
|
2016-10-23 17:41:00 -04:00
|
|
|
endif
|
2017-08-19 15:15:36 -04:00
|
|
|
|
|
|
|
return 0
|
2016-10-23 17:41:00 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-02-25 07:57:54 -05:00
|
|
|
" This function is documented and part of the public API.
|
|
|
|
"
|
2017-07-07 18:47:41 -04:00
|
|
|
" Return 1 if ALE is busy checking a given buffer
|
|
|
|
function! ale#engine#IsCheckingBuffer(buffer) abort
|
|
|
|
let l:info = get(g:ale_buffer_info, a:buffer, {})
|
|
|
|
|
2017-07-26 05:44:27 -04:00
|
|
|
return !empty(get(l:info, 'active_linter_list', []))
|
2017-07-07 18:47:41 -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
|
2018-05-28 12:38:14 -04:00
|
|
|
call ale#engine#InitBufferInfo(a:buffer)
|
2017-02-11 10:16:08 -05:00
|
|
|
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
|
2018-05-28 12:38:14 -04:00
|
|
|
call ale#engine#InitBufferInfo(a:buffer)
|
2017-02-11 10:16:08 -05:00
|
|
|
call add(g:ale_buffer_info[a:buffer].temporary_directory_list, a:directory)
|
|
|
|
endfunction
|
|
|
|
|
2018-07-15 13:24:53 -04:00
|
|
|
function! ale#engine#CreateFile(buffer) abort
|
|
|
|
" This variable can be set to 1 in tests to stub this out.
|
|
|
|
if get(g:, 'ale_create_dummy_temporary_file')
|
|
|
|
return 'TEMP'
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:temporary_file = ale#util#Tempname()
|
|
|
|
call ale#engine#ManageFile(a:buffer, l:temporary_file)
|
|
|
|
|
|
|
|
return l:temporary_file
|
|
|
|
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
|
2018-07-15 13:24:53 -04:00
|
|
|
" This variable can be set to 1 in tests to stub this out.
|
|
|
|
if get(g:, 'ale_create_dummy_temporary_file')
|
|
|
|
return 'TEMP_DIR'
|
|
|
|
endif
|
|
|
|
|
2018-07-12 08:05:59 -04:00
|
|
|
let l:temporary_directory = ale#util#Tempname()
|
2017-03-31 15:14:53 -04:00
|
|
|
" 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
|
2017-08-07 19:46:42 -04:00
|
|
|
let l:info = get(g:ale_buffer_info, a:buffer, {})
|
2017-02-11 10:16:08 -05:00
|
|
|
|
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.
|
2017-07-07 18:47:41 -04:00
|
|
|
if has_key(l:info, 'temporary_file_list')
|
|
|
|
for l:filename in l:info.temporary_file_list
|
|
|
|
call delete(l:filename)
|
|
|
|
endfor
|
2017-02-11 10:16:08 -05:00
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
let l:info.temporary_file_list = []
|
|
|
|
endif
|
2017-02-11 10:16:08 -05:00
|
|
|
|
|
|
|
" 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.
|
2017-07-07 18:47:41 -04:00
|
|
|
if has_key(l:info, 'temporary_directory_list')
|
|
|
|
for l:directory in l:info.temporary_directory_list
|
|
|
|
call delete(l:directory, 'rf')
|
|
|
|
endfor
|
2017-02-11 10:16:08 -05:00
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
let l:info.temporary_directory_list = []
|
|
|
|
endif
|
2017-02-11 10:16:08 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
function! s:GatherOutput(job_id, line) abort
|
|
|
|
if has_key(s:job_info_map, a:job_id)
|
|
|
|
call add(s:job_info_map[a:job_id].output, a:line)
|
2016-10-08 16:04:42 -04:00
|
|
|
endif
|
2017-05-12 16:16:15 -04:00
|
|
|
endfunction
|
2016-10-08 16:04:42 -04:00
|
|
|
|
2018-03-06 05:23:49 -05:00
|
|
|
function! ale#engine#HandleLoclist(linter_name, buffer, loclist) abort
|
2018-03-02 07:10:27 -05:00
|
|
|
let l:info = get(g:ale_buffer_info, a:buffer, {})
|
2017-07-26 05:37:37 -04:00
|
|
|
|
2018-03-02 07:10:27 -05:00
|
|
|
if empty(l:info)
|
2017-07-26 05:37:37 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Remove this linter from the list of active linters.
|
|
|
|
" This may have already been done when the job exits.
|
2018-03-02 07:10:27 -05:00
|
|
|
call filter(l:info.active_linter_list, 'v:val isnot# a:linter_name')
|
2017-07-26 05:37:37 -04:00
|
|
|
|
2017-06-08 12:28:38 -04:00
|
|
|
" Make some adjustments to the loclists to fix common problems, and also
|
|
|
|
" to set default values for loclist items.
|
2017-06-13 12:53:47 -04:00
|
|
|
let l:linter_loclist = ale#engine#FixLocList(a:buffer, a:linter_name, a:loclist)
|
2017-06-08 12:28:38 -04:00
|
|
|
|
|
|
|
" Remove previous items for this linter.
|
2018-03-02 07:10:27 -05:00
|
|
|
call filter(l:info.loclist, 'v:val.linter_name isnot# a:linter_name')
|
|
|
|
|
|
|
|
" We don't need to add items or sort the list when this list is empty.
|
|
|
|
if !empty(l:linter_loclist)
|
|
|
|
" Add the new items.
|
|
|
|
call extend(l:info.loclist, l:linter_loclist)
|
2017-06-08 12:28:38 -04:00
|
|
|
|
2018-03-02 07:10:27 -05: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.
|
|
|
|
call sort(l:info.loclist, 'ale#util#LocItemCompare')
|
|
|
|
endif
|
2017-06-08 12:28:38 -04:00
|
|
|
|
2017-07-31 19:03:24 -04:00
|
|
|
if ale#ShouldDoNothing(a:buffer)
|
2017-07-07 18:47:41 -04:00
|
|
|
return
|
2017-06-08 12:28:38 -04:00
|
|
|
endif
|
|
|
|
|
2018-03-02 07:10:27 -05:00
|
|
|
call ale#engine#SetResults(a:buffer, l:info.loclist)
|
2017-06-08 12:28:38 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
function! s:HandleExit(job_id, exit_code) abort
|
|
|
|
if !has_key(s:job_info_map, a:job_id)
|
2016-09-08 19:23:26 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_info = s:job_info_map[a:job_id]
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:linter = l:job_info.linter
|
|
|
|
let l:output = l:job_info.output
|
|
|
|
let l:buffer = l:job_info.buffer
|
2018-07-05 15:56:31 -04:00
|
|
|
let l:executable = l:job_info.executable
|
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
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
if g:ale_history_enabled
|
|
|
|
call ale#history#SetExitCode(l:buffer, a:job_id, a:exit_code)
|
|
|
|
endif
|
|
|
|
|
2017-06-06 11:44:01 -04:00
|
|
|
" Remove this job from the list.
|
|
|
|
call ale#job#Stop(a:job_id)
|
|
|
|
call remove(s:job_info_map, a:job_id)
|
2017-08-10 19:31:42 -04:00
|
|
|
call filter(g:ale_buffer_info[l:buffer].job_list, 'v:val isnot# a:job_id')
|
|
|
|
call filter(g:ale_buffer_info[l:buffer].active_linter_list, 'v:val isnot# l:linter.name')
|
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
|
|
|
|
|
2017-05-12 15:38:52 -04:00
|
|
|
if has('nvim') && !empty(l:output) && empty(l:output[-1])
|
|
|
|
call remove(l:output, -1)
|
|
|
|
endif
|
|
|
|
|
2016-10-25 15:25:23 -04:00
|
|
|
if l:next_chain_index < len(get(l:linter, 'command_chain', []))
|
2018-07-05 15:56:31 -04:00
|
|
|
call s:InvokeChain(l:buffer, l:executable, l:linter, l:next_chain_index, l:output)
|
2018-09-04 11:51:18 -04:00
|
|
|
|
2016-10-25 15:25:23 -04:00
|
|
|
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
|
2017-05-12 16:16:15 -04:00
|
|
|
call ale#history#RememberOutput(l:buffer, a:job_id, l:output[:])
|
2017-02-16 18:18:57 -05:00
|
|
|
endif
|
|
|
|
|
2018-07-13 04:47:26 -04:00
|
|
|
try
|
|
|
|
let l:loclist = ale#util#GetFunction(l:linter.callback)(l:buffer, l:output)
|
|
|
|
" Handle the function being unknown, or being deleted.
|
|
|
|
catch /E700/
|
|
|
|
let l:loclist = []
|
|
|
|
endtry
|
2016-10-10 14:56:05 -04:00
|
|
|
|
2018-03-06 05:23:49 -05:00
|
|
|
call ale#engine#HandleLoclist(l:linter.name, l:buffer, l:loclist)
|
2017-06-08 12:28:38 -04:00
|
|
|
endfunction
|
2017-03-21 10:52:02 -04:00
|
|
|
|
2017-02-09 13:47:14 -05:00
|
|
|
function! ale#engine#SetResults(buffer, loclist) abort
|
2017-07-07 18:47:41 -04:00
|
|
|
let l:linting_is_done = !ale#engine#IsCheckingBuffer(a:buffer)
|
2017-06-07 18:12:45 -04:00
|
|
|
|
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
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
if l:linting_is_done
|
2017-12-18 06:14:10 -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
|
|
|
|
|
2017-08-07 19:46:42 -04:00
|
|
|
" Reset the save event marker, used for opening windows, etc.
|
|
|
|
call setbufvar(a:buffer, 'ale_save_event_fired', 0)
|
2018-03-03 11:22:56 -05:00
|
|
|
" Set a marker showing how many times a buffer has been checked.
|
|
|
|
call setbufvar(
|
|
|
|
\ a:buffer,
|
|
|
|
\ 'ale_linted',
|
|
|
|
\ getbufvar(a:buffer, 'ale_linted', 0) + 1
|
|
|
|
\)
|
2017-08-07 19:46:42 -04:00
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
" Automatically remove all managed temporary files and directories
|
|
|
|
" now that all jobs have completed.
|
|
|
|
call ale#engine#RemoveManagedFiles(a:buffer)
|
|
|
|
|
|
|
|
" Call user autocommands. This allows users to hook into ALE's lint cycle.
|
2017-12-10 08:03:03 -05:00
|
|
|
silent doautocmd <nomodeline> User ALELintPost
|
2017-07-07 18:47:41 -04:00
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-06-14 12:59:13 -04:00
|
|
|
function! s:RemapItemTypes(type_map, loclist) abort
|
|
|
|
for l:item in a:loclist
|
|
|
|
let l:key = l:item.type
|
2017-08-08 03:39:13 -04:00
|
|
|
\ . (get(l:item, 'sub_type', '') is# 'style' ? 'S' : '')
|
2017-06-14 12:59:13 -04:00
|
|
|
let l:new_key = get(a:type_map, l:key, '')
|
|
|
|
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:new_key is# 'E'
|
|
|
|
\|| l:new_key is# 'ES'
|
|
|
|
\|| l:new_key is# 'W'
|
|
|
|
\|| l:new_key is# 'WS'
|
|
|
|
\|| l:new_key is# 'I'
|
2017-06-14 12:59:13 -04:00
|
|
|
let l:item.type = l:new_key[0]
|
|
|
|
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:new_key is# 'ES' || l:new_key is# 'WS'
|
2017-06-14 12:59:13 -04:00
|
|
|
let l:item.sub_type = 'style'
|
|
|
|
elseif has_key(l:item, 'sub_type')
|
|
|
|
call remove(l:item, 'sub_type')
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2017-06-13 12:53:47 -04:00
|
|
|
function! ale#engine#FixLocList(buffer, linter_name, loclist) abort
|
2017-08-10 18:08:32 -04:00
|
|
|
let l:bufnr_map = {}
|
2017-02-26 09:51:22 -05:00
|
|
|
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 = {
|
2017-08-19 19:05:15 -04:00
|
|
|
\ 'bufnr': a:buffer,
|
2017-02-26 09:51:22 -05:00
|
|
|
\ 'text': l:old_item.text,
|
|
|
|
\ 'lnum': str2nr(l:old_item.lnum),
|
|
|
|
\ 'col': str2nr(get(l:old_item, 'col', 0)),
|
2018-04-08 12:17:30 -04:00
|
|
|
\ 'vcol': 0,
|
2017-02-26 09:51:22 -05:00
|
|
|
\ 'type': get(l:old_item, 'type', 'E'),
|
|
|
|
\ 'nr': get(l:old_item, 'nr', -1),
|
2017-06-13 12:53:47 -04:00
|
|
|
\ 'linter_name': a:linter_name,
|
2017-02-26 09:51:22 -05:00
|
|
|
\}
|
|
|
|
|
2017-11-13 18:34:00 -05:00
|
|
|
if has_key(l:old_item, 'code')
|
|
|
|
let l:item.code = l:old_item.code
|
|
|
|
endif
|
|
|
|
|
2017-08-10 18:08:32 -04:00
|
|
|
if has_key(l:old_item, 'filename')
|
2017-09-13 19:11:17 -04:00
|
|
|
\&& !ale#path#IsTempName(l:old_item.filename)
|
2017-08-10 18:08:32 -04:00
|
|
|
" Use the filename given.
|
2017-08-19 19:05:15 -04:00
|
|
|
" Temporary files are assumed to be for this buffer,
|
|
|
|
" and the filename is not included then, because it looks bad
|
|
|
|
" in the loclist window.
|
2017-08-10 18:08:32 -04:00
|
|
|
let l:filename = l:old_item.filename
|
|
|
|
let l:item.filename = l:filename
|
|
|
|
|
|
|
|
if has_key(l:old_item, 'bufnr')
|
|
|
|
" If a buffer number is also given, include that too.
|
|
|
|
" If Vim detects that he buffer number is valid, it will
|
|
|
|
" be used instead of the filename.
|
|
|
|
let l:item.bufnr = l:old_item.bufnr
|
|
|
|
elseif has_key(l:bufnr_map, l:filename)
|
|
|
|
" Get the buffer number from the map, which can be faster.
|
|
|
|
let l:item.bufnr = l:bufnr_map[l:filename]
|
|
|
|
else
|
|
|
|
" Look up the buffer number.
|
|
|
|
let l:item.bufnr = bufnr(l:filename)
|
|
|
|
let l:bufnr_map[l:filename] = l:item.bufnr
|
|
|
|
endif
|
|
|
|
elseif has_key(l:old_item, 'bufnr')
|
|
|
|
let l:item.bufnr = l:old_item.bufnr
|
|
|
|
endif
|
|
|
|
|
2017-03-02 02:14:30 -05:00
|
|
|
if has_key(l:old_item, 'detail')
|
|
|
|
let l:item.detail = l:old_item.detail
|
|
|
|
endif
|
|
|
|
|
2017-05-31 08:14:39 -04:00
|
|
|
" Pass on a end_col key if set, used for highlights.
|
2017-05-16 13:12:49 -04:00
|
|
|
if has_key(l:old_item, 'end_col')
|
|
|
|
let l:item.end_col = str2nr(l:old_item.end_col)
|
|
|
|
endif
|
|
|
|
|
2017-05-31 08:14:39 -04:00
|
|
|
if has_key(l:old_item, 'end_lnum')
|
|
|
|
let l:item.end_lnum = str2nr(l:old_item.end_lnum)
|
|
|
|
endif
|
|
|
|
|
2017-05-20 18:32:41 -04:00
|
|
|
if has_key(l:old_item, 'sub_type')
|
|
|
|
let l:item.sub_type = l:old_item.sub_type
|
|
|
|
endif
|
|
|
|
|
2017-06-24 07:35:01 -04:00
|
|
|
if l:item.lnum < 1
|
|
|
|
" When errors appear before line 1, put them at line 1.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:item.lnum = 1
|
2017-08-19 15:15:36 -04:00
|
|
|
elseif l:item.bufnr == a:buffer && 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.
|
2017-08-19 15:15:36 -04:00
|
|
|
" This is only done for the current buffer.
|
2016-10-10 19:00:09 -04:00
|
|
|
let l:item.lnum = l:last_line_number
|
2018-04-08 12:17:30 -04:00
|
|
|
elseif get(l:old_item, 'vcol', 0)
|
|
|
|
" Convert virtual column positions to byte positions.
|
|
|
|
" The positions will be off if the buffer has changed recently.
|
|
|
|
let l:line = getbufline(a:buffer, l:item.lnum)[0]
|
|
|
|
|
|
|
|
let l:item.col = ale#util#Col(l:line, l:item.col)
|
|
|
|
|
|
|
|
if has_key(l:item, 'end_col')
|
|
|
|
let l:end_line = get(l:item, 'end_lnum', l:line) != l:line
|
|
|
|
\ ? getbufline(a:buffer, l:item.end_lnum)[0]
|
|
|
|
\ : l:line
|
|
|
|
|
|
|
|
let l:item.end_col = ale#util#Col(l:end_line, l:item.end_col)
|
|
|
|
endif
|
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
|
|
|
|
2017-06-14 12:59:13 -04:00
|
|
|
let l:type_map = get(ale#Var(a:buffer, 'type_map'), a:linter_name, {})
|
|
|
|
|
|
|
|
if !empty(l:type_map)
|
|
|
|
call s:RemapItemTypes(l:type_map, l:new_loclist)
|
|
|
|
endif
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-08-05 15:17:25 -04:00
|
|
|
let l:lines = getbufline(a:buffer, 1, '$')
|
|
|
|
call ale#util#Writefile(a:buffer, l:lines, a:temporary_file)
|
2017-02-11 13:14:18 -05:00
|
|
|
|
|
|
|
return 1
|
|
|
|
endfunction
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
" Run a job.
|
|
|
|
"
|
|
|
|
" Returns 1 when the job was started successfully.
|
2017-02-09 18:32:57 -05:00
|
|
|
function! s:RunJob(options) abort
|
|
|
|
let l:command = a:options.command
|
2018-07-05 15:56:31 -04:00
|
|
|
|
|
|
|
if empty(l:command)
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:executable = a:options.executable
|
2017-02-09 18:32:57 -05:00
|
|
|
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
|
2017-08-14 05:00:46 -04:00
|
|
|
let l:info = g:ale_buffer_info[l:buffer]
|
2016-09-11 11:49:55 -04:00
|
|
|
|
2018-07-05 15:56:31 -04:00
|
|
|
let [l:temporary_file, l:command] = ale#command#FormatCommand(
|
|
|
|
\ l:buffer,
|
|
|
|
\ l:executable,
|
|
|
|
\ l:command,
|
|
|
|
\ l:read_buffer,
|
|
|
|
\)
|
2017-02-11 17:43:13 -05:00
|
|
|
|
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-07-07 05:47:09 -04:00
|
|
|
" Add a newline to commands which need it.
|
|
|
|
" This is only used for Flow for now, and is not documented.
|
|
|
|
if l:linter.add_newline
|
|
|
|
if has('win32')
|
|
|
|
let l:command = l:command . '; echo.'
|
|
|
|
else
|
|
|
|
let l:command = l:command . '; echo'
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2017-12-20 07:20:38 -05:00
|
|
|
let l:command = ale#job#PrepareCommand(l:buffer, l:command)
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_options = {
|
|
|
|
\ 'mode': 'nl',
|
|
|
|
\ 'exit_cb': function('s:HandleExit'),
|
|
|
|
\}
|
|
|
|
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:output_stream is# 'stderr'
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_options.err_cb = function('s:GatherOutput')
|
2017-08-08 03:39:13 -04:00
|
|
|
elseif l:output_stream is# 'both'
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_options.out_cb = function('s:GatherOutput')
|
|
|
|
let l:job_options.err_cb = function('s:GatherOutput')
|
|
|
|
else
|
|
|
|
let l:job_options.out_cb = function('s:GatherOutput')
|
2017-04-03 17:24:30 -04:00
|
|
|
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.
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_id = len(s:job_info_map) + 1
|
2017-04-03 17:24:30 -04:00
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
while has_key(s:job_info_map, l:job_id)
|
|
|
|
let l:job_id += 1
|
2017-04-03 17:24:30 -04:00
|
|
|
endwhile
|
2016-09-09 20:37:40 -04:00
|
|
|
else
|
2017-05-12 16:16:15 -04:00
|
|
|
let l:job_id = ale#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'
|
|
|
|
|
2016-10-08 16:52:41 -04:00
|
|
|
" Only proceed if the job is being run.
|
2017-05-12 16:16:15 -04:00
|
|
|
if l:job_id
|
2016-10-23 17:41:00 -04:00
|
|
|
" Add the job to the list of jobs, so we can track them.
|
2017-08-14 05:00:46 -04:00
|
|
|
call add(l:info.job_list, l:job_id)
|
|
|
|
|
|
|
|
if index(l:info.active_linter_list, l:linter.name) < 0
|
|
|
|
call add(l:info.active_linter_list, l:linter.name)
|
|
|
|
endif
|
2016-09-08 19:23:26 -04:00
|
|
|
|
2017-02-16 16:18:03 -05:00
|
|
|
let l:status = 'started'
|
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,
|
2018-07-05 15:56:31 -04:00
|
|
|
\ 'executable': l:executable,
|
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
|
|
|
\}
|
2018-04-27 18:40:02 -04:00
|
|
|
|
|
|
|
silent doautocmd <nomodeline> User ALEJobStarted
|
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)
|
|
|
|
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(
|
2018-07-24 20:27:28 -04:00
|
|
|
\ type(l:command) is v:t_list
|
2017-05-12 04:20:16 -04:00
|
|
|
\ ? join(l:command[0:1]) . ' ' . ale#Escape(l:command[2])
|
2017-04-03 17:24:30 -04:00
|
|
|
\ : l:command
|
|
|
|
\)
|
2017-05-12 16:16:15 -04:00
|
|
|
|
2017-05-18 08:21:14 -04:00
|
|
|
call l:job_options.exit_cb(l:job_id, v:shell_error)
|
2017-04-03 17:24:30 -04:00
|
|
|
endif
|
2017-07-07 18:47:41 -04:00
|
|
|
|
|
|
|
return l:job_id != 0
|
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)
|
2017-10-02 12:11:54 -04:00
|
|
|
" Run a chain of commands, one asynchronous command after the other,
|
2017-02-04 13:30:30 -05:00
|
|
|
" 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
|
|
|
|
2018-09-04 11:51:18 -04:00
|
|
|
" If we have a command to run, execute that.
|
2017-02-04 13:30:30 -05:00
|
|
|
if !empty(l:command)
|
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
|
|
|
else
|
2017-07-02 08:17:24 -04:00
|
|
|
let l:command = ale#linter#GetCommand(a:buffer, a:linter)
|
2016-10-25 15:25:23 -04:00
|
|
|
endif
|
|
|
|
|
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
|
|
|
|
|
2018-07-05 15:56:31 -04:00
|
|
|
function! s:InvokeChain(buffer, executable, linter, chain_index, input) abort
|
2017-02-09 18:32:57 -05:00
|
|
|
let l:options = ale#engine#ProcessChain(a:buffer, a:linter, a:chain_index, a:input)
|
2018-07-05 15:56:31 -04:00
|
|
|
let l:options.executable = a:executable
|
2017-02-09 18:32:57 -05:00
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
return s:RunJob(l:options)
|
2016-10-25 15:25:23 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
function! s:StopCurrentJobs(buffer, include_lint_file_jobs) abort
|
2017-06-06 11:44:01 -04:00
|
|
|
let l:info = get(g:ale_buffer_info, a:buffer, {})
|
|
|
|
let l:new_job_list = []
|
2017-08-14 05:00:46 -04:00
|
|
|
let l:new_active_linter_list = []
|
2017-06-06 11:44:01 -04:00
|
|
|
|
|
|
|
for l:job_id in get(l:info, 'job_list', [])
|
|
|
|
let l:job_info = get(s:job_info_map, l:job_id, {})
|
2017-03-14 19:51:57 -04:00
|
|
|
|
2017-06-06 11:44:01 -04:00
|
|
|
if !empty(l:job_info)
|
|
|
|
if a:include_lint_file_jobs || !l:job_info.linter.lint_file
|
|
|
|
call ale#job#Stop(l:job_id)
|
|
|
|
call remove(s:job_info_map, l:job_id)
|
|
|
|
else
|
|
|
|
call add(l:new_job_list, l:job_id)
|
2017-08-14 05:00:46 -04:00
|
|
|
" Linters with jobs still running are still active.
|
|
|
|
call add(l:new_active_linter_list, l:job_info.linter.name)
|
2017-06-06 11:44:01 -04:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2017-08-14 05:00:46 -04:00
|
|
|
" Remove duplicates from the active linter list.
|
|
|
|
call uniq(sort(l:new_active_linter_list))
|
|
|
|
|
2017-06-06 11:44:01 -04:00
|
|
|
" Update the List, so it includes only the jobs we still need.
|
|
|
|
let l:info.job_list = l:new_job_list
|
2017-08-14 05:00:46 -04:00
|
|
|
" Update the active linter list, clearing out anything not running.
|
|
|
|
let l:info.active_linter_list = l:new_active_linter_list
|
2017-06-06 11:44:01 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
|
|
|
|
function! s:RemoveProblemsForDisabledLinters(buffer, linters) abort
|
|
|
|
" Figure out which linters are still enabled, and remove
|
|
|
|
" problems for linters which are no longer enabled.
|
|
|
|
let l:name_map = {}
|
|
|
|
|
|
|
|
for l:linter in a:linters
|
|
|
|
let l:name_map[l:linter.name] = 1
|
|
|
|
endfor
|
|
|
|
|
|
|
|
call filter(
|
|
|
|
\ get(g:ale_buffer_info[a:buffer], 'loclist', []),
|
2017-08-22 16:19:36 -04:00
|
|
|
\ 'get(l:name_map, get(v:val, ''linter_name''))',
|
2017-07-07 18:47:41 -04:00
|
|
|
\)
|
2017-06-08 12:28:38 -04:00
|
|
|
endfunction
|
2017-03-14 19:51:57 -04:00
|
|
|
|
2017-08-19 15:15:36 -04:00
|
|
|
function! s:AddProblemsFromOtherBuffers(buffer, linters) abort
|
|
|
|
let l:filename = expand('#' . a:buffer . ':p')
|
|
|
|
let l:loclist = []
|
|
|
|
let l:name_map = {}
|
|
|
|
|
|
|
|
" Build a map of the active linters.
|
|
|
|
for l:linter in a:linters
|
|
|
|
let l:name_map[l:linter.name] = 1
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" Find the items from other buffers, for the linters that are enabled.
|
|
|
|
for l:info in values(g:ale_buffer_info)
|
|
|
|
for l:item in l:info.loclist
|
|
|
|
if has_key(l:item, 'filename')
|
|
|
|
\&& l:item.filename is# l:filename
|
|
|
|
\&& has_key(l:name_map, l:item.linter_name)
|
2017-08-22 17:45:55 -04:00
|
|
|
" Copy the items and set the buffer numbers to this one.
|
|
|
|
let l:new_item = copy(l:item)
|
|
|
|
let l:new_item.bufnr = a:buffer
|
|
|
|
call add(l:loclist, l:new_item)
|
2017-08-19 15:15:36 -04:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if !empty(l:loclist)
|
|
|
|
call sort(l:loclist, function('ale#util#LocItemCompareWithText'))
|
|
|
|
call uniq(l:loclist, function('ale#util#LocItemCompareWithText'))
|
|
|
|
|
2017-08-22 17:45:55 -04:00
|
|
|
" Set the loclist variable, used by some parts of ALE.
|
|
|
|
let g:ale_buffer_info[a:buffer].loclist = l:loclist
|
2017-08-19 15:15:36 -04:00
|
|
|
call ale#engine#SetResults(a:buffer, l:loclist)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
" Run a linter for a buffer.
|
|
|
|
"
|
|
|
|
" Returns 1 if the linter was successfully run.
|
|
|
|
function! s:RunLinter(buffer, linter) abort
|
2017-07-26 05:44:07 -04:00
|
|
|
if !empty(a:linter.lsp)
|
2018-06-15 04:53:13 -04:00
|
|
|
return ale#lsp_linter#CheckWithLSP(a:buffer, a:linter)
|
2017-07-26 05:37:37 -04:00
|
|
|
else
|
2017-07-02 08:17:24 -04:00
|
|
|
let l:executable = ale#linter#GetExecutable(a:buffer, a:linter)
|
2017-06-08 12:28:38 -04:00
|
|
|
|
2017-08-23 16:41:29 -04:00
|
|
|
if ale#engine#IsExecutable(a:buffer, l:executable)
|
2018-07-05 15:56:31 -04:00
|
|
|
return s:InvokeChain(a:buffer, l:executable, a:linter, 0, [])
|
2017-07-07 18:47:41 -04:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#engine#RunLinters(buffer, linters, should_lint_file) abort
|
|
|
|
" Initialise the buffer information if needed.
|
2017-08-19 15:15:36 -04:00
|
|
|
let l:new_buffer = ale#engine#InitBufferInfo(a:buffer)
|
2017-07-07 18:47:41 -04:00
|
|
|
call s:StopCurrentJobs(a:buffer, a:should_lint_file)
|
|
|
|
call s:RemoveProblemsForDisabledLinters(a:buffer, a:linters)
|
|
|
|
|
2017-07-31 17:36:30 -04:00
|
|
|
" We can only clear the results if we aren't checking the buffer.
|
|
|
|
let l:can_clear_results = !ale#engine#IsCheckingBuffer(a:buffer)
|
2017-07-07 18:47:41 -04:00
|
|
|
|
2017-12-10 08:03:03 -05:00
|
|
|
silent doautocmd <nomodeline> User ALELintPre
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
for l:linter in a:linters
|
2017-07-31 17:36:30 -04:00
|
|
|
" Only run lint_file linters if we should.
|
|
|
|
if !l:linter.lint_file || a:should_lint_file
|
|
|
|
if s:RunLinter(a:buffer, l:linter)
|
|
|
|
" If a single linter ran, we shouldn't clear everything.
|
|
|
|
let l:can_clear_results = 0
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
" If we skipped running a lint_file linter still in the list,
|
|
|
|
" we shouldn't clear everything.
|
|
|
|
let l:can_clear_results = 0
|
2017-06-08 12:28:38 -04:00
|
|
|
endif
|
2017-07-07 18:47:41 -04:00
|
|
|
endfor
|
|
|
|
|
2017-07-31 17:36:30 -04:00
|
|
|
" Clear the results if we can. This needs to be done when linters are
|
|
|
|
" disabled, or ALE itself is disabled.
|
|
|
|
if l:can_clear_results
|
2017-07-07 18:47:41 -04:00
|
|
|
call ale#engine#SetResults(a:buffer, [])
|
2017-08-19 15:15:36 -04:00
|
|
|
elseif l:new_buffer
|
|
|
|
call s:AddProblemsFromOtherBuffers(a:buffer, a:linters)
|
2017-07-07 18:47:41 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Clean up a buffer.
|
|
|
|
"
|
|
|
|
" This function will stop all current jobs for the buffer,
|
|
|
|
" clear the state of everything, and remove the Dictionary for managing
|
|
|
|
" the buffer.
|
|
|
|
function! ale#engine#Cleanup(buffer) abort
|
2017-10-14 14:22:19 -04:00
|
|
|
" Don't bother with cleanup code when newer NeoVim versions are exiting.
|
|
|
|
if get(v:, 'exiting', v:null) isnot v:null
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-07-15 13:44:45 -04:00
|
|
|
if !has_key(g:ale_buffer_info, a:buffer)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-07-07 18:47:41 -04:00
|
|
|
call ale#engine#RunLinters(a:buffer, [], 1)
|
|
|
|
|
|
|
|
call remove(g:ale_buffer_info, a:buffer)
|
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
|
2018-06-19 15:53:49 -04:00
|
|
|
let l:start_time = ale#events#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)
|
2017-10-23 18:09:40 -04:00
|
|
|
call extend(l:job_list, get(l:info, 'job_list', []))
|
2016-10-17 18:26:19 -04:00
|
|
|
endfor
|
|
|
|
|
2017-05-12 15:38:52 -04:00
|
|
|
" NeoVim has a built-in API for this, so use that.
|
|
|
|
if has('nvim')
|
|
|
|
let l:nvim_code_list = jobwait(l:job_list, a:deadline)
|
|
|
|
|
|
|
|
if index(l:nvim_code_list, -1) >= 0
|
|
|
|
throw 'Jobs did not complete on time!'
|
|
|
|
endif
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-10-17 18:26:19 -04:00
|
|
|
let l:should_wait_more = 1
|
|
|
|
|
|
|
|
while l:should_wait_more
|
|
|
|
let l:should_wait_more = 0
|
|
|
|
|
2017-05-12 16:16:15 -04:00
|
|
|
for l:job_id in l:job_list
|
|
|
|
if ale#job#IsRunning(l:job_id)
|
2018-06-19 15:53:49 -04:00
|
|
|
let l:now = ale#events#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-10-23 18:09:40 -04:00
|
|
|
for l:job_id in get(l:info, 'job_list', [])
|
2017-05-12 16:16:15 -04:00
|
|
|
if ale#job#IsRunning(l:job_id)
|
2017-03-21 10:52:02 -04:00
|
|
|
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.
|
2018-06-19 15:53:49 -04:00
|
|
|
let l:now = ale#events#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
|