Skip to content

Commit d756b38

Browse files
committed
update parent cookie from todo
1 parent 22094c4 commit d756b38

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

lua/orgmode/files/headline.lua

+44-24
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')
@@ -891,35 +893,53 @@ function Headline:get_cookie()
891893
end
892894

893895
function Headline:update_cookie()
894-
local section = self:node():parent()
895-
if not section then
896+
-- Return early if the headline doesn't have a cookie
897+
local cookie = self:get_cookie()
898+
if not cookie then
896899
return self
897900
end
898901

899-
-- Go through all the lists in this headline and gather checked_boxes
900-
local num_boxes, num_checked_boxes = 0, 0
901-
local body = section:field('body')[1]
902-
for node in body:iter_children() do
903-
if node:type() == 'list' then
904-
local boxes = self:child_checkboxes(node)
905-
num_boxes = num_boxes + #boxes
906-
local checked_boxes = vim.tbl_filter(function(box)
907-
return box:match('%[%w%]')
908-
end, boxes)
909-
num_checked_boxes = num_checked_boxes + #checked_boxes
902+
local num, denum = 0, 0
903+
-- Count checked boxes from all lists
904+
local section = self:node():parent()
905+
if section then
906+
local body = section:field('body')[1]
907+
if body then
908+
for node in body:iter_children() do
909+
if node:type() == 'list' then
910+
local boxes = self:child_checkboxes(node)
911+
denum = denum + #boxes
912+
local checked_boxes = vim.tbl_filter(function(box)
913+
return box:match('%[%w%]')
914+
end, boxes)
915+
num = num + #checked_boxes
916+
end
917+
end
910918
end
911919
end
912920

921+
-- Count done children headlines
922+
local children = self:get_child_headlines()
923+
local dones = vim.tbl_filter(function(h)
924+
return h:is_done()
925+
end, children)
926+
num = num + #dones
927+
denum = denum + #children
928+
913929
-- Update the cookie
914-
local cookie = self:get_cookie()
915-
if cookie then
916-
local new_cookie_val
917-
if self.file:get_node_text(cookie):find('%%') then
918-
new_cookie_val = ('[%d%%]'):format((num_checked_boxes / num_boxes) * 100)
919-
else
920-
new_cookie_val = ('[%d/%d]'):format(num_checked_boxes, num_boxes)
921-
end
922-
return self:_set_node_text(cookie, new_cookie_val)
930+
local new_cookie_val
931+
if self.file:get_node_text(cookie):find('%%') then
932+
new_cookie_val = ('[%d%%]'):format((num / denum) * 100)
933+
else
934+
new_cookie_val = ('[%d/%d]'):format(num, denum)
935+
end
936+
return self:_set_node_text(cookie, new_cookie_val)
937+
end
938+
939+
function Headline:update_parent_cookie_from_todo()
940+
local parent = self:get_parent_headline()
941+
if parent and parent.headline then
942+
parent:update_cookie()
923943
end
924944
return self
925945
end

0 commit comments

Comments
 (0)