Skip to content

Commit

Permalink
refactor(highlights): Use nvim_buf_set_extmark instead of nvim_buf_ad…
Browse files Browse the repository at this point in the history
…d_highlight

nvim_buf_add_highlights is deprecated in Nvim 0.11
  • Loading branch information
kristijanhusak committed Jan 21, 2025
1 parent 6566b69 commit b56ece9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lua/orgmode/agenda/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function Agenda:_open_window()
})
if ft == 'orgagenda' then
vim.bo[buf].modifiable = true
colors.highlight({}, true, buf)
colors.apply_highlights({}, true, buf)
vim.api.nvim_buf_set_lines(buf, 0, -1, true, {})
return buf
end
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/agenda/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function OrgAgendaView:render()
vim.list_extend(virt_texts, compiled.virt_texts)
end
vim.api.nvim_buf_set_lines(self.bufnr, self.start_line - 1, self.end_line - 1, false, lines)
colors.highlight(highlights, false, self.bufnr)
colors.apply_highlights(highlights, false, self.bufnr)
colors.virtual_text(virt_texts, self.bufnr)

return self
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/agenda/view/line.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function OrgAgendaLine:render()
vim.bo[bufnr].modifiable = true
vim.api.nvim_buf_set_lines(bufnr, self.line_nr - 1, self.line_nr, false, { compiled.content })
vim.bo[bufnr].modifiable = false
colors.highlight(compiled.highlights, false, bufnr)
colors.apply_highlights(compiled.highlights, false, bufnr)
colors.virtual_text(compiled.virt_texts, bufnr)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/colors/highlighter/stars.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function OrgStars:on_line(bufnr, line)
local _, end_col = node:end_()

vim.api.nvim_buf_set_extmark(bufnr, self.highlighter.namespace, line, 0, {
end_line = line,
end_row = line,
end_col = end_col - 1,
hl_group = '@org.leading_stars',
ephemeral = true,
Expand Down
72 changes: 43 additions & 29 deletions lua/orgmode/colors/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,56 @@ M.get_todo_keywords_colors = function()
}
end

---@param highlights table[]
---@class OrgHighlightEntry
---@field range OrgRange
---@field hlgroup string
---@field namespace? number
---@field spell? boolean
---@field priority? number
---@field conceal? boolean
---@field url? string
---@field whole_line? boolean

---@param highlights OrgHighlightEntry[]
---@param clear? boolean
---@return string
M.highlight = function(highlights, clear, bufnr)
M.apply_highlights = function(highlights, clear, bufnr)
bufnr = bufnr or 0
if clear then
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
end
for _, hl in ipairs(highlights) do
if hl.whole_line then
vim.api.nvim_buf_set_extmark(bufnr, namespace, hl.range.start_line - 1, hl.range.start_col - 1, {
hl_group = hl.hlgroup,
end_line = hl.range.start_line,
hl_eol = true,
})
elseif hl.extmark then
vim.api.nvim_buf_set_extmark(bufnr, namespace, hl.range.start_line - 1, hl.range.start_col - 1, {
hl_group = hl.hlgroup,
end_line = hl.range.end_line - 1,
end_col = hl.range.end_col - 1,
spell = hl.spell,
priority = hl.priority,
conceal = hl.conceal,
url = hl.url,
})
else
vim.api.nvim_buf_add_highlight(
bufnr,
namespace,
hl.hlgroup,
hl.range.start_line - 1,
hl.range.start_col - 1,
hl.range.end_col - 1
)
end
M.highlight(hl, bufnr)
end
end

---@param hl OrgHighlightEntry
M.highlight = function(hl, bufnr)
bufnr = bufnr or 0
local start_row = hl.range.start_line - 1
local start_col = hl.range.start_col - 1
local opts = {
hl_group = hl.hlgroup,
end_row = hl.range.end_line - 1,
end_col = hl.range.end_col - 1,
spell = hl.spell,
priority = hl.priority,
conceal = hl.conceal,
url = hl.url,
}

if hl.whole_line then
opts.end_row = start_row + 1
opts.end_col = 0
opts.hl_eol = true
end

if opts.end_col < 0 then
opts.end_col = 99999
opts.strict = false
end

return vim.api.nvim_buf_set_extmark(bufnr, hl.namespace or namespace, start_row, start_col, opts)
end

---@param virt_texts{ range: OrgRange, content: string, virt_text_pos: string, hl_groups: string[] }[]
Expand All @@ -130,7 +144,7 @@ M.clear_extmarks = function(bufnr, start_line, end_line)
bufnr,
namespace,
{ start_line, 0 },
{ end_line, 9999 },
{ end_line, 99999 },
{ details = true }
)
for _, extmark in ipairs(extmarks) do
Expand Down
51 changes: 38 additions & 13 deletions lua/orgmode/objects/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local utils = require('orgmode.utils')
local Promise = require('orgmode.utils.promise')
local config = require('orgmode.config')
local namespace = vim.api.nvim_create_namespace('org_calendar')
local colors = require('orgmode.colors')
local Range = require('orgmode.files.elements.range')

