@@ -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' )
@@ -891,35 +893,53 @@ function Headline:get_cookie()
891
893
end
892
894
893
895
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
896
899
return self
897
900
end
898
901
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
910
918
end
911
919
end
912
920
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
+
913
929
-- 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 ()
923
943
end
924
944
return self
925
945
end
0 commit comments