Skip to content

Commit 4ce3262

Browse files
Fix adding closed date when marking non-scheduled headline as done. Fixes #286.
1 parent 92cb84e commit 4ce3262

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

lua/orgmode/config/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,12 @@ function Config:respect_blank_before_new_entry(content, option, prepend_content)
303303
return content
304304
end
305305

306+
function Config:get_indent(amount)
307+
if self.opts.org_indent_mode == 'indent' then
308+
return string.rep(' ', amount)
309+
end
310+
return ''
311+
end
312+
306313
instance = Config:new()
307314
return instance

lua/orgmode/parser/logbook.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ function Logbook.new_from_section(section)
154154
else
155155
line = section:has_planning() and section.range.start_line + 1 or section.range.start_line
156156
end
157-
local indent = ''
158-
if config.org_indent_mode == 'indent' then
159-
indent = string.rep(' ', section.level + 1)
160-
end
157+
local indent = config:get_indent(section.level + 1)
161158

162159
local date = Date.now({ active = false })
163160
local content = {

lua/orgmode/parser/section.lua

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,7 @@ function Section:add_properties(properties)
389389
end
390390

391391
local properties_line = self:has_planning() and self.range.start_line + 1 or self.range.start_line
392-
local indent = ''
393-
if config.org_indent_mode == 'indent' then
394-
indent = string.rep(' ', self.level + 1)
395-
end
392+
local indent = config:get_indent(self.level + 1)
396393
local content = { string.format('%s:PROPERTIES:', indent) }
397394

398395
for name, val in pairs(properties) do
@@ -673,10 +670,7 @@ function Section:_add_planning_date(date, type, active)
673670
})
674671
end
675672

676-
local indent = ''
677-
if config.org_indent_mode == 'indent' then
678-
indent = string.rep(' ', self.level + 1)
679-
end
673+
local indent = config:get_indent(self.level + 1)
680674
return vim.api.nvim_call_function('append', {
681675
self.range.start_line,
682676
string.format('%s%s: %s', indent, type, date_string),

lua/orgmode/treesitter/headline.lua

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local ts_utils = require('nvim-treesitter.ts_utils')
22
local tree_utils = require('orgmode.utils.treesitter')
3+
local Date = require('orgmode.objects.date')
34
local config = require('orgmode.config')
45
local query = vim.treesitter.query
56

67
local Headline = {}
78

8-
---@param headline userdata tree sitter headline node
9+
---@param headline_node userdata tree sitter headline node
910
function Headline:new(headline_node)
1011
local data = { headline = headline_node }
1112
setmetatable(data, self)
@@ -17,6 +18,12 @@ function Headline:stars()
1718
return self.headline:field('stars')[1]
1819
end
1920

21+
---@return number
22+
function Headline:level()
23+
local stars = self:stars()
24+
return query.get_node_text(stars, 0):len()
25+
end
26+
2027
function Headline:priority()
2128
return self:parse('%[#(%w+)%]')
2229
end
@@ -83,6 +90,11 @@ end
8390
function Headline:dates()
8491
local plan = self:plan()
8592
local dates = {}
93+
94+
if not plan then
95+
return dates
96+
end
97+
8698
for _, node in ipairs(ts_utils.get_named_children(plan)) do
8799
local name = vim.treesitter.query.get_node_text(node:named_child(0), 0)
88100
dates[name] = node
@@ -103,12 +115,21 @@ end
103115

104116
function Headline:add_closed_date()
105117
local dates = self:dates()
106-
if vim.tbl_count(dates) == 0 or dates['CLOSED'] then
118+
if dates['CLOSED'] then
107119
return
108120
end
121+
local closed_text = 'CLOSED: ' .. Date.now():to_wrapped_string(false)
122+
if vim.tbl_isempty(dates) then
123+
local indent = config:get_indent(self:level() + 1)
124+
local start_line = self.headline:start()
125+
return vim.api.nvim_call_function('append', {
126+
start_line + 1,
127+
string.format('%s%s', indent, closed_text),
128+
})
129+
end
109130
local last_child = dates['DEADLINE'] or dates['SCHEDULED']
110131
local ptext = query.get_node_text(last_child, 0)
111-
local text = ptext .. ' CLOSED: [' .. vim.fn.strftime('%Y-%m-%d %a %H:%M') .. ']'
132+
local text = ptext .. ' ' .. closed_text
112133
tree_utils.set_node_text(last_child, text)
113134
end
114135

@@ -117,7 +138,11 @@ function Headline:remove_closed_date()
117138
if vim.tbl_count(dates) == 0 or not dates['CLOSED'] then
118139
return
119140
end
141+
local line_nr = dates['CLOSED']:start() + 1
120142
tree_utils.set_node_text(dates['CLOSED'], '', true)
143+
if vim.trim(vim.fn.getline(line_nr)) == '' then
144+
return vim.api.nvim_call_function('deletebufline', { vim.api.nvim_get_current_buf(), line_nr })
145+
end
121146
end
122147

123148
-- @return tsnode, string

0 commit comments

Comments
 (0)