---@alias OrgCalendarOnRenderDayOpts { line: number, from: number, to: number, buf: number, namespace: number }
---@alias OrgCalendarOnRenderDay fun(day: OrgDate, opts: OrgCalendarOnRenderDayOpts)
Expand All @@ -15,7 +17,6 @@ local small_minute_step = config.calendar.min_small_step or config.org_time_stam
---@field win number?
---@field buf number?
---@field callback fun(date: OrgDate | nil, cleared?: boolean)
---@field namespace function
---@field date OrgDate?
---@field title? string
---@field on_day? OrgCalendarOnRenderDay
Expand Down Expand Up @@ -239,17 +240,27 @@ function Calendar:render()
vim.api.nvim_buf_set_lines(self.buf, 0, -1, true, content)
vim.api.nvim_buf_clear_namespace(self.buf, namespace, 0, -1)
if self.clearable then
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 3, 0, -1)
local range = Range:new({
start_line = #content - 2,
start_col = 0,
end_line = #content - 2,
end_col = 1,
})
colors.highlight({
range = range,
hlgroup = 'Comment',
}, self.buf)
self:_apply_hl('Comment', #content - 3, 0, -1)
end

if not self:has_time() then
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 8, 0, -1)
self:_apply_hl('Comment', 8, 0, -1)
end

vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 4, 0, -1)
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 3, 0, -1)
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 2, 0, -1)
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 1, 0, -1)
self:_apply_hl('Comment', #content - 4, 0, -1)
self:_apply_hl('Comment', #content - 3, 0, -1)
self:_apply_hl('Comment', #content - 2, 0, -1)
self:_apply_hl('Comment', #content - 1, 0, -1)

for i, line in ipairs(content) do
local from = 0
Expand All @@ -274,14 +285,28 @@ function Calendar:render()
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf })
end

function Calendar:_apply_hl(hl_group, start_line, start_col, end_col)
local range = Range:new({
start_line = start_line + 1,
start_col = start_col + 1,
end_line = start_line + 1,
end_col = end_col > 0 and end_col + 1 or end_col,
})
colors.highlight({
namespace = namespace,
range = range,
hlgroup = hl_group,
}, self.buf)
end

---@param day OrgDate
---@param opts { from: number, to: number, line: number}
function Calendar:on_render_day(day, opts)
if day:is_today() then
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarToday', opts.line - 1, opts.from - 1, opts.to)
self:_apply_hl('OrgCalendarToday', opts.line - 1, opts.from - 1, opts.to)
end
if day:is_same_day(self.date) then
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarSelected', opts.line - 1, opts.from - 1, opts.to)
self:_apply_hl('OrgCalendarSelected', opts.line - 1, opts.from - 1, opts.to)
end
if self.on_day then
self.on_day(
Expand Down Expand Up @@ -324,12 +349,12 @@ function Calendar:rerender_time()
else
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [d] - select day [T] - clear time' })
end
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Normal', 8, 0, -1)
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 13, 0, -1)
self:_apply_hl('Normal', 8, 0, -1)
self:_apply_hl('Comment', 13, 0, -1)
else
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [t] - enter time' })
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 8, 0, -1)
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 13, 0, -1)
self:_apply_hl('Comment', 8, 0, -1)
self:_apply_hl('Comment', 13, 0, -1)
end
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf })
end
Expand Down

0 comments on commit b56ece9

Please sign in to comment.