diff --git a/.luarc.json b/.luarc.json index 17a5072..3c99bde 100644 --- a/.luarc.json +++ b/.luarc.json @@ -2,5 +2,6 @@ "diagnostics.globals": [ "describe", "it", + "before_each", ] } diff --git a/README.md b/README.md index 3027876..2531726 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ return { config = { -- startVisible = true, -- showBlankVirtLine = true, + -- highlightColor = "Comment", -- hints = { -- Caret = { text = "^", prio = 2 }, -- Dollar = { text = "$", prio = 1 }, diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index f0289bb..a2e8656 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -28,12 +28,14 @@ local M = {} ---@class Precognition.Config ---@field startVisible boolean ---@field showBlankVirtLine boolean +---@field highlightColor string ---@field hints Precognition.HintConfig ---@field gutterHints Precognition.GutterHintConfig ---@class Precognition.PartialConfig ---@field startVisible? boolean ---@field showBlankVirtLine? boolean +---@field highlightColor? string ---@field hints? Precognition.HintConfig ---@field gutterHints? Precognition.GutterHintConfig @@ -70,6 +72,7 @@ local defaultHintConfig = { local default = { startVisible = true, showBlankVirtLine = true, + highlightColor = "Comment", hints = defaultHintConfig, gutterHints = { --prio is not currentlt used for gutter hints @@ -140,7 +143,7 @@ local function build_virt_line(marks, line_len) if line:match("^%s+$") then return {} end - table.insert(virt_line, { line, "Comment" }) + table.insert(virt_line, { line, config.highlightColor }) return virt_line end @@ -172,7 +175,7 @@ local function apply_gutter_hints(gutter_hints, bufnr) end vim.fn.sign_define(gutter_name_prefix .. hint, { text = config.gutterHints[hint].text, - texthl = "Comment", + texthl = config.highlightColor, }) local ok, res = pcall(vim.fn.sign_place, 0, gutter_group, gutter_name_prefix .. hint, bufnr, { lnum = loc, @@ -244,8 +247,9 @@ local function display_marks() end local function on_cursor_moved(ev) + local buf = ev and ev.buf or vim.api.nvim_get_current_buf() if extmark then - local ext = vim.api.nvim_buf_get_extmark_by_id(ev.buf, ns, extmark, { + local ext = vim.api.nvim_buf_get_extmark_by_id(buf, ns, extmark, { details = true, }) if ext and ext[1] ~= vim.api.nvim_win_get_cursor(0)[1] - 1 then @@ -366,6 +370,18 @@ local state = { build_gutter_hints = function() return build_gutter_hints end, + on_cursor_moved = function() + return on_cursor_moved + end, + extmark = function() + return extmark + end, + gutter_group = function() + return gutter_group + end, + ns = function() + return ns + end, } setmetatable(M, { diff --git a/tests/precognition/e2e_spec.lua b/tests/precognition/e2e_spec.lua new file mode 100644 index 0000000..c579d45 --- /dev/null +++ b/tests/precognition/e2e_spec.lua @@ -0,0 +1,179 @@ +local precognition = require("precognition") +---@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 + +describe("e2e tests", function() + before_each(function() + precognition.setup({}) + end) + + it("auto commands are set", function() + local autocmds = vim.api.nvim_get_autocmds({ group = "precognition" }) + eq(4, vim.tbl_count(autocmds)) + precognition.peek() + autocmds = vim.api.nvim_get_autocmds({ group = "precognition" }) + eq(7, vim.tbl_count(autocmds)) + end) + + -- it("namespace is created", function() + -- local ns = vim.api.nvim_get_namespaces() + -- + -- eq(1, ns["precognition"]) + -- eq(2, ns["precognition_gutter"]) + -- end) + -- + it("virtual line is displayed and updated", function() + local buffer = vim.api.nvim_create_buf(true, false) + vim.api.nvim_set_current_buf(buffer) + vim.api.nvim_buf_set_lines( + buffer, + 0, + -1, + false, + { "Hello World this is a test", "line 2", "", "line 4", "", "line 6" } + ) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + + precognition.on_cursor_moved() + + local extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { + details = true, + }) + + local gutter_extmarks = get_gutter_extmarks(buffer) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq("Comment", extmark[4].sign_hl_group) + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq("Comment", extmark[4].sign_hl_group) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq("Comment", extmark[4].sign_hl_group) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq("Comment", extmark[4].sign_hl_group) + eq(2, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) + eq("b e w $", extmarks[3].virt_lines[1][1][1]) + eq("Comment", extmarks[3].virt_lines[1][1][2]) + + vim.api.nvim_win_set_cursor(0, { 1, 6 }) + precognition.on_cursor_moved() + + extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { + details = true, + }) + + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) + eq("b e w $", extmarks[3].virt_lines[1][1][1]) + + vim.api.nvim_win_set_cursor(0, { 2, 1 }) + precognition.on_cursor_moved() + + gutter_extmarks = get_gutter_extmarks(buffer) + + extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { + details = true, + }) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq(2, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) + eq("b e w", extmarks[3].virt_lines[1][1][1]) + + vim.api.nvim_win_set_cursor(0, { 4, 1 }) + precognition.on_cursor_moved() + gutter_extmarks = get_gutter_extmarks(buffer) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq(2, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq(4, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + end) + + it("virtual line text color can be customised", function() + precognition.setup({ highlightColor = "Function" }) + local buffer = vim.api.nvim_create_buf(true, false) + vim.api.nvim_set_current_buf(buffer) + vim.api.nvim_buf_set_lines( + buffer, + 0, + -1, + false, + { "Hello World this is a test", "line 2", "", "line 4", "", "line 6" } + ) + vim.api.nvim_win_set_cursor(0, { 1, 1 }) + + precognition.on_cursor_moved() + + local extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { + details = true, + }) + + local gutter_extmarks = get_gutter_extmarks(buffer) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq("Function", extmark[4].sign_hl_group) + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq("Function", extmark[4].sign_hl_group) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq("Function", extmark[4].sign_hl_group) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq("Function", extmark[4].sign_hl_group) + eq(2, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) + eq("b e w $", extmarks[3].virt_lines[1][1][1]) + eq("Function", extmarks[3].virt_lines[1][1][2]) + end) +end)