Skip to content

Commit 5143eed

Browse files
tests: Add tests for markup highlighter
1 parent 6fb42c4 commit 5143eed

File tree

8 files changed

+352
-30
lines changed

8 files changed

+352
-30
lines changed

lua/orgmode/colors/highlighter/init.lua

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
---@field private stars OrgStarsHighlighter
44
---@field private markup OrgMarkupHighlighter
55
---@field private todos OrgTodosHighlighter
6+
---@field private _ephemeral boolean
67
---@field private buffers table<number, { language_tree: LanguageTree, tree: TSTree }>
78
local OrgHighlighter = {}
89
local config = require('orgmode.config')
@@ -11,6 +12,8 @@ function OrgHighlighter:new()
1112
local data = {
1213
namespace = vim.api.nvim_create_namespace('org_custom_highlighter'),
1314
buffers = {},
15+
-- Use ephemeral for highlights. Added to config to allow toggling from tests.
16+
_ephemeral = true,
1417
}
1518
setmetatable(data, self)
1619
self.__index = self

lua/orgmode/colors/highlighter/markup/dates.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ end
129129
---@param bufnr number
130130
function OrgDates:highlight(highlights, bufnr)
131131
local namespace = self.markup.highlighter.namespace
132+
local ephemeral = self.markup:use_ephemeral()
132133

133134
for _, entry in ipairs(highlights) do
134135
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
135-
ephemeral = true,
136+
ephemeral = ephemeral,
136137
end_col = entry.to.end_col,
137138
hl_group = entry.char == '>' and 'OrgTSTimestampActive' or 'OrgTSTimestampInactive',
138139
priority = 110,

lua/orgmode/colors/highlighter/markup/emphasis.lua

+12-23
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,38 @@ end
6060
function OrgEmphasis:highlight(highlights, bufnr)
6161
local namespace = self.markup.highlighter.namespace
6262
local hide_markers = config.org_hide_emphasis_markers
63+
local ephemeral = self.markup:use_ephemeral()
64+
local conceal = hide_markers and '' or nil
6365

6466
for _, entry in ipairs(highlights) do
65-
local hl_offset = 1
66-
6767
-- Leading delimiter
6868
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
69-
ephemeral = true,
70-
end_col = entry.from.start_col + hl_offset,
69+
ephemeral = ephemeral,
70+
end_col = entry.from.end_col,
7171
hl_group = markers[entry.char].hl_name .. '_delimiter',
7272
spell = markers[entry.char].spell,
7373
priority = 110 + entry.from.start_col,
74+
conceal = conceal,
7475
})
7576

7677
-- Closing delimiter
77-
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - hl_offset, {
78-
ephemeral = true,
78+
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.start_col, {
79+
ephemeral = ephemeral,
7980
end_col = entry.to.end_col,
8081
hl_group = markers[entry.char].hl_name .. '_delimiter',
8182
spell = markers[entry.char].spell,
8283
priority = 110 + entry.from.start_col,
84+
conceal = conceal,
8385
})
8486

8587
-- Main body highlight
86-
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + hl_offset, {
87-
ephemeral = true,
88-
end_col = entry.to.end_col - hl_offset,
88+
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + 1, {
89+
ephemeral = ephemeral,
90+
end_col = entry.to.end_col - 1,
8991
hl_group = markers[entry.char].hl_name,
9092
spell = markers[entry.char].spell,
9193
priority = 110 + entry.from.start_col,
9294
})
93-
94-
if hide_markers then
95-
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
96-
end_col = entry.from.end_col,
97-
ephemeral = true,
98-
conceal = '',
99-
})
100-
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.to.line, entry.to.start_col, {
101-
end_col = entry.to.end_col,
102-
ephemeral = true,
103-
conceal = '',
104-
})
105-
end
10695
end
10796
end
10897

@@ -114,7 +103,7 @@ function OrgEmphasis:parse_node(node)
114103
return false
115104
end
116105

117-
local id = table.concat({'emphasis', node_type}, '_')
106+
local id = table.concat({ 'emphasis', node_type }, '_')
118107

119108
return {
120109
type = 'emphasis',

lua/orgmode/colors/highlighter/markup/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ function OrgMarkup:has_valid_parent(item)
218218
return false
219219
end
220220

221+
function OrgMarkup:use_ephemeral()
222+
---@diagnostic disable-next-line: invisible
223+
return self.highlighter._ephemeral
224+
end
225+
221226
return OrgMarkup

lua/orgmode/colors/highlighter/markup/latex.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ end
9696
---@param highlights OrgMarkupHighlight[]
9797
---@param bufnr number
9898
function OrgLatex:highlight(highlights, bufnr)
99+
local ephemeral = self.markup:use_ephemeral()
99100
local namespace = self.markup.highlighter.namespace
100101
for _, entry in ipairs(highlights) do
101102
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col - 1, {
102-
ephemeral = true,
103+
ephemeral = ephemeral,
103104
end_col = entry.to.end_col,
104105
hl_group = 'org_latex',
105106
spell = false,

lua/orgmode/colors/highlighter/markup/link.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ end
9292
---@param bufnr number
9393
function OrgLink:highlight(highlights, bufnr)
9494
local namespace = self.markup.highlighter.namespace
95+
local ephemeral = self.markup:use_ephemeral()
9596

9697
for _, entry in ipairs(highlights) do
9798
local link =
@@ -100,26 +101,26 @@ function OrgLink:highlight(highlights, bufnr)
100101
local link_end = link:find('%]%[') or (link:len() - 1)
101102

102103
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
103-
ephemeral = true,
104+
ephemeral = ephemeral,
104105
end_col = entry.to.end_col,
105106
hl_group = 'org_hyperlink',
106107
priority = 110,
107108
})
108109

109110
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
110-
ephemeral = true,
111+
ephemeral = ephemeral,
111112
end_col = entry.from.start_col + 1 + alias,
112113
conceal = '',
113114
})
114115

115116
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + 2, {
116-
ephemeral = true,
117+
ephemeral = ephemeral,
117118
end_col = entry.from.start_col - 1 + link_end,
118119
spell = false,
119120
})
120121

121122
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - 2, {
122-
ephemeral = true,
123+
ephemeral = ephemeral,
123124
end_col = entry.to.end_col,
124125
conceal = '',
125126
})

lua/orgmode/init.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local auto_instance_keys = {
1616
---@class Org
1717
---@field initialized boolean
1818
---@field files OrgFiles
19+
---@field highlighter OrgHighlighter
1920
---@field agenda OrgAgenda
2021
---@field capture OrgCapture
2122
---@field clock OrgClock
@@ -41,8 +42,8 @@ function Org:init()
4142
if self.initialized then
4243
return
4344
end
44-
require('orgmode.colors.highlighter'):new()
4545
require('orgmode.events').init()
46+
self.highlighter = require('orgmode.colors.highlighter'):new()
4647
self.files = require('orgmode.files'):new({
4748
paths = require('orgmode.config').org_agenda_files,
4849
})

0 commit comments

Comments
 (0)