Gruntfuggly / todo-tree

Use ripgrep to find TODO tags and display the results in a tree view
Other
1.42k stars 137 forks source link

Fix duplicate entries when refreshing and files have unsaved changes #638

Closed baincd closed 1 year ago

baincd commented 2 years ago

The root cause of this issue is in rebuild().

refreshOpenFiles() removes current tree entries before adding entries from the file buffer, so this should execute after entries are added from the files on disk. At first glance this appears to be how it is coded, because iterateSearchList() is called before refreshOpenFiles().

However, iterateSearchList() calls search(...) which uses ripgrep.search(...), and ripgrep.search actually returns a Promise. This causes the thread of execution to return to rebuild(), which then executes refreshOpenFiles(). After refreshOpenFiles() completes (having added tree entries based on the file buffer), then the promises from ripgrep.search(...) are resolved and tree entries from the files on disk are added.

To fix this, I rewrote search(...) and iterateSearchList() to return Promises. Then in rebuild(), refreshOpenFiles() is executed only once the promises from the ripgrep.search(...) are resolved.

Resolves #637

baincd commented 1 year ago

Hi @Gruntfuggly ,

Is there anything I can do to assist to get this fix merged into todo-tree? It would be nice to squash this annoying bug, and this PR provide what I think is a good fix. Please let me know - I'm happy to help in any way I can!

Thank you again for all your hard work on this awesome extension!

Gruntfuggly commented 1 year ago

Thanks for this - I always have auto save enabled, so I never noticed this.

baincd commented 1 year ago

Totally understand. Happy I could help. Thank you!