diff options
author | xu0o0 <hqcat6@gmail.com> | 2023-09-05 21:34:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-05 14:34:33 +0100 |
commit | ea27441834de203c9a8b080d7fbdaa8719f78a52 (patch) | |
tree | 7ff3c6b245c65ec6f274e1ad73fd08ca038b9878 | |
parent | 3bedafc29a45d478f41d8467a988e29c35618737 (diff) | |
download | ale-ea27441834de203c9a8b080d7fbdaa8719f78a52.zip |
fix(neovim): ignore unrelated diagnostics (#4597)
Fix the NeoVim diagnostics bridge so it only sends over diagnostics relevant to the current buffer.
-rw-r--r-- | lua/diagnostics.lua | 46 | ||||
-rw-r--r-- | test/test_neovim_diagnostics.vader | 77 |
2 files changed, 101 insertions, 22 deletions
diff --git a/lua/diagnostics.lua b/lua/diagnostics.lua index 9a3e1e4e..aa2ec92d 100644 --- a/lua/diagnostics.lua +++ b/lua/diagnostics.lua @@ -12,28 +12,30 @@ module.sendAleResultsToDiagnostics = function(buffer, loclist) -- Convert all the ALE loclist items to the shape that Neovim's diagnostic -- API is expecting. for _, location in ipairs(loclist) do - table.insert( - diagnostics, - -- All line numbers from ALE are 1-indexed, but all line numbers - -- in the diagnostics API are 0-indexed, so we have to subtract 1 - -- to make this work. - { - lnum = location.lnum - 1, - -- Ending line number, or if we don't have one, just make it the same - -- as the starting line number - end_lnum = (location.end_lnum or location.lnum) - 1, - -- Which column does the error start on? - col = math.max((location.col or 1) - 1, 0), - -- end_col does *not* appear to need 1 subtracted, so we don't. - end_col = location.end_col, - -- Which severity: error, warning, or info? - severity = ale_type_to_diagnostic_severity[location.type] or "E", - -- The error message - message = location.text, - -- e.g. "rubocop" - source = location.linter_name, - } - ) + if location.bufnr == buffer then + table.insert( + diagnostics, + -- All line numbers from ALE are 1-indexed, but all line numbers + -- in the diagnostics API are 0-indexed, so we have to subtract 1 + -- to make this work. + { + lnum = location.lnum - 1, + -- Ending line number, or if we don't have one, just make it the same + -- as the starting line number + end_lnum = (location.end_lnum or location.lnum) - 1, + -- Which column does the error start on? + col = math.max((location.col or 1) - 1, 0), + -- end_col does *not* appear to need 1 subtracted, so we don't. + end_col = location.end_col, + -- Which severity: error, warning, or info? + severity = ale_type_to_diagnostic_severity[location.type] or "E", + -- The error message + message = location.text, + -- e.g. "rubocop" + source = location.linter_name, + } + ) + end end local virtualtext_enabled_set = {['all'] = true, ['2'] = true, [2] = true, ['current'] = true, ['1'] = true, [1] = true} diff --git a/test/test_neovim_diagnostics.vader b/test/test_neovim_diagnostics.vader new file mode 100644 index 00000000..41997097 --- /dev/null +++ b/test/test_neovim_diagnostics.vader @@ -0,0 +1,77 @@ +Before: + Save g:ale_use_neovim_diagnostics_api + + function! CollectMessages(buffer) + if !has('nvim-0.6') + return + endif + + let l:messages = [] + for l:diag in v:lua.vim.diagnostic.get(a:buffer) + call add(l:messages, l:diag.message) + endfor + + return l:messages + endfunction + + +After: + unlet! b:other_bufnr + delfunction CollectMessages + Restore + +Execute(Should only set diagnostics belonging to the given buffer): + if has('nvim-0.6') + + let b:other_bufnr = bufnr('/foo/bar/baz', 1) + " Make sure we actually get another buffer number, or the test is invalid. + AssertNotEqual -1, b:other_bufnr + + let g:ale_use_neovim_diagnostics_api = 1 + + call ale#engine#SetResults(bufnr('%'), [ + \ { + \ 'lnum': 1, + \ 'col': 10, + \ 'bufnr': bufnr('%'), + \ 'vcol': 0, + \ 'linter_name': 'bettercode', + \ 'nr': -1, + \ 'type': 'W', + \ 'text': 'A', + \ }, + \ { + \ 'lnum': 2, + \ 'col': 10, + \ 'bufnr': b:other_bufnr, + \ 'vcol': 0, + \ 'linter_name': 'bettercode', + \ 'nr': -1, + \ 'type': 'W', + \ 'text': 'B', + \ }, + \ { + \ 'lnum': 3, + \ 'col': 1, + \ 'bufnr': bufnr('%'), + \ 'vcol': 0, + \ 'linter_name': 'bettercode', + \ 'nr': -1, + \ 'type': 'E', + \ 'text': 'C', + \ }, + \ { + \ 'lnum': 4, + \ 'col': 1, + \ 'bufnr': b:other_bufnr, + \ 'vcol': 0, + \ 'linter_name': 'bettercode', + \ 'nr': -1, + \ 'type': 'E', + \ 'text': 'D', + \ }, + \]) + + AssertEqual ["A", "C"], CollectMessages(bufnr('%')) + + endif |