Skip to content

Commit e63d52f

Browse files
committed
implement adding/removing close stamp
1 parent 6d29ea9 commit e63d52f

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,8 @@ function OrgMappings:toggle_heading()
366366
end
367367

368368
function OrgMappings:_todo_change_state(direction)
369-
local item = Files.get_closest_headline()
370-
local was_done = item:is_done()
371-
local old_state = item.todo_keyword.value
369+
local headline = tree_utils.closest_headline()
370+
local _, old_state, was_done = tree_utils.get_todo(headline)
372371
local changed = self:_change_todo_state(direction, true)
373372
if not changed then
374373
return
@@ -382,10 +381,10 @@ function OrgMappings:_todo_change_state(direction)
382381
if #repeater_dates == 0 then
383382
local log_time = config.org_log_done == 'time'
384383
if log_time and item:is_done() and not was_done then
385-
item:add_closed_date()
384+
tree_utils.add_closed_date(headline)
386385
end
387386
if log_time and not item:is_done() and was_done then
388-
item:remove_closed_date()
387+
tree_utils.remove_closed_date(headline)
389388
end
390389
return item
391390
end

lua/orgmode/utils/treesitter.lua

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,27 @@ end
4646
function M.get_todo(headline)
4747
local keywords = config.todo_keywords.ALL
4848
local done_keywords = config.todo_keywords.DONE
49-
local todo_node = nil
50-
local keyword = nil
51-
local is_done = nil
5249
for _, word in ipairs(keywords) do
5350
local todo = parse_item(headline, string.gsub(word, '-', '%%-'))
5451
if todo then
55-
todo_node = todo
56-
keyword = word
57-
is_done = vim.tbl_contains(done_keywords, word)
58-
break
52+
return todo, word, vim.tbl_contains(done_keywords, word)
5953
end
6054
end
61-
return todo_node, keyword, is_done
6255
end
6356

6457
function M.get_stars(headline)
6558
return headline:field('stars')[1]
6659
end
6760

68-
function M.set_node_text(node, text)
61+
-- @param front_trim boolean
62+
function M.set_node_text(node, text, front_trim)
6963
local sr, sc, er, ec = node:range()
7064
if string.len(text) == 0 then
71-
ec = ec + 1
65+
if front_trim then
66+
sc = sc - 1
67+
else
68+
ec = ec + 1
69+
end
7270
end
7371
vim.api.nvim_buf_set_text(0, sr, sc, er, ec, { text })
7472
end
@@ -105,4 +103,53 @@ function M.set_todo(headline, keyword)
105103
M.set_node_text(stars, string.format('%s %s', text, keyword))
106104
end
107105

106+
function M.get_plan(headline)
107+
local section = headline:parent()
108+
for _, node in ipairs(ts_utils.get_named_children(section)) do
109+
if node:type() == 'plan' then
110+
return node
111+
end
112+
end
113+
end
114+
115+
function M.get_dates(headline)
116+
local plan = M.get_plan(headline)
117+
local dates = {}
118+
for _, node in ipairs(ts_utils.get_named_children(plan)) do
119+
local name = vim.treesitter.query.get_node_text(node:named_child(0), 0)
120+
dates[name] = node
121+
end
122+
return dates
123+
end
124+
125+
function M.repeater_dates(headline)
126+
return vim.tbl_filter(function(entry)
127+
local timestamp = entry:field('timestamp')[1]
128+
for _, node in ipairs(ts_utils.get_named_children(timestamp)) do
129+
if node:type() == 'repeat' then
130+
return true
131+
end
132+
end
133+
end, M.get_dates(headline))
134+
end
135+
136+
function M.add_closed_date(headline)
137+
local dates = M.get_dates(headline)
138+
if vim.tbl_count(dates) == 0 or dates['CLOSED'] then
139+
return
140+
end
141+
local last_child = dates['DEADLINE'] or dates['SCHEDULED']
142+
local ptext = vim.treesitter.query.get_node_text(last_child, 0)
143+
local text = ptext .. ' CLOSED: [' .. vim.fn.strftime('%Y-%m-%d %a %H:%M') .. ']'
144+
M.set_node_text(last_child, text)
145+
end
146+
147+
function M.remove_closed_date(headline)
148+
local dates = M.get_dates(headline)
149+
if vim.tbl_count(dates) == 0 or not dates['CLOSED'] then
150+
return
151+
end
152+
M.set_node_text(dates['CLOSED'], '', true)
153+
end
154+
108155
return M

0 commit comments

Comments
 (0)