Skip to content

Commit 9666b58

Browse files
committed
fix(multilines): handle multilines diagnostics drawing properly
1 parent ff6f5be commit 9666b58

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

lua/tiny-inline-diagnostic/extmarks.lua

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
local M = {}
22

3-
local INITIAL_UUID = 999999999999
3+
local INITIAL_UID = 1
4+
local MAX_UID = 2 ^ 32 - 1
45
local DIAGNOSTIC_NAMESPACE = vim.api.nvim_create_namespace("TinyInlineDiagnostic")
56

67
local state = {
7-
uuid_counter = INITIAL_UUID,
8+
uid_counter = INITIAL_UID,
89
skip_lines = {
910
count = 0,
1011
start_line = 0,
@@ -18,12 +19,12 @@ local function is_valid_buffer(buf)
1819
end
1920

2021
---@return number
21-
local function generate_uuid()
22-
state.uuid_counter = state.uuid_counter - 1
23-
if state.uuid_counter < 0 then
24-
state.uuid_counter = INITIAL_UUID
22+
local function generate_uid()
23+
state.uid_counter = state.uid_counter + 1
24+
if state.uid_counter > MAX_UID then
25+
state.uid_counter = INITIAL_UID
2526
end
26-
return state.uuid_counter
27+
return state.uid_counter
2728
end
2829

2930
---@return {row: number, col: number}
@@ -54,7 +55,7 @@ local function set_extmark(buf, line, virt_text, win_col, priority, pos)
5455
end
5556

5657
vim.api.nvim_buf_set_extmark(buf, DIAGNOSTIC_NAMESPACE, line, 0, {
57-
id = generate_uuid(),
58+
id = generate_uid(),
5859
line_hl_group = line == 0 and "TinyInlineDiagnosticVirtualTextBg" or nil,
5960
virt_text_pos = pos or "eol",
6061
virt_text = virt_text,
@@ -76,7 +77,7 @@ local function create_multiline_extmark(buf, curline, virt_lines, priority)
7677
end
7778

7879
vim.api.nvim_buf_set_extmark(buf, DIAGNOSTIC_NAMESPACE, curline, 0, {
79-
id = curline + 1000,
80+
id = generate_uid(),
8081
virt_text_pos = "eol",
8182
virt_text = virt_lines[1],
8283
virt_lines = remaining_lines,
@@ -91,7 +92,7 @@ local function handle_under_cursor_case(buf, curline, virt_lines, buf_lines_coun
9192
end
9293

9394
vim.api.nvim_buf_set_extmark(buf, DIAGNOSTIC_NAMESPACE, curline + 1, 0, {
94-
id = generate_uuid(),
95+
id = generate_uid(),
9596
virt_text_pos = "overlay",
9697
virt_text_win_col = 0,
9798
virt_text = virt_lines[2],
@@ -121,7 +122,7 @@ local function handle_overflow_case(buf, curline, virt_lines, win_col, offset, p
121122

122123
if #overflow_lines > 0 then
123124
vim.api.nvim_buf_set_extmark(buf, DIAGNOSTIC_NAMESPACE, buf_lines_count - 1, 0, {
124-
id = generate_uuid(),
125+
id = generate_uid(),
125126
virt_lines_above = false,
126127
virt_lines = overflow_lines,
127128
priority = priority,
@@ -216,12 +217,12 @@ end
216217

217218
---@param opts DiagnosticConfig
218219
---@param event table
219-
---@param curline number
220+
---@param diag_line number
220221
---@param virt_lines table
221222
---@param offset number
222223
---@param need_to_be_under boolean
223224
---@param virt_priority number
224-
function M.create_extmarks(opts, event, curline, virt_lines, offset, need_to_be_under, virt_priority)
225+
function M.create_extmarks(opts, event, diag_line, virt_lines, offset, need_to_be_under, virt_priority)
225226
if not is_valid_buffer(event.buf) then
226227
return
227228
end
@@ -239,32 +240,35 @@ function M.create_extmarks(opts, event, curline, virt_lines, offset, need_to_be_
239240
end
240241

241242
-- Handle multiline mode
242-
if opts.options.multilines and curline ~= cursor_line then
243-
if should_skip_line(curline) then
243+
if opts.options.multilines and diag_line ~= cursor_line then
244+
if should_skip_line(diag_line) then
244245
return
245246
end
246-
create_multiline_extmark(event.buf, curline, virt_lines, virt_priority)
247+
create_multiline_extmark(event.buf, diag_line, virt_lines, virt_priority)
248+
249+
state.skip_lines.count = 0
250+
state.skip_lines.start_line = diag_line
247251
return
248252
end
249253

250254
state.skip_lines.count = #virt_lines
251-
state.skip_lines.start_line = curline
255+
state.skip_lines.start_line = diag_line
252256

253257
-- Handle under-cursor case
254258
if need_to_be_under then
255-
handle_under_cursor_case(event.buf, curline, virt_lines, buf_lines_count, virt_priority)
259+
handle_under_cursor_case(event.buf, diag_line, virt_lines, buf_lines_count, virt_priority)
256260
table.remove(virt_lines, 2)
257261
win_col = 0
258-
if curline < buf_lines_count - 1 then
259-
curline = curline + 1
262+
if diag_line < buf_lines_count - 1 then
263+
diag_line = diag_line + 1
260264
end
261265
end
262266

263267
-- Handle overflow case
264-
if curline - 1 + #virt_lines > buf_lines_count - 1 then
265-
handle_overflow_case(event.buf, curline, virt_lines, win_col, offset, virt_priority, buf_lines_count)
268+
if diag_line - 1 + #virt_lines > buf_lines_count - 1 then
269+
handle_overflow_case(event.buf, diag_line, virt_lines, win_col, offset, virt_priority, buf_lines_count)
266270
else
267-
handle_normal_case(event.buf, curline, virt_lines, win_col, offset, virt_priority)
271+
handle_normal_case(event.buf, diag_line, virt_lines, win_col, offset, virt_priority)
268272
end
269273
end
270274

0 commit comments

Comments
 (0)