Skip to content

Commit b56ece9

Browse files
refactor(highlights): Use nvim_buf_set_extmark instead of nvim_buf_add_highlight
nvim_buf_add_highlights is deprecated in Nvim 0.11
1 parent 6566b69 commit b56ece9

File tree

6 files changed

+85
-46
lines changed

6 files changed

+85
-46
lines changed

lua/orgmode/agenda/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function Agenda:_open_window()
234234
})
235235
if ft == 'orgagenda' then
236236
vim.bo[buf].modifiable = true
237-
colors.highlight({}, true, buf)
237+
colors.apply_highlights({}, true, buf)
238238
vim.api.nvim_buf_set_lines(buf, 0, -1, true, {})
239239
return buf
240240
end

lua/orgmode/agenda/view/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function OrgAgendaView:render()
6969
vim.list_extend(virt_texts, compiled.virt_texts)
7070
end
7171
vim.api.nvim_buf_set_lines(self.bufnr, self.start_line - 1, self.end_line - 1, false, lines)
72-
colors.highlight(highlights, false, self.bufnr)
72+
colors.apply_highlights(highlights, false, self.bufnr)
7373
colors.virtual_text(virt_texts, self.bufnr)
7474

7575
return self

lua/orgmode/agenda/view/line.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function OrgAgendaLine:render()
126126
vim.bo[bufnr].modifiable = true
127127
vim.api.nvim_buf_set_lines(bufnr, self.line_nr - 1, self.line_nr, false, { compiled.content })
128128
vim.bo[bufnr].modifiable = false
129-
colors.highlight(compiled.highlights, false, bufnr)
129+
colors.apply_highlights(compiled.highlights, false, bufnr)
130130
colors.virtual_text(compiled.virt_texts, bufnr)
131131
end
132132

lua/orgmode/colors/highlighter/stars.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function OrgStars:on_line(bufnr, line)
3232
local _, end_col = node:end_()
3333

3434
vim.api.nvim_buf_set_extmark(bufnr, self.highlighter.namespace, line, 0, {
35-
end_line = line,
35+
end_row = line,
3636
end_col = end_col - 1,
3737
hl_group = '@org.leading_stars',
3838
ephemeral = true,

lua/orgmode/colors/init.lua

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,56 @@ M.get_todo_keywords_colors = function()
7070
}
7171
end
7272

73-
---@param highlights table[]
73+
---@class OrgHighlightEntry
74+
---@field range OrgRange
75+
---@field hlgroup string
76+
---@field namespace? number
77+
---@field spell? boolean
78+
---@field priority? number
79+
---@field conceal? boolean
80+
---@field url? string
81+
---@field whole_line? boolean
82+
83+
---@param highlights OrgHighlightEntry[]
7484
---@param clear? boolean
7585
---@return string
76-
M.highlight = function(highlights, clear, bufnr)
86+
M.apply_highlights = function(highlights, clear, bufnr)
7787
bufnr = bufnr or 0
7888
if clear then
7989
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
8090
end
8191
for _, hl in ipairs(highlights) do
82-
if hl.whole_line then
83-
vim.api.nvim_buf_set_extmark(bufnr, namespace, hl.range.start_line - 1, hl.range.start_col - 1, {
84-
hl_group = hl.hlgroup,
85-
end_line = hl.range.start_line,
86-
hl_eol = true,
87-
})
88-
elseif hl.extmark then
89-
vim.api.nvim_buf_set_extmark(bufnr, namespace, hl.range.start_line - 1, hl.range.start_col - 1, {
90-
hl_group = hl.hlgroup,
91-
end_line = hl.range.end_line - 1,
92-
end_col = hl.range.end_col - 1,
93-
spell = hl.spell,
94-
priority = hl.priority,
95-
conceal = hl.conceal,
96-
url = hl.url,
97-
})
98-
else
99-
vim.api.nvim_buf_add_highlight(
100-
bufnr,
101-
namespace,
102-
hl.hlgroup,
103-
hl.range.start_line - 1,
104-
hl.range.start_col - 1,
105-
hl.range.end_col - 1
106-
)
107-
end
92+
M.highlight(hl, bufnr)
93+
end
94+
end
95+
96+
---@param hl OrgHighlightEntry
97+
M.highlight = function(hl, bufnr)
98+
bufnr = bufnr or 0
99+
local start_row = hl.range.start_line - 1
100+
local start_col = hl.range.start_col - 1
101+
local opts = {
102+
hl_group = hl.hlgroup,
103+
end_row = hl.range.end_line - 1,
104+
end_col = hl.range.end_col - 1,
105+
spell = hl.spell,
106+
priority = hl.priority,
107+
conceal = hl.conceal,
108+
url = hl.url,
109+
}
110+
111+
if hl.whole_line then
112+
opts.end_row = start_row + 1
113+
opts.end_col = 0
114+
opts.hl_eol = true
108115
end
116+
117+
if opts.end_col < 0 then
118+
opts.end_col = 99999
119+
opts.strict = false
120+
end
121+
122+
return vim.api.nvim_buf_set_extmark(bufnr, hl.namespace or namespace, start_row, start_col, opts)
109123
end
110124

111125
---@param virt_texts{ range: OrgRange, content: string, virt_text_pos: string, hl_groups: string[] }[]
@@ -130,7 +144,7 @@ M.clear_extmarks = function(bufnr, start_line, end_line)
130144
bufnr,
131145
namespace,
132146
{ start_line, 0 },
133-
{ end_line, 9999 },
147+
{ end_line, 99999 },
134148
{ details = true }
135149
)
136150
for _, extmark in ipairs(extmarks) do

