From dad87d93346239087bacb685e8e39107321a15d2 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 24 May 2024 20:22:36 +0100 Subject: [PATCH 01/15] feat: inlay hints --- lua/precognition/init.lua | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index fa7b128..545fd23 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -1,4 +1,5 @@ local hm = require("precognition.horizontal_motions") + local vm = require("precognition.vertical_motions") local utils = require("precognition.utils") @@ -89,13 +90,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) @@ -105,9 +106,10 @@ local ns = vim.api.nvim_create_namespace("precognition") local gutter_group = "precognition_gutter" ---@param marks Precognition.VirtLine +---@param line_num integer ---@param line_len integer ---@return table -local function build_virt_line(marks, line_len) +local function build_virt_line(marks, line_num, line_len) if not marks then return {} end @@ -141,6 +143,33 @@ local function build_virt_line(marks, line_len) end end + if vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) then + local inlays_hints = vim.lsp.inlay_hint.get({ + bufnr = 0, + range = { + start = { line = line_num - 1, character = 0 }, + ["end"] = { line = line_num - 1, character = line_len - 1 }, + }, + }) + -- sort the hints by start character + table.sort(inlays_hints, function(a, b) + return a.inlay_hint.label[1].location.range.start.character + < b.inlay_hint.label[1].location.range.start.character + end) + + local total_added = 0 + local i = 0 + for _, hint in ipairs(inlays_hints) do + local length = #hint.inlay_hint.label[1].value + 1 + local start = hint.inlay_hint.label[1].location.range.start.character + 1 + -- Pad characters to the left of the hint to avoid overlapping with the inlay hint + local pad = utils.create_pad_array(length, "") + table.insert(line_table, start + 1, pad) + total_added = total_added + length + i = i + 1 + vim.print("Added " .. total_added .. " spaces over " .. i .. "occurrences") + end + end local line = table.concat(line_table) if line:match("^%s+$") then return {} @@ -232,7 +261,7 @@ local function display_marks() Zero = 1, } - local virt_line = build_virt_line(virtual_line_marks, line_len) + local virt_line = build_virt_line(virtual_line_marks, cursorline, line_len) -- TODO: can we add indent lines to the virt line to match indent-blankline or similar (if installed)? From 607cd62192e96d2f029afdbe52e6fa01111dc7c9 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 24 May 2024 20:32:05 +0100 Subject: [PATCH 02/15] tests: fix tests --- lua/precognition/init.lua | 1 - tests/precognition/virtline_spec.lua | 24 +++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 545fd23..dfabd4c 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -1,5 +1,4 @@ local hm = require("precognition.horizontal_motions") - local vm = require("precognition.vertical_motions") local utils = require("precognition.utils") diff --git a/tests/precognition/virtline_spec.lua b/tests/precognition/virtline_spec.lua index 0bbf0cd..06931dd 100644 --- a/tests/precognition/virtline_spec.lua +++ b/tests/precognition/virtline_spec.lua @@ -8,7 +8,7 @@ describe("Build Virtual Line", function() Caret = 4, Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq(" ^ $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -17,7 +17,7 @@ describe("Build Virtual Line", function() local marks = { Caret = 4, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq(" ^ ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -26,7 +26,7 @@ describe("Build Virtual Line", function() local marks = { Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq(" $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -35,7 +35,7 @@ describe("Build Virtual Line", function() local marks = { Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq("^ ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -50,7 +50,7 @@ describe("Build Virtual Line", function() w = 10, Dollar = 50, } - local virtual_line = precognition.build_virt_line(marks, 50) + local virtual_line = precognition.build_virt_line(marks, 1, 50) local line_num = 1 for char in virtual_line[1][1]:gmatch(".") do if line_num == 1 then @@ -74,6 +74,7 @@ describe("Build Virtual Line", function() it("example virtual line", function() local line = "abcdef ghijkl mnopqr stuvwx yz" local cursorcol = 2 + local cursorline = 1 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) @@ -84,7 +85,7 @@ describe("Build Virtual Line", function() 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) + }, cursorline, line_len) eq("b e w $", virt_line[1][1]) eq(#line, #virt_line[1][1]) @@ -94,6 +95,7 @@ describe("Build Virtual Line", function() local line = " abc def" -- abc def local cursorcol = 5 + local cursorline = 1 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) @@ -104,7 +106,7 @@ describe("Build Virtual Line", function() 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) + }, cursorline, line_len) eq(" ^ e w $", virt_line[1][1]) eq(#line, #virt_line[1][1]) @@ -133,7 +135,7 @@ describe("Priority", function() Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq(" w ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -159,7 +161,7 @@ describe("Priority", function() Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 10) + local virtual_line = precognition.build_virt_line(marks, 1, 10) eq(" w $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -184,7 +186,7 @@ describe("Priority", function() Dollar = 1, } - local virtual_line = precognition.build_virt_line(marks, 1) + local virtual_line = precognition.build_virt_line(marks, 1, 1) eq("$", virtual_line[1][1]) eq(1, #virtual_line[1][1]) @@ -202,7 +204,7 @@ describe("Priority", function() }, }) - virtual_line = precognition.build_virt_line(marks, 1) + virtual_line = precognition.build_virt_line(marks, 1, 1) eq("^", virtual_line[1][1]) eq(1, #virtual_line[1][1]) end) From fd1a352d416be0222bdb745af1aad026fbfce395 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 24 May 2024 20:33:33 +0100 Subject: [PATCH 03/15] chore: format --- lua/precognition/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index dfabd4c..c0dd523 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -89,13 +89,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) From ce77dc44aa08cf927b6dc9233cd4b97afc646433 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 24 May 2024 21:44:33 +0100 Subject: [PATCH 04/15] fix: 0.9 compatibility --- lua/precognition/compat.lua | 10 ++++++++++ lua/precognition/init.lua | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 lua/precognition/compat.lua diff --git a/lua/precognition/compat.lua b/lua/precognition/compat.lua new file mode 100644 index 0000000..04424d1 --- /dev/null +++ b/lua/precognition/compat.lua @@ -0,0 +1,10 @@ +local M = {} + +function M.inlay_hints_enabled(t) + if vim.lsp and vim.lsp.inlay_hint and vim.lsp.inlay_hint.is_enabled then + return vim.lsp.inlay_hint.is_enabled(t) + end + return false +end + +return M diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index c0dd523..9ac2d60 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -1,6 +1,7 @@ local hm = require("precognition.horizontal_motions") local vm = require("precognition.vertical_motions") local utils = require("precognition.utils") +local compat = require("precognition.compat") local M = {} @@ -89,13 +90,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) @@ -142,7 +143,7 @@ local function build_virt_line(marks, line_num, line_len) end end - if vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) then + if compat.inlay_hints_enabled({ bufnr = 0 }) then local inlays_hints = vim.lsp.inlay_hint.get({ bufnr = 0, range = { From db5e9b24a9a5deb2122ca45847f63a738a452160 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 24 May 2024 21:46:06 +0100 Subject: [PATCH 05/15] chore: format --- lua/precognition/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 9ac2d60..da69074 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -90,13 +90,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) From 5424e07f72e4d793fb8d917ea91b706200cb0d4f Mon Sep 17 00:00:00 2001 From: tris203 Date: Sat, 25 May 2024 13:28:00 +0100 Subject: [PATCH 06/15] tests: refactor create test utils --- tests/precognition/e2e_spec.lua | 38 ++++++------------------------ tests/precognition/utils/utils.lua | 30 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 tests/precognition/utils/utils.lua diff --git a/tests/precognition/e2e_spec.lua b/tests/precognition/e2e_spec.lua index dec6d34..232a385 100644 --- a/tests/precognition/e2e_spec.lua +++ b/tests/precognition/e2e_spec.lua @@ -1,32 +1,8 @@ local precognition = require("precognition") +local tu = require("tests.precognition.utils.utils") ---@diagnostic disable-next-line: undefined-field local eq = assert.are.same -local function get_gutter_extmarks(buffer) - local gutter_extmarks = {} - for _, extmark in - pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { - details = true, - })) - do - if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then - table.insert(gutter_extmarks, extmark) - end - end - return gutter_extmarks -end - -local function hex2dec(hex) - hex = hex:gsub("#", "") - local r = tonumber("0x" .. hex:sub(1, 2)) - local g = tonumber("0x" .. hex:sub(3, 4)) - local b = tonumber("0x" .. hex:sub(5, 6)) - - local dec = (r * 256 ^ 2) + (g * 256) + b - - return dec -end - describe("e2e tests", function() before_each(function() precognition.setup({}) @@ -65,7 +41,7 @@ describe("e2e tests", function() details = true, }) - local gutter_extmarks = get_gutter_extmarks(buffer) + local gutter_extmarks = tu.get_gutter_extmarks(buffer) for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then @@ -104,7 +80,7 @@ describe("e2e tests", function() vim.api.nvim_win_set_cursor(0, { 2, 1 }) precognition.on_cursor_moved() - gutter_extmarks = get_gutter_extmarks(buffer) + gutter_extmarks = tu.get_gutter_extmarks(buffer) extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { details = true, @@ -129,7 +105,7 @@ describe("e2e tests", function() vim.api.nvim_win_set_cursor(0, { 4, 1 }) precognition.on_cursor_moved() - gutter_extmarks = get_gutter_extmarks(buffer) + gutter_extmarks = tu.get_gutter_extmarks(buffer) for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then @@ -165,7 +141,7 @@ describe("e2e tests", function() details = true, }) - local gutter_extmarks = get_gutter_extmarks(buffer) + local gutter_extmarks = tu.get_gutter_extmarks(buffer) for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then @@ -199,7 +175,7 @@ describe("e2e tests", function() local background = "#00ff00" local foreground = "#ff0000" local customColor = { foreground = foreground, background = background } - local customMark = { fg = hex2dec(foreground), bg = hex2dec(background) } + local customMark = { fg = tu.hex2dec(foreground), bg = tu.hex2dec(background) } precognition.setup({ highlightColor = customColor }) local buffer = vim.api.nvim_create_buf(true, false) vim.api.nvim_set_current_buf(buffer) @@ -218,7 +194,7 @@ describe("e2e tests", function() details = true, }) - local gutter_extmarks = get_gutter_extmarks(buffer) + local gutter_extmarks = tu.get_gutter_extmarks(buffer) for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then diff --git a/tests/precognition/utils/utils.lua b/tests/precognition/utils/utils.lua new file mode 100644 index 0000000..98359de --- /dev/null +++ b/tests/precognition/utils/utils.lua @@ -0,0 +1,30 @@ +local precognition = require("precognition") + +local M = {} + +function M.get_gutter_extmarks(buffer) + local gutter_extmarks = {} + for _, extmark in + pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { + details = true, + })) + do + if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then + table.insert(gutter_extmarks, extmark) + end + end + return gutter_extmarks +end + +function M.hex2dec(hex) + hex = hex:gsub("#", "") + local r = tonumber("0x" .. hex:sub(1, 2)) + local g = tonumber("0x" .. hex:sub(3, 4)) + local b = tonumber("0x" .. hex:sub(5, 6)) + + local dec = (r * 256 ^ 2) + (g * 256) + b + + return dec +end + +return M From 307ff718e9c0cf5678f61c07025637971b8fbe07 Mon Sep 17 00:00:00 2001 From: tris203 Date: Sun, 26 May 2024 13:04:33 +0100 Subject: [PATCH 07/15] wip: works fine apart from tab spaced lines --- lua/precognition/compat.lua | 8 +++++++ lua/precognition/init.lua | 31 ++++++++++++++-------------- tests/precognition/virtline_spec.lua | 8 +++---- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lua/precognition/compat.lua b/lua/precognition/compat.lua index 04424d1..e5b1e9b 100644 --- a/lua/precognition/compat.lua +++ b/lua/precognition/compat.lua @@ -7,4 +7,12 @@ function M.inlay_hints_enabled(t) return false end +function M.flatten(t) + if vim.fn.has("nvim-0.11") then + return vim.iter(t):flatten():totable() + else + return vim.tbl_flatten(t) + end +end + return M diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index da69074..7f18a0f 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -90,13 +90,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) @@ -151,24 +151,23 @@ local function build_virt_line(marks, line_num, line_len) ["end"] = { line = line_num - 1, character = line_len - 1 }, }, }) - -- sort the hints by start character - table.sort(inlays_hints, function(a, b) - return a.inlay_hint.label[1].location.range.start.character - < b.inlay_hint.label[1].location.range.start.character - end) - - local total_added = 0 - local i = 0 + + -- local total_added = 0 + -- local i = 0 for _, hint in ipairs(inlays_hints) do local length = #hint.inlay_hint.label[1].value + 1 - local start = hint.inlay_hint.label[1].location.range.start.character + 1 + local start = hint.inlay_hint.label[1].location.range.start.character + 2 + vim.print(vim.inspect(hint.inlay_hint.label[1])) + -- + 2 as we want to add to the character after the hint -- Pad characters to the left of the hint to avoid overlapping with the inlay hint - local pad = utils.create_pad_array(length, "") - table.insert(line_table, start + 1, pad) - total_added = total_added + length - i = i + 1 - vim.print("Added " .. total_added .. " spaces over " .. i .. "occurrences") + local pad = utils.create_pad_array(length, " ") + table.insert(line_table, start, pad) + vim.print("Added " .. length .. " spaces at " .. start) + -- total_added = total_added + length + -- i = i + 1 + -- vim.print("Added " .. total_added .. " spaces over " .. i .. " occurrences") end + line_table = compat.flatten(line_table) end local line = table.concat(line_table) if line:match("^%s+$") then diff --git a/tests/precognition/virtline_spec.lua b/tests/precognition/virtline_spec.lua index 06931dd..365c24e 100644 --- a/tests/precognition/virtline_spec.lua +++ b/tests/precognition/virtline_spec.lua @@ -226,7 +226,7 @@ describe("replacment charcters", function() Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 1) + local virtual_line = precognition.build_virt_line(marks, 1, 1) eq("x", virtual_line[1][1]) eq(1, #virtual_line[1][1]) end) @@ -246,7 +246,7 @@ describe("replacment charcters", function() Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 1) + local virtual_line = precognition.build_virt_line(marks, 1, 1) eq("â", virtual_line[1][1]) eq(2, #virtual_line[1][1]) end) @@ -263,7 +263,7 @@ describe("replacment charcters", function() Dollar = 8, } - local virtual_line = precognition.build_virt_line(marks, 8) + local virtual_line = precognition.build_virt_line(marks, 1, 8) eq("0^e w $", virtual_line[1][1]) end) @@ -288,7 +288,7 @@ describe("replacment charcters", function() Dollar = 8, } - local virtual_line = precognition.build_virt_line(marks, 8) + local virtual_line = precognition.build_virt_line(marks, 1, 8) eq("0âe w $", virtual_line[1][1]) end) end) From a50454f47e3ebb59c49eacf22c037cb488245b20 Mon Sep 17 00:00:00 2001 From: tris203 Date: Sun, 26 May 2024 13:06:16 +0100 Subject: [PATCH 08/15] chore: style --- lua/precognition/init.lua | 4 ++-- tests/precognition/utils/utils.lua | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 7f18a0f..d898ea2 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -90,13 +90,13 @@ local config = default ---@type integer? local extmark -- the active extmark in the current buffer ---@type boolean -local dirty -- whether a redraw is needed +local dirty -- whether a redraw is needed ---@type boolean local visible = false ---@type string local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame ---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs -local gutter_signs_cache = {} -- cache for gutter signs +local gutter_signs_cache = {} -- cache for gutter signs ---@type integer local au = vim.api.nvim_create_augroup("precognition", { clear = true }) diff --git a/tests/precognition/utils/utils.lua b/tests/precognition/utils/utils.lua index 98359de..8f0a8cf 100644 --- a/tests/precognition/utils/utils.lua +++ b/tests/precognition/utils/utils.lua @@ -5,9 +5,9 @@ local M = {} function M.get_gutter_extmarks(buffer) local gutter_extmarks = {} for _, extmark in - pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { - details = true, - })) + pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { + details = true, + })) do if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then table.insert(gutter_extmarks, extmark) From d96eced15e27e134a28ebe4909028a17f1c973da Mon Sep 17 00:00:00 2001 From: tris203 Date: Sun, 26 May 2024 23:26:42 +0100 Subject: [PATCH 09/15] feat: padding table and tab compatibility --- lua/precognition/init.lua | 54 ++++++++++++++-------------- lua/precognition/utils.lua | 24 +++++++++++++ tests/precognition/virtline_spec.lua | 32 ++++++++--------- 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index d898ea2..0c87104 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -55,6 +55,10 @@ local M = {} ---@field PrevParagraph Precognition.PlaceLoc ---@field NextParagraph Precognition.PlaceLoc +---@class Precognition.ExtraPadding +---@field start integer +---@field length integer + ---@type Precognition.HintConfig local defaultHintConfig = { Caret = { text = "^", prio = 2 }, @@ -106,10 +110,10 @@ local ns = vim.api.nvim_create_namespace("precognition") local gutter_group = "precognition_gutter" ---@param marks Precognition.VirtLine ----@param line_num integer ---@param line_len integer +---@param extra_padding Precognition.ExtraPadding[] ---@return table -local function build_virt_line(marks, line_num, line_len) +local function build_virt_line(marks, line_len, extra_padding) if not marks then return {} end @@ -143,32 +147,13 @@ local function build_virt_line(marks, line_num, line_len) end end - if compat.inlay_hints_enabled({ bufnr = 0 }) then - local inlays_hints = vim.lsp.inlay_hint.get({ - bufnr = 0, - range = { - start = { line = line_num - 1, character = 0 }, - ["end"] = { line = line_num - 1, character = line_len - 1 }, - }, - }) - - -- local total_added = 0 - -- local i = 0 - for _, hint in ipairs(inlays_hints) do - local length = #hint.inlay_hint.label[1].value + 1 - local start = hint.inlay_hint.label[1].location.range.start.character + 2 - vim.print(vim.inspect(hint.inlay_hint.label[1])) - -- + 2 as we want to add to the character after the hint - -- Pad characters to the left of the hint to avoid overlapping with the inlay hint - local pad = utils.create_pad_array(length, " ") - table.insert(line_table, start, pad) - vim.print("Added " .. length .. " spaces at " .. start) - -- total_added = total_added + length - -- i = i + 1 - -- vim.print("Added " .. total_added .. " spaces over " .. i .. " occurrences") + if #extra_padding > 0 then + for _, padding in ipairs(extra_padding) do + table.insert(line_table, padding.start, utils.create_pad_array(padding.length, " ")) end line_table = compat.flatten(line_table) end + local line = table.concat(line_table) if line:match("^%s+$") then return {} @@ -238,6 +223,8 @@ local function display_marks() local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop local cur_line = vim.api.nvim_get_current_line():gsub("\t", string.rep(" ", tab_width)) local line_len = vim.fn.strcharlen(cur_line) + ---@type Precognition.ExtraPadding[] + local extra_padding = {} -- local after_cursor = vim.fn.strcharpart(cur_line, cursorcol + 1) -- local before_cursor = vim.fn.strcharpart(cur_line, 0, cursorcol - 1) -- local before_cursor_rev = string.reverse(before_cursor) @@ -260,7 +247,22 @@ local function display_marks() Zero = 1, } - local virt_line = build_virt_line(virtual_line_marks, cursorline, line_len) + if compat.inlay_hints_enabled({ bufnr = 0 }) then + local inlays_hints = vim.lsp.inlay_hint.get({ + bufnr = 0, + range = { + start = { line = cursorline - 1, character = 0 }, + ["end"] = { line = cursorline - 1, character = line_len - 1 }, + }, + }) + + for _, hint in ipairs(inlays_hints) do + local length, ws_offset = utils.calc_ws_offset(hint, tab_width, cursorcol) + table.insert(extra_padding, { start = ws_offset, length = length }) + end + end + + local virt_line = build_virt_line(virtual_line_marks, line_len, extra_padding) -- TODO: can we add indent lines to the virt line to match indent-blankline or similar (if installed)? diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index b1c2802..ac6f2bd 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -49,4 +49,28 @@ function M.create_pad_array(len, str) return pad_array end +---calculates the white space offset of a partial string +---@param hint any +---@param tab_width integer +---@param cursorcol integer +---@return integer +---@return integer +function M.calc_ws_offset(hint, tab_width, cursorcol) + -- + 1 here because of trailing padding + local length = #hint.inlay_hint.label[1].value + 1 + local start = hint.inlay_hint.position.character + local finetune + if cursorcol - 1 >= start then + finetune = 1 + else + finetune = -1 + end + + local unexpanded = vim.api.nvim_get_current_line() + local prefix = vim.fn.strcharpart(unexpanded, 0, start) + local expanded = string.gsub(prefix, "\t", string.rep(" ", tab_width)) + local ws_offset = vim.fn.strcharlen(expanded) + finetune + return length, ws_offset +end + return M diff --git a/tests/precognition/virtline_spec.lua b/tests/precognition/virtline_spec.lua index 365c24e..1756763 100644 --- a/tests/precognition/virtline_spec.lua +++ b/tests/precognition/virtline_spec.lua @@ -8,7 +8,7 @@ describe("Build Virtual Line", function() Caret = 4, Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq(" ^ $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -17,7 +17,7 @@ describe("Build Virtual Line", function() local marks = { Caret = 4, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq(" ^ ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -26,7 +26,7 @@ describe("Build Virtual Line", function() local marks = { Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq(" $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -35,7 +35,7 @@ describe("Build Virtual Line", function() local marks = { Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq("^ ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -50,7 +50,7 @@ describe("Build Virtual Line", function() w = 10, Dollar = 50, } - local virtual_line = precognition.build_virt_line(marks, 1, 50) + local virtual_line = precognition.build_virt_line(marks, 50, {}) local line_num = 1 for char in virtual_line[1][1]:gmatch(".") do if line_num == 1 then @@ -74,7 +74,6 @@ describe("Build Virtual Line", function() it("example virtual line", function() local line = "abcdef ghijkl mnopqr stuvwx yz" local cursorcol = 2 - local cursorline = 1 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) @@ -85,7 +84,7 @@ describe("Build Virtual Line", function() 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), - }, cursorline, line_len) + }, line_len, {}) eq("b e w $", virt_line[1][1]) eq(#line, #virt_line[1][1]) @@ -95,7 +94,6 @@ describe("Build Virtual Line", function() local line = " abc def" -- abc def local cursorcol = 5 - local cursorline = 1 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) @@ -106,7 +104,7 @@ describe("Build Virtual Line", function() 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), - }, cursorline, line_len) + }, line_len, {}) eq(" ^ e w $", virt_line[1][1]) eq(#line, #virt_line[1][1]) @@ -135,7 +133,7 @@ describe("Priority", function() Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq(" w ", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -161,7 +159,7 @@ describe("Priority", function() Dollar = 10, } - local virtual_line = precognition.build_virt_line(marks, 1, 10) + local virtual_line = precognition.build_virt_line(marks, 10, {}) eq(" w $", virtual_line[1][1]) eq(10, #virtual_line[1][1]) end) @@ -186,7 +184,7 @@ describe("Priority", function() Dollar = 1, } - local virtual_line = precognition.build_virt_line(marks, 1, 1) + local virtual_line = precognition.build_virt_line(marks, 1, {}) eq("$", virtual_line[1][1]) eq(1, #virtual_line[1][1]) @@ -204,7 +202,7 @@ describe("Priority", function() }, }) - virtual_line = precognition.build_virt_line(marks, 1, 1) + virtual_line = precognition.build_virt_line(marks, 1, {}) eq("^", virtual_line[1][1]) eq(1, #virtual_line[1][1]) end) @@ -226,7 +224,7 @@ describe("replacment charcters", function() Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 1, 1) + local virtual_line = precognition.build_virt_line(marks, 1, {}) eq("x", virtual_line[1][1]) eq(1, #virtual_line[1][1]) end) @@ -246,7 +244,7 @@ describe("replacment charcters", function() Caret = 1, } - local virtual_line = precognition.build_virt_line(marks, 1, 1) + local virtual_line = precognition.build_virt_line(marks, 1, {}) eq("â", virtual_line[1][1]) eq(2, #virtual_line[1][1]) end) @@ -263,7 +261,7 @@ describe("replacment charcters", function() Dollar = 8, } - local virtual_line = precognition.build_virt_line(marks, 1, 8) + local virtual_line = precognition.build_virt_line(marks, 8, {}) eq("0^e w $", virtual_line[1][1]) end) @@ -288,7 +286,7 @@ describe("replacment charcters", function() Dollar = 8, } - local virtual_line = precognition.build_virt_line(marks, 1, 8) + local virtual_line = precognition.build_virt_line(marks, 8, {}) eq("0âe w $", virtual_line[1][1]) end) end) From 554a47096d1552e3dc5ad6faaca3a0431534ec68 Mon Sep 17 00:00:00 2001 From: tris203 Date: Mon, 27 May 2024 00:06:12 +0100 Subject: [PATCH 10/15] fix: tweaks to positioning --- lua/precognition/compat.lua | 8 -------- lua/precognition/init.lua | 3 +-- lua/precognition/utils.lua | 9 +-------- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/lua/precognition/compat.lua b/lua/precognition/compat.lua index e5b1e9b..04424d1 100644 --- a/lua/precognition/compat.lua +++ b/lua/precognition/compat.lua @@ -7,12 +7,4 @@ function M.inlay_hints_enabled(t) return false end -function M.flatten(t) - if vim.fn.has("nvim-0.11") then - return vim.iter(t):flatten():totable() - else - return vim.tbl_flatten(t) - end -end - return M diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 0c87104..98c92da 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -149,9 +149,8 @@ local function build_virt_line(marks, line_len, extra_padding) if #extra_padding > 0 then for _, padding in ipairs(extra_padding) do - table.insert(line_table, padding.start, utils.create_pad_array(padding.length, " ")) + line_table[padding.start] = line_table[padding.start] .. string.rep(" ", padding.length) end - line_table = compat.flatten(line_table) end local line = table.concat(line_table) diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index ac6f2bd..a84fa2f 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -59,17 +59,10 @@ function M.calc_ws_offset(hint, tab_width, cursorcol) -- + 1 here because of trailing padding local length = #hint.inlay_hint.label[1].value + 1 local start = hint.inlay_hint.position.character - local finetune - if cursorcol - 1 >= start then - finetune = 1 - else - finetune = -1 - end - local unexpanded = vim.api.nvim_get_current_line() local prefix = vim.fn.strcharpart(unexpanded, 0, start) local expanded = string.gsub(prefix, "\t", string.rep(" ", tab_width)) - local ws_offset = vim.fn.strcharlen(expanded) + finetune + local ws_offset = vim.fn.strcharlen(expanded) return length, ws_offset end From e34b3b797a67632fef4a3b4c79f8920a8bc26db1 Mon Sep 17 00:00:00 2001 From: tris203 Date: Mon, 27 May 2024 00:08:55 +0100 Subject: [PATCH 11/15] chore: unused parameter --- lua/precognition/init.lua | 2 +- lua/precognition/utils.lua | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 98c92da..e7e3f47 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, cursorcol) + local length, ws_offset = utils.calc_ws_offset(hint, tab_width) table.insert(extra_padding, { start = ws_offset, length = length }) end end diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index a84fa2f..84d3799 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -52,10 +52,9 @@ end ---calculates the white space offset of a partial string ---@param hint any ---@param tab_width integer ----@param cursorcol integer ---@return integer ---@return integer -function M.calc_ws_offset(hint, tab_width, cursorcol) +function M.calc_ws_offset(hint, tab_width) -- + 1 here because of trailing padding local length = #hint.inlay_hint.label[1].value + 1 local start = hint.inlay_hint.position.character From 6758524ce6cf0918ae767d6478524f0a53ad92d0 Mon Sep 17 00:00:00 2001 From: tris203 Date: Wed, 29 May 2024 16:45:52 +0100 Subject: [PATCH 12/15] tests: LSP tests --- .luarc.json | 1 + tests/precognition/inlay_hints_spec.lua | 74 +++++++++++++++++++++++++ tests/precognition/utils/compat.lua | 5 ++ tests/precognition/utils/lsp.lua | 53 ++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 tests/precognition/inlay_hints_spec.lua create mode 100644 tests/precognition/utils/compat.lua create mode 100644 tests/precognition/utils/lsp.lua diff --git a/.luarc.json b/.luarc.json index 3c99bde..f0141ca 100644 --- a/.luarc.json +++ b/.luarc.json @@ -3,5 +3,6 @@ "describe", "it", "before_each", + "after_each", ] } diff --git a/tests/precognition/inlay_hints_spec.lua b/tests/precognition/inlay_hints_spec.lua new file mode 100644 index 0000000..eaea20a --- /dev/null +++ b/tests/precognition/inlay_hints_spec.lua @@ -0,0 +1,74 @@ +local server = require("tests.precognition.utils.lsp").server +local compat = require("tests.precognition.utils.compat") +local precognition = require("precognition") +---@diagnostic disable-next-line: undefined-field +local eq = assert.are.same +---@diagnostic disable-next-line: undefined-field +local neq = assert.are_not.same +local buf + +local function wait(condition, msg) + vim.wait(100, condition) + local result = condition() + neq(false, result, msg) + neq(nil, result, msg) +end + +describe("lsp based tests", function() + before_each(function() + require("tests.precognition.utils.lsp").Reset() + buf = vim.api.nvim_create_buf(true, false) + local srv = vim.lsp.start_client({ cmd = server }) + if srv then + vim.lsp.buf_attach_client(buf, srv) + end + end) + + it("initialize lsp", function() + eq(2, #require("tests.precognition.utils.lsp").messages) + eq("initialize", require("tests.precognition.utils.lsp").messages[1].method) + eq("initialized", require("tests.precognition.utils.lsp").messages[2].method) + end) + + it("can enable inlay hints", function() + vim.lsp.inlay_hint.enable(true, { bufnr = buf }) + + eq(3, #require("tests.precognition.utils.lsp").messages) + eq("textDocument/inlayHint", require("tests.precognition.utils.lsp").messages[3].method) + end) + + it("inlay hint shifts the line", function() + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "here is a string" }) + vim.api.nvim_set_current_buf(buf) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + + precognition.on_cursor_moved() + + local extmarks = vim.api.nvim_buf_get_extmark_by_id(buf, precognition.ns, precognition.extmark, { + details = true, + }) + + eq("b e w $", extmarks[3].virt_lines[1][1][1]) + + vim.lsp.inlay_hint.enable(true, { bufnr = buf }) + -- NOTE:The test LSP replies with an inlay hint, that suggest "foo" as line 1, position 4 + -- This means that the inlay hint is shifted by 3 chars + + precognition.on_cursor_moved() + + extmarks = vim.api.nvim_buf_get_extmark_by_id(buf, precognition.ns, precognition.extmark, { + details = true, + }) + + eq("b e w $", extmarks[3].virt_lines[1][1][1]) + end) + + after_each(function() + vim.lsp.inlay_hint.enable(false, { bufnr = buf }) + vim.api.nvim_buf_delete(buf, { force = true }) + vim.lsp.stop_client(compat.get_active_lsp_clients()) + wait(function() + return vim.tbl_count(compat.get_active_lsp_clients()) == 0 + end, "clients must stop") + end) +end) diff --git a/tests/precognition/utils/compat.lua b/tests/precognition/utils/compat.lua new file mode 100644 index 0000000..b36c14c --- /dev/null +++ b/tests/precognition/utils/compat.lua @@ -0,0 +1,5 @@ +local M = {} + +M.get_active_lsp_clients = vim.lsp.get_clients() or vim.lsp.get_active_clients() + +return M diff --git a/tests/precognition/utils/lsp.lua b/tests/precognition/utils/lsp.lua new file mode 100644 index 0000000..7218300 --- /dev/null +++ b/tests/precognition/utils/lsp.lua @@ -0,0 +1,53 @@ +local M = {} + +M.messages = {} + +function M.server() + local closing = false + local srv = {} + + function srv.request(method, params, handler) + table.insert(M.messages, { method = method, params = params }) + if method == "initialize" then + handler(nil, { + capabilities = { + inlayHintProvider = true, + }, + }) + elseif method == "shutdown" then + handler(nil, nil) + elseif method == "textDocument/inlayHint" then + handler(nil, { + { + position = { line = 0, character = 3 }, + label = { { value = "foo" } }, + }, + }) + else + assert(false, "Unhandled method: " .. method) + end + end + + function srv.notify(method, params) + table.insert(M.messages, { method = method, params = params }) + if method == "exit" then + closing = true + end + end + + function srv.is_closing() + return closing + end + + function srv.terminate() + closing = true + end + + return srv +end + +function M.Reset() + M.messages = {} +end + +return M From d946daf9c344d60e5234bd5b9b3e7044f5880e06 Mon Sep 17 00:00:00 2001 From: tris203 Date: Wed, 29 May 2024 17:31:05 +0100 Subject: [PATCH 13/15] tests: more tests --- lua/precognition/init.lua | 2 +- lua/precognition/utils.lua | 9 ++--- tests/precognition/virtline_spec.lua | 51 +++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) 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() From 50185f09a1b9997aaa1a41fc5bb5019be65fc98d Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Fri, 31 May 2024 16:35:21 -0700 Subject: [PATCH 14/15] chore: remove debug print --- lua/precognition/utils.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index abcc515..86cafd1 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -61,7 +61,6 @@ function M.calc_ws_offset(hint, tab_width, current_line) local start = hint.inlay_hint.position.character 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 From 96c658a0c4124b9dd1011d29fa259615bb97e531 Mon Sep 17 00:00:00 2001 From: tris203 Date: Sat, 1 Jun 2024 19:40:05 +0100 Subject: [PATCH 15/15] chore: end --- lua/precognition/utils.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index ab8a613..ffdb4b1 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -65,7 +65,8 @@ function M.calc_ws_offset(hint, tab_width, current_line) local expanded = string.gsub(prefix, "\t", string.rep(" ", tab_width)) local ws_offset = vim.fn.strcharlen(expanded) return length, ws_offset - +end + ---Add extra padding for multi byte character characters ---@param cur_line string ---@param extra_padding Precognition.ExtraPadding[]