diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index e7e3f47..e333df0 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -256,7 +256,7 @@ local function display_marks() }) for _, hint in ipairs(inlays_hints) do - local length, ws_offset = utils.calc_ws_offset(hint, tab_width) + local length, ws_offset = utils.calc_ws_offset(hint, tab_width, vim.api.nvim_get_current_line()) table.insert(extra_padding, { start = ws_offset, length = length }) end end diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index 84d3799..abcc515 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -50,17 +50,18 @@ function M.create_pad_array(len, str) end ---calculates the white space offset of a partial string ----@param hint any +---@param hint vim.lsp.inlay_hint.get.ret ---@param tab_width integer +---@param current_line string ---@return integer ---@return integer -function M.calc_ws_offset(hint, tab_width) +function M.calc_ws_offset(hint, tab_width, current_line) -- + 1 here because of trailing padding local length = #hint.inlay_hint.label[1].value + 1 local start = hint.inlay_hint.position.character - local unexpanded = vim.api.nvim_get_current_line() - local prefix = vim.fn.strcharpart(unexpanded, 0, start) + local prefix = vim.fn.strcharpart(current_line, 0, start) local expanded = string.gsub(prefix, "\t", string.rep(" ", tab_width)) + vim.print(expanded) local ws_offset = vim.fn.strcharlen(expanded) return length, ws_offset end diff --git a/tests/precognition/virtline_spec.lua b/tests/precognition/virtline_spec.lua index 1756763..78fbca2 100644 --- a/tests/precognition/virtline_spec.lua +++ b/tests/precognition/virtline_spec.lua @@ -92,7 +92,6 @@ describe("Build Virtual Line", function() it("example virtual line with whitespace padding", function() local line = " abc def" - -- abc def local cursorcol = 5 local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop local cur_line = line:gsub("\t", string.rep(" ", tab_width)) @@ -109,6 +108,56 @@ describe("Build Virtual Line", function() eq(" ^ e w $", virt_line[1][1]) eq(#line, #virt_line[1][1]) end) + + it("can build a line with extra padding", function() + local line = " abc def" + local cursorcol = 5 + local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop + local cur_line = line:gsub("\t", string.rep(" ", tab_width)) + local line_len = vim.fn.strcharlen(cur_line) + local extra_padding = { { start = 4, length = 4 } } + + local virt_line = precognition.build_virt_line({ + w = hm.next_word_boundary(cur_line, cursorcol, line_len, false), + e = hm.end_of_word(cur_line, cursorcol, line_len, false), + b = hm.prev_word_boundary(cur_line, cursorcol, line_len, false), + Caret = hm.line_start_non_whitespace(cur_line, cursorcol, line_len), + Dollar = hm.line_end(cur_line, cursorcol, line_len), + }, line_len, extra_padding) + + local total_added = 0 + for _, pad in ipairs(extra_padding) do + total_added = total_added + pad.length + end + + eq(" ^ e w $", virt_line[1][1]) + eq(#line + total_added, #virt_line[1][1]) + end) + + it("can build a line with multiple extra padddings", function() + local line = " abc def" + local cursorcol = 5 + local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop + local cur_line = line:gsub("\t", string.rep(" ", tab_width)) + local line_len = vim.fn.strcharlen(cur_line) + local extra_padding = { { start = 4, length = 4 }, { start = 10, length = 5 } } + + local virt_line = precognition.build_virt_line({ + w = hm.next_word_boundary(cur_line, cursorcol, line_len, false), + e = hm.end_of_word(cur_line, cursorcol, line_len, false), + b = hm.prev_word_boundary(cur_line, cursorcol, line_len, false), + Caret = hm.line_start_non_whitespace(cur_line, cursorcol, line_len), + Dollar = hm.line_end(cur_line, cursorcol, line_len), + }, line_len, extra_padding) + + local total_added = 0 + for _, pad in ipairs(extra_padding) do + total_added = total_added + pad.length + end + + eq(" ^ e w $", virt_line[1][1]) + eq(#line + total_added, #virt_line[1][1]) + end) end) describe("Priority", function()