Skip to content

Commit

Permalink
feat: custom highlight colors (#28)
Browse files Browse the repository at this point in the history
* feat: custom highlight colors

* docs: custom color

* test: add tests

* tests: mac isn't on 0.10 yet

* chore: fmt
  • Loading branch information
tris203 authored May 22, 2024
1 parent 7a4c5eb commit 455f527
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 3 deletions.
1 change: 1 addition & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"diagnostics.globals": [
"describe",
"it",
"before_each",
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ return {
config = {
-- startVisible = true,
-- showBlankVirtLine = true,
-- highlightColor = "Comment",
-- hints = {
-- Caret = { text = "^", prio = 2 },
-- Dollar = { text = "$", prio = 1 },
Expand Down
22 changes: 19 additions & 3 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand Down
179 changes: 179 additions & 0 deletions tests/precognition/e2e_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 455f527

Please sign in to comment.