Text Folding Reloaded

If we look at the incoming bug reports, a lot still seem to be located in our code folding implementation.

The problem of the current code folding code, even after the GSoC rewrite of large parts, is that it is inter weaved with the syntax highlighting engine.

During highlighting, we try to build up some tree containing all folding start/ends. To accommodate languages that have indentation based folding like Python, we started to fake such starts/ends which are normally tokens like { or }. As these tree can be unbalanced and contain overlapping regions, the implementation is quiet complex.

Additionally, as the folding relies heavily on the syntax highlighting, which is done per document, it is impossible to have different folding per view.

After looking again at the code to solve some issues, it got more and more clear to me, that we need to restart that from scratch.

Instead of feeding the highlighting info into a tree, I started to implement text folding that just allows you to mark regions as folding ranges manually. Then internally a non-overlapping tree of ranges is created that uses our moving cursors to keep track of changes in the document. Each view will have an instance of such a tree, which allows to have folding per view.

These folds are only created on demand, e.g. if you decide to fold some region via selection + “fold” command. This already works quiet well in my Kate repository clone. The missing part is now the integration with the highlighting, that should use this well defined API to only create a folding region in the moment the user wants to fold it. In addition some API for the highlighters is needed to indicate that for some line there is the fold action. After that is done, I might consider to replace the current implementation with the novel one, which is btw. much easier to unit test (which is already done for most API primitives).

Btw., the work is done in this repository, if somebody wants to contribute, patches are welcome .P

6 thoughts on “Text Folding Reloaded

  1. 3.10/4.10 Kate IDE View -> Code Folding menu is a little busy now. I liked the previous 4 options (Collapse/Expand Top Level, Collapse/Expand One Local Level. In an application or class, collapsing functions/methods makes the code very readable. I can’t image using level 2, level 3, level 4 that are now in the menu. Has the code folding syntax/rules changed in the xml syntax highlighting file? Is it still beginRegion, endRegion?

    1. Yes, it’s still beginRegion / endRegion. The syntax did not change.

      As far as I know, the features are not all back. Maybe we should simply drop folding the level 2, or at least 3 and 4 again.

      1. “Maybe we should simply drop folding the level 2, or at least 3 and 4 again.”

        i just set up a new machine and see that this is different from my older version of kate. absolutely terrible to have dropped this feature. i don;t understand how anyone using classes can liev without it.

  2. Please do not remove folding for level 2. It is very useful for me.
    Usually I open php/python files with one class and click to collapse 2 level. Then I can see all methods/structures faster.
    While I edit a method I can jump to other method faster because a lots of methods is collapsed.

  3. Heh, after each Kubuntu update i find Kate less and less useful. First I lost the ability to edit files on remote filesystems mounted via sshfs (due to suddenly obligatory feature to create/update swap files on the target directory after each keystroke). Now Kate can’t fold functions that reside inside a class. In my case this means manually folding about a hundred functions or so after each fileopen – and that is because I have to open the files via sftp instead of sshfs, and Kate can’t remember folding state for files edited via sftp (if it can remember it at all, since i didn’t check that after the last update)…

    I love Kate, how it looks (especially with zenburn and solarize colors) and works when editing the code. The map-view scrollbar is just brilliant. Unfortunately, now it is useless for me, and I have to go back to Windows and Notepad++. What a shame.

  4. I would like to have the level 2 folding back as well, because of the same reasons. If it does not suite majority of users why not just to put it as empty placehoder for definable hotkey.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top