@@ -357,12 +357,14 @@ end
357
357
function Headline :set_todo (keyword )
358
358
local todo , node = self :get_todo ()
359
359
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 ()
361
362
end
362
363
363
364
local stars = self :_get_child_node (' stars' )
364
365
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 ()
366
368
end
367
369
368
370
memoize (' get_todo' )
@@ -904,28 +906,47 @@ function Headline:set_cookie(num, denum)
904
906
return self
905
907
end
906
908
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
910
912
return self
911
913
end
912
914
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
924
937
end
925
938
end
926
939
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
+
927
948
-- Update the cookie
928
- return self :set_cookie (num_checked_boxes , num_boxes )
949
+ return self :set_cookie (num , denum )
929
950
end
930
951
931
952
function Headline :child_checkboxes (list_node )
0 commit comments