Skip to content

Commit be0e12b

Browse files
committed
fix(extmarks): include inlay hints in offset calculation. Fixes #69
nice
1 parent 6ae90ab commit be0e12b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lua/tiny-inline-diagnostic/chunk.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ function M.get_chunks(opts, diags_on_line, diag_index, diag_line, cursor_line, b
240240
local other_extmarks_offset = extmarks.handle_other_extmarks(opts, buf, diag_line, line_length)
241241

242242
if (opts.options.overflow.mode ~= "none" and not opts.options.multilines) or cursor_line == diag_line then
243-
if line_length > win_width - opts.options.softwrap then
243+
if (line_length + other_extmarks_offset) > win_width - opts.options.softwrap then
244244
need_to_be_under = true
245245
end
246246
end
@@ -267,7 +267,7 @@ function M.get_chunks(opts, diags_on_line, diag_index, diag_line, cursor_line, b
267267
offset = win_col
268268
end
269269

270-
chunks = M.get_message_chunks_for_overflow(diag_message, offset + other_extmarks_offset, win_width, opts)
270+
chunks = M.get_message_chunks_for_overflow(diag_message, offset, win_width, opts)
271271
elseif opts.options.overflow.mode == "none" then
272272
chunks = utils.wrap_text(diag_message, 0)
273273
elseif opts.options.overflow.mode == "oneline" then

lua/tiny-inline-diagnostic/extmarks.lua

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ function M.clear(buf)
146146
pcall(vim.api.nvim_buf_clear_namespace, buf, DIAGNOSTIC_NAMESPACE, 0, -1)
147147
end
148148

149+
--- Count characters of inlay hints on a line
150+
--- @param buf number
151+
--- @param linenr number
152+
local function count_inlay_hints_characters(buf, linenr)
153+
local inlay_hints = vim.lsp.inlay_hint.get({
154+
bufnr = buf,
155+
range = {
156+
start = { line = linenr, character = 0 },
157+
["end"] = { line = linenr, character = -1 },
158+
},
159+
})
160+
local count = 0
161+
for _, hint in ipairs(inlay_hints) do
162+
if type(hint.inlay_hint.label) == "string" then
163+
count = count + #hint.inlay_hint.label
164+
else
165+
for _, label in ipairs(hint.inlay_hint.label) do
166+
count = count + #label.value
167+
end
168+
end
169+
end
170+
return count
171+
end
172+
149173
---@param bufnr number
150174
---@param linenr number
151175
---@param col number
@@ -163,12 +187,12 @@ function M.get_extmarks_on_line(bufnr, linenr, col)
163187
return vim.api.nvim_buf_get_extmarks(bufnr, -1, { linenr, col }, { linenr, -1 }, opts)
164188
end
165189

166-
---@param buf number
190+
---@param bufnr number
167191
---@param curline number
168192
---@param col number
169193
---@return number
170-
function M.handle_other_extmarks(_, buf, curline, col)
171-
local extmarks = M.get_extmarks_on_line(buf, curline, col)
194+
function M.handle_other_extmarks(_, bufnr, curline, col)
195+
local extmarks = M.get_extmarks_on_line(bufnr, curline, col)
172196
local offset = 0
173197

174198
for _, extmark in ipairs(extmarks) do
@@ -180,6 +204,11 @@ function M.handle_other_extmarks(_, buf, curline, col)
180204
end
181205
end
182206

207+
if vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }) then
208+
local inlay_hints_count = count_inlay_hints_characters(bufnr, curline)
209+
offset = offset + inlay_hints_count
210+
end
211+
183212
return offset
184213
end
185214

0 commit comments

Comments
 (0)