Custom code folding

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Custom code folding

Postby majenko » Thu Jul 24, 2014 6:01 pm

I am trying to use RSyntaxTextArea to display a big chunk of formatted data (actually a report on the structure of an ELF file). I would like to have some large chunks of HEX data in code folded areas.

I can't get it to honour my folds though.

I am building the content of the text area up in a StringBuilder and generating folds as I go and making a list of Fold objects, then I add them en-masse to the fold manager of my text area. It seems to completely ignore it though.

Here's some snippets of code:
Code: Select all
RSyntaxTextArea text = new RSyntaxTextArea();
StringBuilder sb = new StringBuilder();
ArrayList<Fold> folds = new ArrayList<Fold>();
// ... blah blah blah adding stuff to sb
// ... then in a loop:
    Fold f = new Fold(FoldType.FOLD_TYPE_USER_DEFINED_MIN + 1, text, sb.length());
    sb.append(elfFile.dumpRawData(elfFile.sh_addr, elfFile.sh_offset, elfFile.sh_size));
// then at the end:
RTextScrollPane scroll = new RTextScrollPane(text);
FoldManager fm = text.getFoldManager();

If I set the syntax highlighting to, say, C++, it creates folds for any { ... } it finds. The fm.clear() doesn't seem to get rid of those even. It's like all changes to the FoldManager object are completely ignored.

Am I misunderstanding how it works?
Posts: 17
Joined: Sun Jul 14, 2013 11:01 am

Re: Custom code folding

Postby robert » Fri Aug 01, 2014 1:17 pm

Interesting, I thought this approach would work (assuming you called repaint() afterward), though this is not the expected means of displaying folds, and this is assuming the content is read-only (as the document mutating will cause the FoldManager to re-parse the document for new folds).

The "official" way to do this is to create an implementation of the [url=[FoldParser interface[/url] that knows how to identify folds, and install that globally via:

FoldParserManager.get().addFoldParserMapping(SyntaxConstants.SYNTAX_STYLE_NONE, myParser);

Note that this will cause your fold parser to be run on any instance of RSTA that is using SYNTAX_STYLE_NONE, which may not be desired. If that's not desired you may have to create your own syntax highlighting scheme (even if it does no highlighting) just to get this to work. Making up your own syntax style constant may not be sufficient as we may fall back to SYNTAX_STYLE_NONE in that case; I'd have to try it out to be sure.

In any case, please try the above; maybe that will fix your problem. If not, we could change the API to allow individual instances of RSTA to accept custom FoldManagers (or be under the rule of different FoldParserManagers) to allow different RSyntaxTextAreas to have different foldings for the same type of syntax highlighting. Though typing that out makes me realize how incredibly unlikely that is. :)
User avatar
Posts: 852
Joined: Sat May 10, 2008 5:16 pm

Re: Custom code folding

Postby majenko » Fri Aug 01, 2014 5:26 pm

The problem here is that there is nothing to identify where the folds should be in the text. Yes, it's read only, so that's not an issue.

The whole output is basically a report in the form of:
Code: Select all
Blah: blah blah
Blah: blah blah blah
blah blah blah
blah blah...

0x9D001000: 03 28 D3 29 94 28 48 10 D9 B2 FE FF FF FF FF FF
0x9D001010: 03 28 D3 29 94 28 48 10 D9 B2 FE FF FF FF FF FF
0x9D001020: 03 28 D3 29 94 28 48 10 D9 B2 FE FF FF FF FF FF

Foo blah bar foo...

with some different sections in it but similar stuff. I basically want some (but not all) the hex dump blocks to be folded, as well as some other wordy chunks of text, so it's down to the program that generates the text to work out where the folds should be.

So a fold parser would be of no use whatsoever in this situation, which is why I was trying the approach of creating a list of folds and passing it to the fold manager, which seemed to completely ignore it.

I wasn't calling any repaint() functions, as nothing had been displayed by that point anyway, so nothing had been painted to repaint.

It's almost as if all my hard work gets thrown out when the RSTA instance gets displayed...?
Posts: 17
Joined: Sun Jul 14, 2013 11:01 am

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest