ALECodeAction fix: check linter name when searching for nearest error. (#3901)

It is easier to explain this fix with an example:

* tsserver and LSPs ask for error information when you want to fix
  error. tsserver `ts@getCodeFixes` command needs tsserver error code.

* now let's imagine that user has eslint and tsserver in use. Sometimes
  both can report same error in different way.

* Now there is no guarantee which error will come first and if eslint
  error comes first then tsserver will not return code fixes as we are
  passing wrong error code to it.

This fix will return proper error code based on linter.
This commit is contained in:
Dalius Dobravolskas 2021-09-16 16:05:38 +03:00 committed by GitHub
parent 31349f208b
commit dca56dd772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -299,7 +299,7 @@ function! ale#codefix#HandleLSPResponse(conn_id, response) abort
endif
endfunction
function! s:FindError(buffer, line, column, end_line, end_column) abort
function! s:FindError(buffer, line, column, end_line, end_column, linter_name) abort
let l:nearest_error = v:null
if a:line == a:end_line
@ -308,7 +308,9 @@ function! s:FindError(buffer, line, column, end_line, end_column) abort
let l:nearest_error_diff = -1
for l:error in get(g:ale_buffer_info[a:buffer], 'loclist', [])
if has_key(l:error, 'code') && l:error.lnum == a:line
if has_key(l:error, 'code')
\ && (a:linter_name is v:null || l:error.linter_name is# a:linter_name)
\ && l:error.lnum == a:line
let l:diff = abs(l:error.col - a:column)
if l:nearest_error_diff == -1 || l:diff < l:nearest_error_diff
@ -341,7 +343,7 @@ function! s:OnReady(
if a:linter.lsp is# 'tsserver'
let l:nearest_error =
\ s:FindError(l:buffer, a:line, a:column, a:end_line, a:end_column)
\ s:FindError(l:buffer, a:line, a:column, a:end_line, a:end_column, a:linter.lsp)
if l:nearest_error isnot v:null
let l:message = ale#lsp#tsserver_message#GetCodeFixes(
@ -368,7 +370,7 @@ function! s:OnReady(
let l:diagnostics = []
let l:nearest_error =
\ s:FindError(l:buffer, a:line, a:column, a:end_line, a:end_column)
\ s:FindError(l:buffer, a:line, a:column, a:end_line, a:end_column, v:null)
if l:nearest_error isnot v:null
let l:diagnostics = [

View File

@ -280,7 +280,7 @@ Execute(tsserver codefix requests should be sent):
call ale#linter#Reset()
runtime ale_linters/typescript/tsserver.vim
let g:ale_buffer_info = {bufnr(''): {'loclist': [{'lnum': 2, 'col': 5, 'code': 2304}]}}
let g:ale_buffer_info = {bufnr(''): {'loclist': [{'lnum': 2, 'col': 5, 'code': 2304, 'linter_name': 'tsserver'}]}}
call setpos('.', [bufnr(''), 2, 16, 0])
" ALECodeAction
@ -314,7 +314,7 @@ Execute(tsserver codefix requests should be sent only for error with code):
call ale#linter#Reset()
runtime ale_linters/typescript/tsserver.vim
let g:ale_buffer_info = {bufnr(''): {'loclist': [{'lnum': 2, 'col': 16}, {'lnum': 2, 'col': 16, 'code': 2304}]}}
let g:ale_buffer_info = {bufnr(''): {'loclist': [{'lnum': 2, 'col': 16, 'linter_name': 'tsserver'}, {'lnum': 2, 'col': 16, 'code': 2304, 'linter_name': 'tsserver'}]}}
call setpos('.', [bufnr(''), 2, 16, 0])
" ALECodeAction