Skip to content

Commit 5d7c03a

Browse files
seflueSebastian Flügge
and
Sebastian Flügge
authored
fix(statistics cookie): count only TODO headlines (#938)
Previously, the todo statistics cookie was counting all child headlines in its calculation, regardless of whether they had TODO keywords. This fix modifies the `update_todo_cookie` function to: 1. Filter child headlines to only those with valid TODO keywords 2. Calculate the ratio of DONE tasks to total tasks with TODO keywords 3. Update the cookie with the correct statistics This ensures consistency with Org Mode's behavior where non-task headlines are excluded from statistics calculations. Co-authored-by: Sebastian Flügge <sebastian.fluegge@dnv.com>
1 parent abf8890 commit 5d7c03a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lua/orgmode/files/headline.lua

+8-3
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,19 @@ function Headline:update_todo_cookie()
946946
return self
947947
end
948948

949-
-- Count done children headlines
949+
-- Count done children headlines and total children with TODO keywords
950950
local children = self:get_child_headlines()
951+
local headlines_with_todo = vim.tbl_filter(function(h)
952+
local todo, _, _ = h:get_todo()
953+
return todo ~= nil
954+
end, children)
955+
951956
local dones = vim.tbl_filter(function(h)
952957
return h:is_done()
953-
end, children)
958+
end, headlines_with_todo)
954959

955960
-- Set the cookie
956-
return self:_set_cookie(cookie, #dones, #children)
961+
return self:_set_cookie(cookie, #dones, #headlines_with_todo)
957962
end
958963

959964
function Headline:update_parent_cookie()

tests/plenary/ui/mappings/todo_spec.lua

+21
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,25 @@ describe('Todo mappings', function()
430430
'** TODO item',
431431
}, vim.api.nvim_buf_get_lines(0, 0, 6, false))
432432
end)
433+
it('should update headline cookies when children todo state changes', function()
434+
helpers.create_file({
435+
'* Test orgmode [/]',
436+
'** TODO item',
437+
'** TODO item',
438+
'** TODO item',
439+
'** Non-todo item',
440+
})
441+
vim.fn.cursor(4, 1)
442+
local now = Date.now()
443+
-- Changing to DONE and adding closed date
444+
vim.cmd([[norm citd]])
445+
assert.are.same({
446+
'* Test orgmode [1/3]',
447+
'** TODO item',
448+
'** TODO item',
449+
'** DONE item',
450+
' CLOSED: [' .. now:to_string() .. ']',
451+
'** Non-todo item',
452+
}, vim.api.nvim_buf_get_lines(0, 0, 6, false))
453+
end)
433454
end)

0 commit comments

Comments
 (0)