Handle line numbers beyond the end for any linter.

This commit is contained in:
w0rp 2016-10-04 18:17:02 +01:00
parent 8c1f0178ed
commit bd2f39f21a
3 changed files with 22 additions and 7 deletions

View File

@ -49,12 +49,6 @@ function! ale#handlers#HandleCSSLintFormat(buffer, lines)
" so you can actually read the error type.
let pattern = '^.*: line \(\d\+\), col \(\d\+\), \(Error\|Warning\) - \(.\+\) (\([^)]\+\))$'
let output = []
" 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.
"
" TODO: Find a faster way to compute this.
let last_line_number = len(getbufline(a:buffer, 1, '$'))
for line in a:lines
let l:match = matchlist(line, pattern)
@ -74,7 +68,7 @@ function! ale#handlers#HandleCSSLintFormat(buffer, lines)
" vcol is Needed to indicate that the column is a character.
call add(output, {
\ 'bufnr': a:buffer,
\ 'lnum': min([l:match[1] + 0, last_line_number]),
\ 'lnum': l:match[1] + 0,
\ 'vcol': 0,
\ 'col': l:match[2] + 0,
\ 'text': text,

View File

@ -19,3 +19,8 @@ function! s:FindWrapperScript()
endfunction
let g:ale#util#stdin_wrapper = s:FindWrapperScript()
" Return the number of lines for a given buffer.
function! ale#util#GetLineCount(buffer)
return len(getbufline(a:buffer, 1, '$'))
endfunction

View File

@ -93,6 +93,19 @@ function! s:LocItemCompare(left, right)
return 0
endfunction
function! s:FixLoclist(buffer, loclist)
" 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.
let last_line_number = ale#util#GetLineCount(a:buffer)
for item in a:loclist
if item.lnum > last_line_number
let item.lnum = last_line_number
endif
endfor
endfunction
function! s:HandleExit(job)
if !has_key(s:job_info_map, a:job)
return
@ -108,6 +121,9 @@ function! s:HandleExit(job)
let linter_loclist = s:GetFunction(linter.callback)(buffer, output)
" Make some adjustments to the loclists to fix common problems.
call s:FixLoclist(buffer, linter_loclist)
if g:ale_buffer_should_reset_map[buffer]
let g:ale_buffer_should_reset_map[buffer] = 0
let g:ale_buffer_loclist_map[buffer] = []