File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ function Listitem:update_checkbox(action)
7171 else
7272 local parent_headline = self .file :get_closest_headline_or_nil ()
7373 if parent_headline then
74- parent_headline :update_cookie ( parent_list )
74+ parent_headline :update_cookies ( )
7575 end
7676 end
7777end
Original file line number Diff line number Diff line change @@ -908,6 +908,39 @@ function Headline:update_cookie(list_node)
908908 end
909909end
910910
911+ function Headline :update_cookies ()
912+ local section = self :node ():parent ()
913+ if not section then
914+ return nil
915+ end
916+
917+ -- go through all the lists in this headline and gather checked_boxes
918+ local num_boxes , num_checked_boxes = 0 , 0
919+ local body = section :field (' body' )[1 ]
920+ for node in body :iter_children () do
921+ if node :type () == ' list' then
922+ local boxes = self :child_checkboxes (node )
923+ num_boxes = num_boxes + # boxes
924+ local checked_boxes = vim .tbl_filter (function (box )
925+ return box :match (' %[%w%]' )
926+ end , boxes )
927+ num_checked_boxes = num_checked_boxes + # checked_boxes
928+ end
929+ end
930+
931+ -- update the cookie
932+ local cookie = self :get_cookie ()
933+ if cookie then
934+ local new_cookie_val
935+ if self .file :get_node_text (cookie ):find (' %%' ) then
936+ new_cookie_val = (' [%d%%]' ):format ((num_checked_boxes / num_boxes ) * 100 )
937+ else
938+ new_cookie_val = (' [%d/%d]' ):format (num_checked_boxes , num_boxes )
939+ end
940+ return self :_set_node_text (cookie , new_cookie_val )
941+ end
942+ end
943+
911944function Headline :child_checkboxes (list_node )
912945 return vim .tbl_map (function (node )
913946 local text = self .file :get_node_text (node )
You can’t perform that action at this time.
0 commit comments