If there is an opened file with some parts folded and some external change comes folding after (or which includes) that change becomes invalid — keeps old line numbers and doesn't match region anymore. How to reproduce: 1. Open some file which has regions for folding (C++, XML, etc) in Kate or KWrite. 2. Fold some region. 3. Open that file other text editor, e.g. vim, and add new line inside of that region. 4. Back to Kate/KWrite, and update file. Actual results: End of folding is broken. Expected results: Folding is either unfolded or recalculated based on new start and end of region. As Kate keeps foldings after restart unnoticed refresh may lead to very strange folds especially after VCS code update and many lines removed/added. I'm not sure this is the right place, but at least bug reproduced with both Kate and KWrite.
I doubt it is possible to fix this. Kate would need to keep a copy of the original file and do a diff with the updated file to see if any of the folding markers still apply to the updated file.
From my perspective simplest solution is to unfold all folds when external update is coming. That will fix at least while Kate is running. As we have no support to fold/unfold all (https://bugs.kde.org/show_bug.cgi?id=352868) it become really annoying to fix folds. More complex and finer solution is to check are current folds match file text fold start end. If not — unfold. That could be done at file open/activation and after external update. Anyway folds markers should be recalculated at that moment as content changes.
More thoughts about more complex and nice solution as I see it. It could be done at moment of recalculation of fold positions or later. If we do check later we have something like two lists (or trees?) of pairs. First list "markers": correct folds starts and endings. Second list "folds": current (folded) folds starts and endings. At update time (open/activate file or external update) we are checking "folds": for each element in "folds" find matching in "markers" by start. I expect they both are sorted by start so we can search with binary search dividing not by half but proportionally starting from first acceptable (no point to check previous folds with lines less than current) — just keep some current start index. If there is no match in "markers" remove this item from "folds". If there is match by start but ending doesn't match either remove from "folds" or change it endings with new valid ending (can't see any problems ATM). If both start and endings match, check next item in "folds". After that all incorrect items would be fixed or removed from "folds" (at worst it would be empty) and it could be used to show folds in file. If "markers" and "folds" are trees instead of lists algorithm of search should be slightly more complex to check inside of node whose start is less than investigated and end is bigger (< and > or <= and >= not sure ATM). I'm ready to spend my time to implement this (starting from this weekend) and it would be nice to have some hint where to start.
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/400
Git commit e4e165278048fe11c5c9279c1ec4f3d8565ca37b by Christoph Cullmann, on behalf of Waqar Ahmed. Committed on 10/08/2022 at 19:39. Pushed by cullmann into branch 'master'. Ignore folding ranges if document has changed M +10 -2 src/buffer/katetextfolding.cpp https://invent.kde.org/frameworks/ktexteditor/commit/e4e165278048fe11c5c9279c1ec4f3d8565ca37b