Bug 384459 - Text folding becomes invalid after file external update
Summary: Text folding becomes invalid after file external update
Status: RESOLVED FIXED
Alias: None
Product: frameworks-ktexteditor
Classification: Frameworks and Libraries
Component: general (other bugs)
Version First Reported In: 5.37.0
Platform: openSUSE Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-07 09:15 UTC by Mykola Krachkovsky
Modified: 2022-08-10 19:45 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mykola Krachkovsky 2017-09-07 09:15:37 UTC
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.
Comment 1 Christoph Feck 2017-09-20 23:23:20 UTC
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.
Comment 2 Mykola Krachkovsky 2017-09-21 07:47:16 UTC
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.
Comment 3 Mykola Krachkovsky 2017-09-21 08:25:17 UTC
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.
Comment 4 Bug Janitor Service 2022-08-10 17:06:00 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/400
Comment 5 Christoph Cullmann 2022-08-10 19:45:44 UTC
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