Skip to content

Commit

Permalink
tests: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed May 29, 2024
1 parent 6758524 commit d946daf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lua/precognition/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 50 additions & 1 deletion tests/precognition/virtline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand Down

0 comments on commit d946daf

Please sign in to comment.