Skip to content

Commit 56cc488

Browse files
committed
update parent cookie from todo
1 parent 380df4b commit 56cc488

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

lua/orgmode/files/headline.lua

+38-17
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ end
357357
function Headline:set_todo(keyword)
358358
local todo, node = self:get_todo()
359359
if todo then
360-
return self:_set_node_text(node, keyword)
360+
self:_set_node_text(node, keyword)
361+
return self:update_parent_cookie_from_todo()
361362
end
362363

363364
local stars = self:_get_child_node('stars')
364365
local _, level = stars:end_()
365-
return self:_set_node_text(stars, ('%s %s'):format(('*'):rep(level), keyword))
366+
self:_set_node_text(stars, ('%s %s'):format(('*'):rep(level), keyword))
367+
return self:update_parent_cookie_from_todo()
366368
end
367369

368370
memoize('get_todo')
@@ -904,28 +906,47 @@ function Headline:set_cookie(num, denum)
904906
return self
905907
end
906908

907-
function Headline:update_cookie()
908-
local section = self:node():parent()
909-
if not section then
909+
function Headline:update_parent_cookie_from_todo()
910+
local parent = self:get_parent_headline()
911+
if not parent or not parent.headline then
910912
return self
911913
end
912914

913-
-- Go through all the lists in this headline and gather checked_boxes
914-
local num_boxes, num_checked_boxes = 0, 0
915-
local body = section:field('body')[1]
916-
for node in body:iter_children() do
917-
if node:type() == 'list' then
918-
local boxes = self:child_checkboxes(node)
919-
num_boxes = num_boxes + #boxes
920-
local checked_boxes = vim.tbl_filter(function(box)
921-
return box:match('%[%w%]')
922-
end, boxes)
923-
num_checked_boxes = num_checked_boxes + #checked_boxes
915+
-- Update the cookie
916+
return parent:update_cookie()
917+
end
918+
919+
function Headline:update_cookie()
920+
local num, denum = 0, 0
921+
922+
-- Count checked boxes from all lists
923+
local section = self:node():parent()
924+
if section then
925+
local body = section:field('body')[1]
926+
if body then
927+
for node in body:iter_children() do
928+
if node:type() == 'list' then
929+
local boxes = self:child_checkboxes(node)
930+
denum = denum + #boxes
931+
local checked_boxes = vim.tbl_filter(function(box)
932+
return box:match('%[%w%]')
933+
end, boxes)
934+
num = num + #checked_boxes
935+
end
936+
end
924937
end
925938
end
926939

940+
-- Count done children headlines
941+
local peers = self:get_child_headlines()
942+
local dones = vim.tbl_filter(function(h)
943+
return h:is_done()
944+
end, peers)
945+
num = num + #dones
946+
denum = denum + #peers
947+
927948
-- Update the cookie
928-
return self:set_cookie(num_checked_boxes, num_boxes)
949+
return self:set_cookie(num, denum)
929950
end
930951

931952
function Headline:child_checkboxes(list_node)

0 commit comments

Comments
 (0)