Skip to content
This repository was archived by the owner on Jul 17, 2022. It is now read-only.

Commit 6814f3b

Browse files
author
Jose Alvarez
committed
fix(inlay hints): guard against unloaded buffer and nonexistent lines
1 parent 4405c1a commit 6814f3b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lua/nvim-lsp-ts-utils/inlay-hints.lua

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ end
2121

2222
local function handler(err, result, ctx)
2323
if not err and result and M.state.enabled then
24-
hide(ctx.bufnr)
24+
local bufnr = ctx.bufnr
25+
if not vim.api.nvim_buf_is_loaded(bufnr) then
26+
return
27+
end
28+
29+
hide(bufnr)
2530

2631
local hints = result.inlayHints or {}
2732
local parsed = {}
28-
2933
for _, value in ipairs(hints) do
3034
local pos = value.position
3135
local line_str = tostring(pos.line)
@@ -42,14 +46,17 @@ local function handler(err, result, ctx)
4246
end
4347
end
4448

49+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
4550
for key, value in pairs(parsed) do
4651
local line = tonumber(key)
47-
for _, hint in ipairs(value) do
48-
vim.api.nvim_buf_set_extmark(ctx.bufnr, M.state.ns, line, -1, {
49-
virt_text_pos = "eol",
50-
virt_text = { { hint.text, o.get().inlay_hints_highlight } },
51-
hl_mode = "combine",
52-
})
52+
if lines[line + 1] then
53+
for _, hint in ipairs(value) do
54+
vim.api.nvim_buf_set_extmark(ctx.bufnr, M.state.ns, line, -1, {
55+
virt_text_pos = "eol",
56+
virt_text = { { hint.text, o.get().inlay_hints_highlight } },
57+
hl_mode = "combine",
58+
})
59+
end
5360
end
5461
end
5562
end

0 commit comments

Comments
 (0)