Skip to content

Commit e4b60e3

Browse files
committed
fix(filter): handle mixed whole-line and column diagnostics (#179)
1 parent 962241b commit e4b60e3

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

lua/tiny-inline-diagnostic/filter.lua

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ function M.at_position(opts, diagnostics, line, col)
3434
end
3535

3636
local current_pos_diags = vim.tbl_filter(function(diag)
37-
if diag.lnum ~= line then
38-
return false
39-
end
40-
if diag.col == 0 and diag.end_col == 0 then
41-
return true
42-
end
43-
return col >= diag.col and col <= diag.end_col
37+
return diag.lnum == line and col >= diag.col and col <= diag.end_col
4438
end, diagnostics)
4539

4640
if opts.options.show_diags_only_under_cursor then
47-
return current_pos_diags
41+
local seen = {}
42+
for _, d in ipairs(current_pos_diags) do
43+
seen[d] = true
44+
end
45+
local result = vim.list_extend({}, current_pos_diags)
46+
for _, d in ipairs(diags_on_line) do
47+
if not seen[d] and d.col == 0 and d.end_col == 0 then
48+
result[#result + 1] = d
49+
end
50+
end
51+
return result
4852
else
4953
return #current_pos_diags > 0 and current_pos_diags or diags_on_line
5054
end

tests/test_filter.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,38 @@ T["at_position"]["matches whole line when diagnostic has no column info under de
8686
MiniTest.expect.equality(result[1].lnum, 5)
8787
end
8888

89+
T["at_position"]["default mode keeps line fallback when mixed whole-line and column-specific diagnostics"] = function()
90+
local diagnostics = {
91+
H.make_diagnostic({ lnum = 5, col = 0, end_col = 0 }),
92+
H.make_diagnostic({ lnum = 5, col = 10, end_col = 20 }),
93+
}
94+
95+
local result = filter.at_position({ options = {} }, diagnostics, 5, 5)
96+
MiniTest.expect.equality(#result, 2)
97+
end
98+
99+
T["at_position"]["default mode returns only column-specific diagnostic when cursor is on it"] = function()
100+
local diagnostics = {
101+
H.make_diagnostic({ lnum = 5, col = 0, end_col = 0 }),
102+
H.make_diagnostic({ lnum = 5, col = 10, end_col = 20 }),
103+
}
104+
105+
local result = filter.at_position({ options = {} }, diagnostics, 5, 15)
106+
MiniTest.expect.equality(#result, 1)
107+
MiniTest.expect.equality(result[1].col, 10)
108+
end
109+
110+
T["at_position"]["show_diags_only_under_cursor merges whole-line and under-cursor diagnostics"] = function()
111+
local diagnostics = {
112+
H.make_diagnostic({ lnum = 5, col = 0, end_col = 0 }),
113+
H.make_diagnostic({ lnum = 5, col = 10, end_col = 20 }),
114+
}
115+
116+
local result =
117+
filter.at_position({ options = { show_diags_only_under_cursor = true } }, diagnostics, 5, 15)
118+
MiniTest.expect.equality(#result, 2)
119+
end
120+
89121
T["under_cursor"] = MiniTest.new_set()
90122

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

0 commit comments

Comments
 (0)