@@ -43,15 +43,41 @@ def __init__(self, markdown_file: Path) -> None:
4343 with self ._filesystem_ids_path .open ("rb" ) as f :
4444 self ._ids_map = pickle .load (f )
4545
46+ all_items = self .get_all_items (include_non_tasks = True )
47+
48+ # dict with items. Ignore lines with no tasks
4649 self ._items_cache : dict [str , dict ] = {
47- str (item .id ): item for item in self . get_all_items ()
50+ str (item .id ): item for item in all_items if item
4851 }
4952
53+ # Array with item ids in the same order found in the .md file
54+ # It will have None in positions with no Markdown tasks
55+ self ._items_order = [ str (item .id ) if item else None for item in all_items ]
56+
5057 def start (self ):
5158 pass
5259
5360 def finish (self ):
54- contents = '\n ' .join (str (i ) for i in self ._items_cache .values ()) + "\n "
61+ contents = ""
62+ # add existing file lines as they are if they are not tasks
63+ # or change them for the tasks in text format when appropriate
64+ for item_id , line in zip (self ._items_order , self ._filesystem_file .contents .splitlines ()):
65+ if item_id :
66+ try :
67+ line_content = str (self .get_item (item_id ))
68+ except KeyError :
69+ continue
70+ else :
71+ line_content = line
72+
73+ contents += line_content + "\n "
74+
75+ # so far we've inserted older tasks. add newly synced ones
76+ new_ids = [ item_id for item_id in self ._items_cache .keys () if item_id not in self ._items_order ]
77+ for item_id in new_ids :
78+ line_content = str (self .get_item (item_id ))
79+ contents += line_content + "\n "
80+
5581 self ._filesystem_file .contents = contents
5682 self ._filesystem_file .flush ()
5783
@@ -74,23 +100,22 @@ def get_persistent_id(self, id):
74100
75101 def get_all_items (self , ** kargs ) -> Sequence [FilesystemFile ]:
76102 """Read all items again from storage."""
77- all_items = tuple (
78- MarkdownTaskItem .from_markdown (line , self ._filesystem_file )
79- for line in self ._filesystem_file .contents .splitlines ()
80- )
81-
103+ """The array will have None in lines with no tasks"""
82104 result = []
83- for item in all_items :
84- if item is None :
85- continue
86- item_id = item ._id ()
87- persistent_id = self .get_persistent_id (item_id )
88- if persistent_id != item_id :
89- item ._persistent_id = persistent_id
90- result .append (item )
105+ found_tasks = 0
106+ for line in self ._filesystem_file .contents .splitlines ():
107+ item = MarkdownTaskItem .from_markdown (line , self ._filesystem_file )
108+ if item :
109+ found_tasks += 1
110+ item_id = item ._id ()
111+ persistent_id = self .get_persistent_id (item_id )
112+ if persistent_id != item_id :
113+ item ._persistent_id = persistent_id
114+ if item or kargs .get ('include_non_tasks' ):
115+ result .append (item )
91116
92117 logger .opt (lazy = True ).debug (
93- f"Found { len ( all_items ) } matching tasks inside { self ._filename_path } "
118+ f"Found { found_tasks } matching tasks inside { self ._filename_path } "
94119 )
95120 return result
96121
0 commit comments