Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: further customise highlight colors #31

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ return {
config = {
-- startVisible = true,
-- showBlankVirtLine = true,
-- highlightColor = "Comment",
-- highlightColor = { link = "Comment"),
-- hints = {
-- Caret = { text = "^", prio = 2 },
-- Dollar = { text = "$", prio = 1 },
Expand Down Expand Up @@ -52,6 +52,9 @@ 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
tris203 marked this conversation as resolved.
Show resolved Hide resolved
a set of custom [highlight values](<https://neovim.io/doc/user/api.html#nvim_set_hl()>).

## ❔Usage

Expand Down
17 changes: 12 additions & 5 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
---@field highlightColor? vim.api.keyset.highlight
---@field hints? Precognition.HintConfig
---@field gutterHints? Precognition.GutterHintConfig

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -352,9 +352,16 @@ 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
willothy marked this conversation as resolved.
Show resolved Hide resolved

ns = vim.api.nvim_create_namespace("precognition")
au = vim.api.nvim_create_augroup("precognition", { clear = true })

local hl_name = "PrecognitionHighlight"
vim.api.nvim_set_hl(0, hl_name, config.highlightColor)

if config.startVisible then
M.show()
end
Expand Down
93 changes: 82 additions & 11 deletions tests/precognition/e2e_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand All @@ -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)
Loading