From 6814f3b7b4661213a837746bb461bad80c2fe526 Mon Sep 17 00:00:00 2001 From: Jose Alvarez Date: Sun, 21 Nov 2021 11:09:16 -0500 Subject: [PATCH] fix(inlay hints): guard against unloaded buffer and nonexistent lines --- lua/nvim-lsp-ts-utils/inlay-hints.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lua/nvim-lsp-ts-utils/inlay-hints.lua b/lua/nvim-lsp-ts-utils/inlay-hints.lua index 5b36c2f..35aa6f6 100644 --- a/lua/nvim-lsp-ts-utils/inlay-hints.lua +++ b/lua/nvim-lsp-ts-utils/inlay-hints.lua @@ -21,11 +21,15 @@ end local function handler(err, result, ctx) if not err and result and M.state.enabled then - hide(ctx.bufnr) + local bufnr = ctx.bufnr + if not vim.api.nvim_buf_is_loaded(bufnr) then + return + end + + hide(bufnr) local hints = result.inlayHints or {} local parsed = {} - for _, value in ipairs(hints) do local pos = value.position local line_str = tostring(pos.line) @@ -42,14 +46,17 @@ local function handler(err, result, ctx) end end + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) for key, value in pairs(parsed) do local line = tonumber(key) - for _, hint in ipairs(value) do - vim.api.nvim_buf_set_extmark(ctx.bufnr, M.state.ns, line, -1, { - virt_text_pos = "eol", - virt_text = { { hint.text, o.get().inlay_hints_highlight } }, - hl_mode = "combine", - }) + if lines[line + 1] then + for _, hint in ipairs(value) do + vim.api.nvim_buf_set_extmark(ctx.bufnr, M.state.ns, line, -1, { + virt_text_pos = "eol", + virt_text = { { hint.text, o.get().inlay_hints_highlight } }, + hl_mode = "combine", + }) + end end end end