Skip to content

refactor(agenda): use extmarks to set tags in right column #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lua/orgmode/agenda/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ local Promise = require('orgmode.utils.promise')
---@field views table[]
---@field filters OrgAgendaFilter
---@field files OrgFiles
local Agenda = {}
local Agenda = {
_ns_id = vim.api.nvim_create_namespace('orgmode.ui.agenda'),
}

---@param opts? table
function Agenda:new(opts)
Expand Down Expand Up @@ -161,11 +163,24 @@ function Agenda:_render(skip_rebuild)
vim.w.org_window_pos = nil
end
end
local lines = vim.tbl_map(function(item)
return item.line_content
end, self.content)
vim.bo.modifiable = true
vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
for index, item in ipairs(self.content) do
vim.api.nvim_buf_set_lines(0, index - 1, -1, true, { item.line_content })
if item.headline and #item.headline:get_tags() > 0 then
-- Get the first highlight group from the given item and apply it to the tag if available. If
-- not use the generic `Normal` hl group
local extmark_hls = {
(item.highlights and item.highlights[1] and item.highlights[1].hlgroup) and item.highlights[1].hlgroup
or 'Normal',
'@org.agenda.tag',
}
vim.api.nvim_buf_set_extmark(0, self._ns_id, index - 1, 0, {
virt_text = { { item.headline:tags_to_string(), extmark_hls } },
virt_text_pos = 'right_align',
hl_mode = 'combine',
})
end
end
vim.bo.modifiable = false
vim.bo.modified = false
colors.highlight(self.highlights, true)
Expand Down
6 changes: 0 additions & 6 deletions lua/orgmode/agenda/views/agenda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ function AgendaView.build_agenda_item_content(agenda_item, longest_category, lon
todo_keyword = todo_padding .. todo_keyword
local line = string.format('%s%s%s %s', category, date, todo_keyword, headline:get_title_with_priority())
local todo_keyword_pos = string.format('%s%s%s', category, date, todo_padding):len()
if #headline:get_tags() > 0 then
local tags_string = headline:tags_to_string()
local padding_length = math.max(1, win_width - vim.api.nvim_strwidth(line) - vim.api.nvim_strwidth(tags_string))
local indent = string.rep(' ', padding_length)
line = string.format('%s%s%s', line, indent, tags_string)
end

local item_highlights = {}
if #agenda_item.highlights then
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ function OrgMappings:meta_return(suffix)
end

if #text_edits > 0 then
vim.lsp.util.apply_text_edits(text_edits, 0, constants.default_offset_encoding)
vim.lsp.util.apply_text_edits(text_edits, vim.api.nvim_get_current_buf(), constants.default_offset_encoding)

vim.fn.cursor(end_row + 1 + (add_empty_line and 1 or 0), 1) -- +1 for next line

Expand Down