fix(diagnostic): allow virtual_{lines,text} cursor exclusivity #33517

Problem:
virtual_text diagnostics are great when skimming a file, and
virtual_lines are great when "zooming in" on a particular problem.
Having both enabled results in duplicate diagnostics on-screen.

Solution:
This PR expands the behavior of `current_line` for virtual_text and
virtual_lines by making `virtual_text.current_line = false` distinct
from `nil`.  If you set:

    vim.diagnostic.config({
      virtual_text = { current_line = false },
      virtual_lines = { current_line = true },
    })

With this configuration, virtual_text will be used to display
diagnostics until the cursor reaches the same line, at which point they
will be hidden and virtual_lines will take its place.
This commit is contained in:
Michael Clayton
2025-05-01 06:54:39 -04:00
committed by GitHub
parent 97a6259442
commit d567f899ef
3 changed files with 37 additions and 23 deletions

View File

@@ -2144,6 +2144,25 @@ describe('vim.diagnostic', function()
eq(1, #result)
eq(' Error here!', result[1][4].virt_text[3][1])
end)
it('can hide virtual_text for the current line', function()
local result = exec_lua(function()
vim.api.nvim_win_set_cursor(0, { 1, 0 })
vim.diagnostic.config({ virtual_text = { current_line = false } })
vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
_G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
_G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
})
local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
return extmarks
end)
eq(1, #result)
eq(' Another error there!', result[1][4].virt_text[3][1])
end)
end)
describe('handlers.virtual_lines', function()