Skip to content

Commit

Permalink
tests: Add tests for markup highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Feb 16, 2024
1 parent 6fb42c4 commit 5143eed
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 30 deletions.
3 changes: 3 additions & 0 deletions lua/orgmode/colors/highlighter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
---@field private stars OrgStarsHighlighter
---@field private markup OrgMarkupHighlighter
---@field private todos OrgTodosHighlighter
---@field private _ephemeral boolean
---@field private buffers table<number, { language_tree: LanguageTree, tree: TSTree }>
local OrgHighlighter = {}
local config = require('orgmode.config')
Expand All @@ -11,6 +12,8 @@ function OrgHighlighter:new()
local data = {
namespace = vim.api.nvim_create_namespace('org_custom_highlighter'),
buffers = {},
-- Use ephemeral for highlights. Added to config to allow toggling from tests.
_ephemeral = true,
}
setmetatable(data, self)
self.__index = self
Expand Down
3 changes: 2 additions & 1 deletion lua/orgmode/colors/highlighter/markup/dates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ end
---@param bufnr number
function OrgDates:highlight(highlights, bufnr)
local namespace = self.markup.highlighter.namespace
local ephemeral = self.markup:use_ephemeral()

for _, entry in ipairs(highlights) do
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.to.end_col,
hl_group = entry.char == '>' and 'OrgTSTimestampActive' or 'OrgTSTimestampInactive',
priority = 110,
Expand Down
35 changes: 12 additions & 23 deletions lua/orgmode/colors/highlighter/markup/emphasis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,49 +60,38 @@ end
function OrgEmphasis:highlight(highlights, bufnr)
local namespace = self.markup.highlighter.namespace
local hide_markers = config.org_hide_emphasis_markers
local ephemeral = self.markup:use_ephemeral()
local conceal = hide_markers and '' or nil

for _, entry in ipairs(highlights) do
local hl_offset = 1

-- Leading delimiter
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
ephemeral = true,
end_col = entry.from.start_col + hl_offset,
ephemeral = ephemeral,
end_col = entry.from.end_col,
hl_group = markers[entry.char].hl_name .. '_delimiter',
spell = markers[entry.char].spell,
priority = 110 + entry.from.start_col,
conceal = conceal,
})

-- Closing delimiter
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - hl_offset, {
ephemeral = true,
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.start_col, {
ephemeral = ephemeral,
end_col = entry.to.end_col,
hl_group = markers[entry.char].hl_name .. '_delimiter',
spell = markers[entry.char].spell,
priority = 110 + entry.from.start_col,
conceal = conceal,
})

-- Main body highlight
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + hl_offset, {
ephemeral = true,
end_col = entry.to.end_col - hl_offset,
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + 1, {
ephemeral = ephemeral,
end_col = entry.to.end_col - 1,
hl_group = markers[entry.char].hl_name,
spell = markers[entry.char].spell,
priority = 110 + entry.from.start_col,
})

if hide_markers then
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
end_col = entry.from.end_col,
ephemeral = true,
conceal = '',
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.to.line, entry.to.start_col, {
end_col = entry.to.end_col,
ephemeral = true,
conceal = '',
})
end
end
end

Expand All @@ -114,7 +103,7 @@ function OrgEmphasis:parse_node(node)
return false
end

local id = table.concat({'emphasis', node_type}, '_')
local id = table.concat({ 'emphasis', node_type }, '_')

return {
type = 'emphasis',
Expand Down
5 changes: 5 additions & 0 deletions lua/orgmode/colors/highlighter/markup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,9 @@ function OrgMarkup:has_valid_parent(item)
return false
end

function OrgMarkup:use_ephemeral()
---@diagnostic disable-next-line: invisible
return self.highlighter._ephemeral
end

return OrgMarkup
3 changes: 2 additions & 1 deletion lua/orgmode/colors/highlighter/markup/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ end
---@param highlights OrgMarkupHighlight[]
---@param bufnr number
function OrgLatex:highlight(highlights, bufnr)
local ephemeral = self.markup:use_ephemeral()
local namespace = self.markup.highlighter.namespace
for _, entry in ipairs(highlights) do
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col - 1, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.to.end_col,
hl_group = 'org_latex',
spell = false,
Expand Down
9 changes: 5 additions & 4 deletions lua/orgmode/colors/highlighter/markup/link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ end
---@param bufnr number
function OrgLink:highlight(highlights, bufnr)
local namespace = self.markup.highlighter.namespace
local ephemeral = self.markup:use_ephemeral()

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

vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.to.end_col,
hl_group = 'org_hyperlink',
priority = 110,
})

vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.from.start_col + 1 + alias,
conceal = '',
})

vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col + 2, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.from.start_col - 1 + link_end,
spell = false,
})

vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.end_col - 2, {
ephemeral = true,
ephemeral = ephemeral,
end_col = entry.to.end_col,
conceal = '',
})
Expand Down
3 changes: 2 additions & 1 deletion lua/orgmode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local auto_instance_keys = {
---@class Org
---@field initialized boolean
---@field files OrgFiles
---@field highlighter OrgHighlighter
---@field agenda OrgAgenda
---@field capture OrgCapture
---@field clock OrgClock
Expand All @@ -41,8 +42,8 @@ function Org:init()
if self.initialized then
return
end
require('orgmode.colors.highlighter'):new()
require('orgmode.events').init()
self.highlighter = require('orgmode.colors.highlighter'):new()
self.files = require('orgmode.files'):new({
paths = require('orgmode.config').org_agenda_files,
})
Expand Down
Loading

0 comments on commit 5143eed

Please sign in to comment.