From 1cdbde305bbfbc0f119329d876784ebe2281b7f7 Mon Sep 17 00:00:00 2001 From: Joshua Wood Date: Thu, 23 May 2024 18:02:44 +1200 Subject: [PATCH 1/5] feat: further customise highlight colors --- README.md | 4 ++++ lua/precognition/init.lua | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2531726..1e51366 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ return { - `showBlankVirtLine = false` Setting this option will mean that if a Virtual Line would be blank it wont be rendered +- `highlightColor` can be a string or a highlight table. If it's a string, it + must be a valid highlight, see `:highlight`. It can also be a table that defines + a set of [highlight values](https://neovim.io/doc/user/api.html#nvim_set_hl()). + ## ❔Usage diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index a2e8656..0280e14 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -35,7 +35,7 @@ local M = {} ---@class Precognition.PartialConfig ---@field startVisible? boolean ---@field showBlankVirtLine? boolean ----@field highlightColor? string +---@field highlightColor? string | vim.api.keyset.highlight ---@field hints? Precognition.HintConfig ---@field gutterHints? Precognition.GutterHintConfig @@ -355,6 +355,13 @@ function M.setup(opts) ns = vim.api.nvim_create_namespace("precognition") au = vim.api.nvim_create_augroup("precognition", { clear = true }) + + if type(config.highlightColor) == "table" then + vim.api.nvim_set_hl(ns, "precognition", config.highlightColor) + vim.api.nvim_set_hl_ns(ns) + config.highlightColor = "precognition" + end + if config.startVisible then M.show() end From e8b35c4f9dcbd837b199889f3da47abefb6bb6d3 Mon Sep 17 00:00:00 2001 From: Joshua Wood Date: Thu, 23 May 2024 19:43:15 +1200 Subject: [PATCH 2/5] Fixes indentation, highlight casing. --- lua/precognition/init.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 0280e14..6753044 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -357,9 +357,10 @@ function M.setup(opts) au = vim.api.nvim_create_augroup("precognition", { clear = true }) if type(config.highlightColor) == "table" then - vim.api.nvim_set_hl(ns, "precognition", config.highlightColor) - vim.api.nvim_set_hl_ns(ns) - config.highlightColor = "precognition" + local hl_name = "Precognition" + vim.api.nvim_set_hl(ns, hl_name, config.highlightColor) + vim.api.nvim_set_hl_ns(ns) + config.highlightColor = hl_name end if config.startVisible then From 48b95deb032b574dbd11ad0eb501ebc10615614a Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 23 May 2024 17:22:44 +0100 Subject: [PATCH 3/5] refactor: drop string input add tests --- lua/precognition/init.lua | 21 ++++---- tests/precognition/e2e_spec.lua | 93 +++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 6753044..fba6720 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -28,14 +28,14 @@ local M = {} ---@class Precognition.Config ---@field startVisible boolean ---@field showBlankVirtLine boolean ----@field highlightColor string +---@field highlightColor vim.api.keyset.highlight ---@field hints Precognition.HintConfig ---@field gutterHints Precognition.GutterHintConfig ---@class Precognition.PartialConfig ---@field startVisible? boolean ---@field showBlankVirtLine? boolean ----@field highlightColor? string | vim.api.keyset.highlight +---@field highlightColor? vim.api.keyset.highlight ---@field hints? Precognition.HintConfig ---@field gutterHints? Precognition.GutterHintConfig @@ -72,7 +72,7 @@ local defaultHintConfig = { local default = { startVisible = true, showBlankVirtLine = true, - highlightColor = "Comment", + highlightColor = { link = "Comment" }, hints = defaultHintConfig, gutterHints = { --prio is not currentlt used for gutter hints @@ -143,7 +143,7 @@ local function build_virt_line(marks, line_len) if line:match("^%s+$") then return {} end - table.insert(virt_line, { line, config.highlightColor }) + table.insert(virt_line, { line, "PrecognitionHighlight" }) return virt_line end @@ -175,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 = config.highlightColor, + texthl = "PrecognitionHighlight", }) local ok, res = pcall(vim.fn.sign_place, 0, gutter_group, gutter_name_prefix .. hint, bufnr, { lnum = loc, @@ -352,16 +352,15 @@ end ---@param opts Precognition.PartialConfig function M.setup(opts) config = vim.tbl_deep_extend("force", default, opts or {}) + if opts.highlightColor then + config.highlightColor = opts.highlightColor + end ns = vim.api.nvim_create_namespace("precognition") au = vim.api.nvim_create_augroup("precognition", { clear = true }) - if type(config.highlightColor) == "table" then - local hl_name = "Precognition" - vim.api.nvim_set_hl(ns, hl_name, config.highlightColor) - vim.api.nvim_set_hl_ns(ns) - config.highlightColor = hl_name - end + local hl_name = "PrecognitionHighlight" + vim.api.nvim_set_hl(0, hl_name, config.highlightColor) if config.startVisible then M.show() diff --git a/tests/precognition/e2e_spec.lua b/tests/precognition/e2e_spec.lua index c579d45..dec6d34 100644 --- a/tests/precognition/e2e_spec.lua +++ b/tests/precognition/e2e_spec.lua @@ -16,6 +16,17 @@ local function get_gutter_extmarks(buffer) 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({}) @@ -58,16 +69,17 @@ describe("e2e tests", function() for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then - eq("Comment", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq({ link = "Comment" }, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) eq(5, extmark[2]) elseif extmark[4].sign_text == "gg" then - eq("Comment", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) eq(0, extmark[2]) elseif extmark[4].sign_text == "{ " then - eq("Comment", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) eq(0, extmark[2]) elseif extmark[4].sign_text == "} " then - eq("Comment", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) eq(2, extmark[2]) else assert(false, "unexpected sign text") @@ -76,7 +88,8 @@ describe("e2e tests", function() 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]) + eq("PrecognitionHighlight", extmarks[3].virt_lines[1][1][2]) + eq({ link = "Comment" }, vim.api.nvim_get_hl(0, { name = extmarks[3].virt_lines[1][1][2] })) vim.api.nvim_win_set_cursor(0, { 1, 6 }) precognition.on_cursor_moved() @@ -134,7 +147,60 @@ describe("e2e tests", function() end) it("virtual line text color can be customised", function() - precognition.setup({ highlightColor = "Function" }) + precognition.setup({ highlightColor = { link = "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("PrecognitionHighlight", extmark[4].sign_hl_group) + eq({ link = "Function" }, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq({ link = "Function" }, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq({ link = "Function" }, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq({ link = "Function" }, vim.api.nvim_get_hl(0, { name = 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("PrecognitionHighlight", extmarks[3].virt_lines[1][1][2]) + eq({ link = "Function" }, vim.api.nvim_get_hl(0, { name = extmarks[3].virt_lines[1][1][2] })) + end) + + it("virtual line can be customised without a link", function() + local background = "#00ff00" + local foreground = "#ff0000" + local customColor = { foreground = foreground, background = background } + local customMark = { fg = hex2dec(foreground), bg = hex2dec(background) } + precognition.setup({ highlightColor = customColor }) local buffer = vim.api.nvim_create_buf(true, false) vim.api.nvim_set_current_buf(buffer) vim.api.nvim_buf_set_lines( @@ -156,16 +222,20 @@ describe("e2e tests", function() for _, extmark in pairs(gutter_extmarks) do if extmark[4].sign_text == "G " then - eq("Function", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq(customMark, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) eq(5, extmark[2]) elseif extmark[4].sign_text == "gg" then - eq("Function", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq(customMark, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) eq(0, extmark[2]) elseif extmark[4].sign_text == "{ " then - eq("Function", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq(customMark, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) eq(0, extmark[2]) elseif extmark[4].sign_text == "} " then - eq("Function", extmark[4].sign_hl_group) + eq("PrecognitionHighlight", extmark[4].sign_hl_group) + eq(customMark, vim.api.nvim_get_hl(0, { name = extmark[4].sign_hl_group })) eq(2, extmark[2]) else assert(false, "unexpected sign text") @@ -174,6 +244,7 @@ describe("e2e tests", function() 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]) + eq("PrecognitionHighlight", extmarks[3].virt_lines[1][1][2]) + eq(customMark, vim.api.nvim_get_hl(0, { name = extmarks[3].virt_lines[1][1][2] })) end) end) From 32209928b73a70b024addf7a7ddf931152c11a00 Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 23 May 2024 17:26:08 +0100 Subject: [PATCH 4/5] docs: update docs --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e51366..a4ce9a6 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ return { config = { -- startVisible = true, -- showBlankVirtLine = true, - -- highlightColor = "Comment", + -- highlightColor = { link = "Comment"), -- hints = { -- Caret = { text = "^", prio = 2 }, -- Dollar = { text = "$", prio = 1 }, @@ -52,10 +52,9 @@ return { - `showBlankVirtLine = false` Setting this option will mean that if a Virtual Line would be blank it wont be rendered -- `highlightColor` can be a string or a highlight table. If it's a string, it - must be a valid highlight, see `:highlight`. It can also be a table that defines - a set of [highlight values](https://neovim.io/doc/user/api.html#nvim_set_hl()). - +- `highlightColor` is set as a highlight table. It can be a `link` field + that must be a valid highlight, see `:highlight`. It can also be a table that defines + a set of custom [highlight values](). ## ❔Usage From 2ac25e892f9f6215021dce8cdd5b7d789b0cfc7a Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 23 May 2024 22:10:33 +0100 Subject: [PATCH 5/5] docs: clearer wording --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a4ce9a6..47a1034 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,10 @@ return { - `showBlankVirtLine = false` Setting this option will mean that if a Virtual Line would be blank it wont be rendered -- `highlightColor` is set as a highlight table. It can be a `link` field - that must be a valid highlight, see `:highlight`. It can also be a table that defines - a set of custom [highlight values](). +- highlightColor can be set in two ways: + +1. As a table containing a link property pointing to an existing highlight group (see `:highlight` for valid options). +2. As a table specifying custom highlight values, such as foreground and background colors. ([more info]()) ## ❔Usage