Richtextfx: can richtextFx fold code area?

Created on 26 Aug 2020  路  13Comments  路  Source: FXMisc/RichTextFX

just like ide does, can fold specific area into one line

All 13 comments

or can I find text using richtextFx?

RichTextFX doesn't have folding or search built in.

I need folding for my project. How one can go about implementing this? There is an API for displaying things on a margin of the editor to indicate the fold, but I cannot figure out how to keep some text in the buffer (for saving) and at the same time do not display it. Any ideas?

You'll need to create and manage your own separate folded text buffer that contains the sections of folded text separate from what is being displayed. Maybe something like List\ where FoldInfo is for the line number, line count, and text of the fold. Then you'll need to update the line numbers of folds whenever a line before the fold is added or deleted.

To save you won't be able to use getText but will rather have to assemble the whole text by stepping through the List of FoldInfo objects together with extracting the displayed text.

Sounds almost as terrible as the solution I went with 馃挬

I extended GenericStyledArea and provided a custom nodeFactory that returns a 1-pixel-sized Rectangle instead of TextExt based on style. Those are still visible, selectable, and editable but that's ok for me. I intend to catch those cases and just unfold when needed.
I am not confortable messing with the plain text buffer.
I'm not exacly sure what will happen if the Rectangle will have size = 0. Seems to render correctly at the first glance.

Okay I have an alternative that's more along your line that I'll share with you, I just need to assemble it a bit first ..... will hopefully post tomorrow.

Okay so here is my take two. It's based on a previous project I did where I subclassed GenericStyledArea (as StyledSegmentTextArea) to take _AbstractSegments_ instead of String in order to simplify using multiple custom objects. An extension of _AbstractSegment_ can be made to produce Text, Labels, Images, Hyperlinks, whatever you want, so that GenericStyledArea聽can handle / display them.

For clarity I've stripped out all the extras and just left the basics, TextSegment and a new FoldedSegment: FoldSource.zip

Run FoldingTextDemo to try it out.

Just for background and clarity, whenever any text is added to the area _RichTextFX_ invokes MySegmentOps to produce TextSegments by default for Strings. MySegmentOps is also the proxy for join and split operations the area wants to do on/between any TextSegments and FoldedSegments.

There are also two jar libraries in the zip file which are needed for copy/paste XML encoding and decoding.

Now you can choose between the lesser of all the terrible ideas :-)

Thank you! I will take some sweet time with this example so I moved it to a separate repository:
https://github.com/atoktoto/RichTextFX-folding-demo

After giving this some more thought I've come up with a third (simpler) way of doing this by using paragraph styling, which I think I can bake into RichTextFX. I'll know for sure next week some time .....

I would like a vote/input on a preferred method name please: 聽

  1. hideParagraphs 聽/ 聽unhideParagraphs
  2. collapseParagraphs 聽/ 聽expandParagraphs
  3. foldParagraphs 聽/ 聽unfoldParagraphs
  4. other ?

The 3. option seems to be the most intuitive for me.

I was hoping to submit a PR by now but I'm not yet happy with the API. So this is just to let you know that I've basically got it done and there should be a PR next week sometime. Sorry for the delay ....

Okay, a PR has been submitted. Will merge next week sometime pending any feedback or changes .....

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JordanMartinez picture JordanMartinez  路  11Comments

ASchmidt84 picture ASchmidt84  路  9Comments

saifalmutory picture saifalmutory  路  7Comments

prat-man picture prat-man  路  9Comments

kszilagyi picture kszilagyi  路  6Comments