Skip to content

Commit 22bd57a

Browse files
committed
fix(filter): anchor zero-width diagnostics at col and col-1 (#179)
1 parent e4b60e3 commit 22bd57a

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lua/tiny-inline-diagnostic/filter.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ function M.at_position(opts, diagnostics, line, col)
3434
end
3535

3636
local current_pos_diags = vim.tbl_filter(function(diag)
37-
return diag.lnum == line and col >= diag.col and col <= diag.end_col
37+
if diag.lnum ~= line then
38+
return false
39+
end
40+
41+
-- Zero-width LSP range (Range.end exclusive, start == end): anchor to the
42+
-- character on either side of the point so end-of-token diagnostics match
43+
if diag.col == diag.end_col and diag.col > 0 then
44+
return col == diag.col or col == diag.col - 1
45+
end
46+
return col >= diag.col and col <= diag.end_col
3847
end, diagnostics)
3948

4049
if opts.options.show_diags_only_under_cursor then

tests/test_filter.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ T["at_position"]["show_diags_only_under_cursor merges whole-line and under-curso
118118
MiniTest.expect.equality(#result, 2)
119119
end
120120

121+
T["at_position"]["zero-width diagnostic matches cursor at col and col-1"] = function()
122+
local diagnostics = {
123+
H.make_diagnostic({ lnum = 5, col = 33, end_col = 33 }),
124+
}
125+
local opts = { options = { show_diags_only_under_cursor = true } }
126+
127+
MiniTest.expect.equality(#filter.at_position(opts, diagnostics, 5, 32), 1)
128+
MiniTest.expect.equality(#filter.at_position(opts, diagnostics, 5, 33), 1)
129+
MiniTest.expect.equality(#filter.at_position(opts, diagnostics, 5, 31), 0)
130+
MiniTest.expect.equality(#filter.at_position(opts, diagnostics, 5, 34), 0)
131+
end
132+
121133
T["under_cursor"] = MiniTest.new_set()
122134

123135
T["under_cursor"]["returns empty for invalid buffer"] = function()

0 commit comments

Comments
 (0)