From a093f370bfde48aa12a87d2641eca5a9686b6a73 Mon Sep 17 00:00:00 2001 From: Arindam Das Date: Fri, 17 Mar 2023 13:30:36 +0530 Subject: [PATCH 1/3] fix(inlay_hints): safely call nvim_buf_get_lines with pcall --- lua/rust-tools/inlay_hints.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index 0df31a1..7f7eaa3 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -124,25 +124,27 @@ local function parse_hints(result, bufnr) if type(result) ~= "table" then return {} end + for _, value in pairs(result) do local range = value.position local line = value.position.line local label = value.label local kind = value.kind - local function add_line() - if map[line] ~= nil then - table.insert(map[line], { label = label, kind = kind, range = range }) - else - map[line] = { { label = label, kind = kind, range = range } } - end + local status_ok, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, line, line + 1, true) + + if not status_ok then + break end - local line_len = - string.len(vim.api.nvim_buf_get_lines(bufnr, line, line + 1, true)[1]) + local line_len = string.len(lines[1]) max_line_len = math.max(max_line_len, line_len) - add_line() + if map[line] ~= nil then + table.insert(map[line], { label = label, kind = kind, range = range }) + else + map[line] = { { label = label, kind = kind, range = range } } + end end return map, max_line_len end From 0fc421bdb43fd4a0f57b1b27ef6292c08a6ee446 Mon Sep 17 00:00:00 2001 From: Arindam Das Date: Sat, 20 May 2023 13:30:48 +0530 Subject: [PATCH 2/3] Revert "fix(inlay_hints): safely call nvim_buf_get_lines with pcall" This reverts commit a093f370bfde48aa12a87d2641eca5a9686b6a73. --- lua/rust-tools/inlay_hints.lua | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index 7f7eaa3..0df31a1 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -124,27 +124,25 @@ local function parse_hints(result, bufnr) if type(result) ~= "table" then return {} end - for _, value in pairs(result) do local range = value.position local line = value.position.line local label = value.label local kind = value.kind - local status_ok, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, line, line + 1, true) - - if not status_ok then - break + local function add_line() + if map[line] ~= nil then + table.insert(map[line], { label = label, kind = kind, range = range }) + else + map[line] = { { label = label, kind = kind, range = range } } + end end - local line_len = string.len(lines[1]) + local line_len = + string.len(vim.api.nvim_buf_get_lines(bufnr, line, line + 1, true)[1]) max_line_len = math.max(max_line_len, line_len) - if map[line] ~= nil then - table.insert(map[line], { label = label, kind = kind, range = range }) - else - map[line] = { { label = label, kind = kind, range = range } } - end + add_line() end return map, max_line_len end From 23fac873486cecb2d71152244b610237454788cd Mon Sep 17 00:00:00 2001 From: Arindam Das Date: Sat, 20 May 2023 13:37:12 +0530 Subject: [PATCH 3/3] fix(inlay_hints): disable strict_indexing for nvim_buf_get_lines The last parameter of vim.api.nvim_buf_get_lines() is a boolean called "strict_indexing". It causes the following behaviour: - When strict_indexing is "true" out of bounds indices cause an error - When strict_indexing is "false" out of bounds indices are clamped to the nearest valid index. Now nvim_buf_get_lines() returns an array, which might be empty. So attempting to access the first element (our line) could evaluate to nil. So we use "" as the fallback line string value. --- lua/rust-tools/inlay_hints.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/rust-tools/inlay_hints.lua b/lua/rust-tools/inlay_hints.lua index 0df31a1..0374735 100644 --- a/lua/rust-tools/inlay_hints.lua +++ b/lua/rust-tools/inlay_hints.lua @@ -139,7 +139,7 @@ local function parse_hints(result, bufnr) end local line_len = - string.len(vim.api.nvim_buf_get_lines(bufnr, line, line + 1, true)[1]) + string.len(vim.api.nvim_buf_get_lines(bufnr, line, line + 1, false)[1] or "") max_line_len = math.max(max_line_len, line_len) add_line()