lua/orgmode/objects/calendar.lua

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local utils = require('orgmode.utils')
33
local Promise = require('orgmode.utils.promise')
44
local config = require('orgmode.config')
55
local namespace = vim.api.nvim_create_namespace('org_calendar')
6+
local colors = require('orgmode.colors')
7+
local Range = require('orgmode.files.elements.range')
68

79
---@alias OrgCalendarOnRenderDayOpts { line: number, from: number, to: number, buf: number, namespace: number }
810
---@alias OrgCalendarOnRenderDay fun(day: OrgDate, opts: OrgCalendarOnRenderDayOpts)
@@ -15,7 +17,6 @@ local small_minute_step = config.calendar.min_small_step or config.org_time_stam
1517
---@field win number?
1618
---@field buf number?
1719
---@field callback fun(date: OrgDate | nil, cleared?: boolean)
18-
---@field namespace function
1920
---@field date OrgDate?
2021
---@field title? string
2122
---@field on_day? OrgCalendarOnRenderDay
@@ -239,17 +240,27 @@ function Calendar:render()
239240
vim.api.nvim_buf_set_lines(self.buf, 0, -1, true, content)
240241
vim.api.nvim_buf_clear_namespace(self.buf, namespace, 0, -1)
241242
if self.clearable then
242-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 3, 0, -1)
243+
local range = Range:new({
244+
start_line = #content - 2,
245+
start_col = 0,
246+
end_line = #content - 2,
247+
end_col = 1,
248+
})
249+
colors.highlight({
250+
range = range,
251+
hlgroup = 'Comment',
252+
}, self.buf)
253+
self:_apply_hl('Comment', #content - 3, 0, -1)
243254
end
244255

245256
if not self:has_time() then
246-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 8, 0, -1)
257+
self:_apply_hl('Comment', 8, 0, -1)
247258
end
248259

249-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 4, 0, -1)
250-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 3, 0, -1)
251-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 2, 0, -1)
252-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 1, 0, -1)
260+
self:_apply_hl('Comment', #content - 4, 0, -1)
261+
self:_apply_hl('Comment', #content - 3, 0, -1)
262+
self:_apply_hl('Comment', #content - 2, 0, -1)
263+
self:_apply_hl('Comment', #content - 1, 0, -1)
253264

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

288+
function Calendar:_apply_hl(hl_group, start_line, start_col, end_col)
289+
local range = Range:new({
290+
start_line = start_line + 1,
291+
start_col = start_col + 1,
292+
end_line = start_line + 1,
293+
end_col = end_col > 0 and end_col + 1 or end_col,
294+
})
295+
colors.highlight({
296+
namespace = namespace,
297+
range = range,
298+
hlgroup = hl_group,
299+
}, self.buf)
300+
end
301+
277302
---@param day OrgDate
278303
---@param opts { from: number, to: number, line: number}
279304
function Calendar:on_render_day(day, opts)
280305
if day:is_today() then
281-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarToday', opts.line - 1, opts.from - 1, opts.to)
306+
self:_apply_hl('OrgCalendarToday', opts.line - 1, opts.from - 1, opts.to)
282307
end
283308
if day:is_same_day(self.date) then
284-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarSelected', opts.line - 1, opts.from - 1, opts.to)
309+
self:_apply_hl('OrgCalendarSelected', opts.line - 1, opts.from - 1, opts.to)
285310
end
286311
if self.on_day then
287312
self.on_day(
@@ -324,12 +349,12 @@ function Calendar:rerender_time()
324349
else
325350
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [d] - select day [T] - clear time' })
326351
end
327-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Normal', 8, 0, -1)
328-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 13, 0, -1)
352+
self:_apply_hl('Normal', 8, 0, -1)
353+
self:_apply_hl('Comment', 13, 0, -1)
329354
else
330355
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [t] - enter time' })
331-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 8, 0, -1)
332-
vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', 13, 0, -1)
356+
self:_apply_hl('Comment', 8, 0, -1)
357+
self:_apply_hl('Comment', 13, 0, -1)
333358
end
334359
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf })
335360
end

0 commit comments

Comments
 (0)