Skip to content

Commit c39601e

Browse files
committed
refactor(agenda): use extmarks to set tags in right column
1 parent 80314df commit c39601e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lua/orgmode/agenda/init.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ local Promise = require('orgmode.utils.promise')
1717
---@field views table[]
1818
---@field filters OrgAgendaFilter
1919
---@field files OrgFiles
20-
local Agenda = {}
20+
local Agenda = {
21+
_ns_id = vim.api.nvim_create_namespace('orgmode.ui.agenda'),
22+
}
2123

2224
---@param opts? table
2325
function Agenda:new(opts)
@@ -161,11 +163,24 @@ function Agenda:_render(skip_rebuild)
161163
vim.w.org_window_pos = nil
162164
end
163165
end
164-
local lines = vim.tbl_map(function(item)
165-
return item.line_content
166-
end, self.content)
167166
vim.bo.modifiable = true
168-
vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
167+
for index, item in ipairs(self.content) do
168+
vim.api.nvim_buf_set_lines(0, index - 1, -1, true, { item.line_content })
169+
if item.headline and #item.headline:get_tags() > 0 then
170+
-- Get the first highlight group from the given item and apply it to the tag if available. If
171+
-- not use the generic `Normal` hl group
172+
local extmark_hls = {
173+
(item.highlights and item.highlights[1] and item.highlights[1].hlgroup) and item.highlights[1].hlgroup
174+
or 'Normal',
175+
'@org.agenda.tag',
176+
}
177+
vim.api.nvim_buf_set_extmark(0, self._ns_id, index - 1, 0, {
178+
virt_text = { { item.headline:tags_to_string(), extmark_hls } },
179+
virt_text_pos = 'right_align',
180+
hl_mode = 'combine',
181+
})
182+
end
183+
end
169184
vim.bo.modifiable = false
170185
vim.bo.modified = false
171186
colors.highlight(self.highlights, true)

lua/orgmode/agenda/views/agenda.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,6 @@ function AgendaView.build_agenda_item_content(agenda_item, longest_category, lon
293293
todo_keyword = todo_padding .. todo_keyword
294294
local line = string.format('%s%s%s %s', category, date, todo_keyword, headline:get_title_with_priority())
295295
local todo_keyword_pos = string.format('%s%s%s', category, date, todo_padding):len()
296-
if #headline:get_tags() > 0 then
297-
local tags_string = headline:tags_to_string()
298-
local padding_length = math.max(1, win_width - vim.api.nvim_strwidth(line) - vim.api.nvim_strwidth(tags_string))
299-
local indent = string.rep(' ', padding_length)
300-
line = string.format('%s%s%s', line, indent, tags_string)
301-
end
302296

303297
local item_highlights = {}
304298
if #agenda_item.highlights then

0 commit comments

Comments
 (0)