2016-10-03 14:41:02 -04:00
|
|
|
" Author: w0rp <devw0rp@gmail.com>
|
|
|
|
" Description: Contains miscellaneous functions
|
|
|
|
|
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
|
|
|
" A null file for sending output to nothing.
|
|
|
|
let g:ale#util#nul_file = '/dev/null'
|
|
|
|
|
|
|
|
if has('win32')
|
|
|
|
let g:ale#util#nul_file = 'nul'
|
|
|
|
endif
|
|
|
|
|
2016-10-04 13:17:02 -04:00
|
|
|
" Return the number of lines for a given 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
|
|
|
function! ale#util#GetLineCount(buffer) abort
|
2016-10-04 13:17:02 -04:00
|
|
|
return len(getbufline(a:buffer, 1, '$'))
|
|
|
|
endfunction
|
2016-10-04 13:24:46 -04:00
|
|
|
|
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! ale#util#GetFunction(string_or_ref) abort
|
|
|
|
if type(a:string_or_ref) == type('')
|
|
|
|
return function(a:string_or_ref)
|
|
|
|
endif
|
2016-10-07 13:33:16 -04:00
|
|
|
|
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
|
|
|
return a:string_or_ref
|
|
|
|
endfunction
|
2016-10-13 15:56:18 -04:00
|
|
|
|
2017-08-19 15:15:36 -04:00
|
|
|
" Compare two loclist items for ALE, sorted by their buffers, filenames, and
|
|
|
|
" line numbers and column numbers.
|
2016-10-13 15:56:18 -04:00
|
|
|
function! ale#util#LocItemCompare(left, right) abort
|
2017-08-12 09:37:56 -04:00
|
|
|
if a:left.bufnr < a:right.bufnr
|
2016-10-13 15:56:18 -04:00
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
2017-08-12 09:37:56 -04:00
|
|
|
if a:left.bufnr > a:right.bufnr
|
2016-10-13 15:56:18 -04:00
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
2017-08-19 09:28:51 -04:00
|
|
|
if a:left.bufnr == -1
|
|
|
|
if a:left.filename < a:right.filename
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:left.filename > a:right.filename
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2017-08-12 09:37:56 -04:00
|
|
|
if a:left.lnum < a:right.lnum
|
2016-10-13 15:56:18 -04:00
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
2017-08-12 09:37:56 -04:00
|
|
|
if a:left.lnum > a:right.lnum
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:left.col < a:right.col
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:left.col > a:right.col
|
2016-10-13 15:56:18 -04:00
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
2016-10-13 16:24:47 -04:00
|
|
|
|
2017-08-19 15:15:36 -04:00
|
|
|
" Compare two loclist items, including the text for the items.
|
|
|
|
"
|
|
|
|
" This function can be used for de-duplicating lists.
|
|
|
|
function! ale#util#LocItemCompareWithText(left, right) abort
|
|
|
|
let l:cmp_value = ale#util#LocItemCompare(a:left, a:right)
|
|
|
|
|
|
|
|
if l:cmp_value
|
|
|
|
return l:cmp_value
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:left.text < a:right.text
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:left.text > a:right.text
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
2017-08-12 09:27:47 -04:00
|
|
|
" This function will perform a binary search and a small sequential search
|
|
|
|
" on the list to find the last problem in the buffer and line which is
|
|
|
|
" on or before the column. The index of the problem will be returned.
|
|
|
|
"
|
|
|
|
" -1 will be returned if nothing can be found.
|
|
|
|
function! ale#util#BinarySearch(loclist, buffer, line, column) abort
|
2016-10-13 16:24:47 -04:00
|
|
|
let l:min = 0
|
|
|
|
let l:max = len(a:loclist) - 1
|
|
|
|
|
|
|
|
while 1
|
|
|
|
if l:max < l:min
|
2017-08-12 09:27:47 -04:00
|
|
|
return -1
|
2016-10-13 16:24:47 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
let l:mid = (l:min + l:max) / 2
|
2017-08-12 09:27:47 -04:00
|
|
|
let l:item = a:loclist[l:mid]
|
2016-10-13 16:24:47 -04:00
|
|
|
|
2017-08-12 09:27:47 -04:00
|
|
|
" Binary search for equal buffers, equal lines, then near columns.
|
|
|
|
if l:item.bufnr < a:buffer
|
|
|
|
let l:min = l:mid + 1
|
|
|
|
elseif l:item.bufnr > a:buffer
|
|
|
|
let l:max = l:mid - 1
|
|
|
|
elseif l:item.lnum < a:line
|
2016-10-13 16:24:47 -04:00
|
|
|
let l:min = l:mid + 1
|
2017-08-12 09:27:47 -04:00
|
|
|
elseif l:item.lnum > a:line
|
2016-10-13 16:24:47 -04:00
|
|
|
let l:max = l:mid - 1
|
|
|
|
else
|
2017-08-12 09:27:47 -04:00
|
|
|
" This part is a small sequential search.
|
|
|
|
let l:index = l:mid
|
|
|
|
|
|
|
|
" Search backwards to find the first problem on the line.
|
|
|
|
while l:index > 0
|
|
|
|
\&& a:loclist[l:index - 1].bufnr == a:buffer
|
|
|
|
\&& a:loclist[l:index - 1].lnum == a:line
|
|
|
|
let l:index -= 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
" Find the last problem on or before this column.
|
|
|
|
while l:index < l:max
|
|
|
|
\&& a:loclist[l:index + 1].bufnr == a:buffer
|
|
|
|
\&& a:loclist[l:index + 1].lnum == a:line
|
|
|
|
\&& a:loclist[l:index + 1].col <= a:column
|
|
|
|
let l:index += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return l:index
|
2016-10-13 16:24:47 -04:00
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
endfunction
|
2017-02-14 16:02:49 -05:00
|
|
|
|
|
|
|
" A function for testing if a function is running inside a sandbox.
|
|
|
|
" See :help sandbox
|
|
|
|
function! ale#util#InSandbox() abort
|
|
|
|
try
|
2017-05-25 12:23:16 -04:00
|
|
|
function! s:SandboxCheck() abort
|
|
|
|
endfunction
|
2017-02-14 16:02:49 -05:00
|
|
|
catch /^Vim\%((\a\+)\)\=:E48/
|
|
|
|
" E48 is the sandbox error.
|
|
|
|
return 1
|
|
|
|
endtry
|
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
2017-03-09 15:22:02 -05:00
|
|
|
|
|
|
|
" Get the number of milliseconds since some vague, but consistent, point in
|
|
|
|
" the past.
|
|
|
|
"
|
|
|
|
" This function can be used for timing execution, etc.
|
|
|
|
"
|
|
|
|
" The time will be returned as a Number.
|
|
|
|
function! ale#util#ClockMilliseconds() abort
|
|
|
|
return float2nr(reltimefloat(reltime()) * 1000)
|
|
|
|
endfunction
|
2017-04-17 19:35:53 -04:00
|
|
|
|
|
|
|
" Given a single line, or a List of lines, and a single pattern, or a List
|
|
|
|
" of patterns, return all of the matches for the lines(s) from the given
|
|
|
|
" patterns, using matchlist().
|
|
|
|
"
|
|
|
|
" Only the first pattern which matches a line will be returned.
|
|
|
|
function! ale#util#GetMatches(lines, patterns) abort
|
|
|
|
let l:matches = []
|
|
|
|
let l:lines = type(a:lines) == type([]) ? a:lines : [a:lines]
|
|
|
|
let l:patterns = type(a:patterns) == type([]) ? a:patterns : [a:patterns]
|
|
|
|
|
|
|
|
for l:line in l:lines
|
|
|
|
for l:pattern in l:patterns
|
|
|
|
let l:match = matchlist(l:line, l:pattern)
|
|
|
|
|
|
|
|
if !empty(l:match)
|
|
|
|
call add(l:matches, l:match)
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:matches
|
|
|
|
endfunction
|
2017-06-06 17:27:20 -04:00
|
|
|
|
2017-06-07 09:02:29 -04:00
|
|
|
function! s:LoadArgCount(function) abort
|
|
|
|
let l:Function = a:function
|
2017-06-06 17:27:20 -04:00
|
|
|
|
|
|
|
redir => l:output
|
2017-06-07 04:26:54 -04:00
|
|
|
silent! function Function
|
2017-06-06 17:27:20 -04:00
|
|
|
redir END
|
|
|
|
|
2017-06-07 04:26:54 -04:00
|
|
|
if !exists('l:output')
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2017-06-06 17:27:20 -04:00
|
|
|
let l:match = matchstr(split(l:output, "\n")[0], '\v\([^)]+\)')[1:-2]
|
2017-08-10 19:31:42 -04:00
|
|
|
let l:arg_list = filter(split(l:match, ', '), 'v:val isnot# ''...''')
|
2017-06-06 17:27:20 -04:00
|
|
|
|
|
|
|
return len(l:arg_list)
|
|
|
|
endfunction
|
2017-06-07 09:02:29 -04:00
|
|
|
|
|
|
|
" Given the name of a function, a Funcref, or a lambda, return the number
|
|
|
|
" of named arguments for a function.
|
|
|
|
function! ale#util#FunctionArgCount(function) abort
|
|
|
|
let l:Function = ale#util#GetFunction(a:function)
|
|
|
|
let l:count = s:LoadArgCount(l:Function)
|
|
|
|
|
|
|
|
" If we failed to get the count, forcibly load the autoload file, if the
|
|
|
|
" function is an autoload function. autoload functions aren't normally
|
|
|
|
" defined until they are called.
|
|
|
|
if l:count == 0
|
|
|
|
let l:function_name = matchlist(string(l:Function), 'function([''"]\(.\+\)[''"])')[1]
|
|
|
|
|
|
|
|
if l:function_name =~# '#'
|
|
|
|
execute 'runtime autoload/' . join(split(l:function_name, '#')[:-2], '/') . '.vim'
|
|
|
|
let l:count = s:LoadArgCount(l:Function)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
return l:count
|
|
|
|
endfunction
|
2017-07-11 18:47:13 -04:00
|
|
|
|
|
|
|
" Escape a string so the characters in it will be safe for use inside of PCRE
|
|
|
|
" or RE2 regular expressions without characters having special meanings.
|
|
|
|
function! ale#util#EscapePCRE(unsafe_string) abort
|
|
|
|
return substitute(a:unsafe_string, '\([\-\[\]{}()*+?.^$|]\)', '\\\1', 'g')
|
|
|
|
endfunction
|
2017-07-26 19:06:15 -04:00
|
|
|
|
|
|
|
" Given a String or a List of String values, try and decode the string(s)
|
|
|
|
" as a JSON value which can be decoded with json_decode. If the JSON string
|
|
|
|
" is invalid, the default argument value will be returned instead.
|
|
|
|
"
|
|
|
|
" This function is useful in code where the data can't be trusted to be valid
|
|
|
|
" JSON, and where throwing exceptions is mostly just irritating.
|
|
|
|
function! ale#util#FuzzyJSONDecode(data, default) abort
|
|
|
|
if empty(a:data)
|
|
|
|
return a:default
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:str = type(a:data) == type('') ? a:data : join(a:data, '')
|
|
|
|
|
|
|
|
try
|
|
|
|
return json_decode(l:str)
|
|
|
|
catch /E474/
|
|
|
|
return a:default
|
|
|
|
endtry
|
|
|
|
endfunction
|
2017-08-05 15:17:25 -04:00
|
|
|
|
|
|
|
" Write a file, including carriage return characters for DOS files.
|
|
|
|
"
|
|
|
|
" The buffer number is required for determining the fileformat setting for
|
|
|
|
" the buffer.
|
|
|
|
function! ale#util#Writefile(buffer, lines, filename) abort
|
2017-08-08 03:39:13 -04:00
|
|
|
let l:corrected_lines = getbufvar(a:buffer, '&fileformat') is# 'dos'
|
2017-08-05 15:17:25 -04:00
|
|
|
\ ? map(copy(a:lines), 'v:val . "\r"')
|
|
|
|
\ : a:lines
|
|
|
|
|
|
|
|
call writefile(l:corrected_lines, a:filename) " no-custom-checks
|
|
|
|
endfunction
|