Skip to content

Commit c7f3bfc

Browse files
committed
feat(files): add method OrgHeadline:toggle_tag()
1 parent 6e2f699 commit c7f3bfc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lua/orgmode/files/headline.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,28 @@ function Headline:set_tags(tags)
288288
vim.api.nvim_buf_set_text(bufnr, pred_end_row, pred_end_col, pred_end_row, end_col, { text })
289289
end
290290

291+
---@param tag string
292+
---@param onoff? boolean true=>add, false=>remove, nil=>toggle the tag
293+
function Headline:toggle_tag(tag, onoff)
294+
local current_tags = self:get_own_tags()
295+
296+
local present = vim.tbl_contains(current_tags, tag)
297+
if onoff == nil then
298+
onoff = not present
299+
end
300+
301+
if onoff and not present then
302+
table.insert(current_tags, tag)
303+
elseif not onoff and present then
304+
current_tags = vim.tbl_filter(function(i)
305+
return i ~= tag
306+
end, current_tags)
307+
end
308+
309+
self:set_tags(utils.tags_to_string(current_tags))
310+
return onoff
311+
end
312+
291313
function Headline:align_tags()
292314
local own_tags, node = self:get_own_tags()
293315
if node then

0 commit comments

Comments
 (0)