RichTextArea: How to return existing paragraphs from SyntaxDecorator?
Andy Goryachev
andy.goryachev at oracle.com
Tue Apr 29 15:02:34 UTC 2025
Multiple topics here.
1. The view does size 100 paragraphs (Params.SLIDING_WINDOW_EXTENT) to ensure smooth scrolling when text wrap is on. Initially I thought we could allow the application to control this parameter, but we decided against it, as it exposes internal aspect of the implementation.
2. You probably should not cache the paragraphs in the model, or try to optimize the styling in the way I understood you did. When the view asks for the paragraph, the model needs to render it in all its glory, or things would break.
It looks you are trying to implement the syntax highlighting, right? The first question you might want to ask is determine whether the styles are known at the time when the paragraph gets requested. If not - you probably want to return a plain text paragraph, that is with no styling, and once the styling is known, fire the model's style change event, which will cause the view to request the (newly updated) paragraphs it needs. In other words, the code you supplied won't work - the model should always build a new paragraph (or supply a cached one only if you can guarantee that nothing in it changed, not event line index).
3. Not sure about the scrolling, it seems to work on macOS as expected. Does it work with a stock model that has none of the "optimizations"?
-andy
From: openjfx-dev <openjfx-dev-retn at openjdk.org> on behalf of PavelTurk <pavelturk2000 at gmail.com>
Date: Monday, April 28, 2025 at 16:54
To: openjfx-dev at openjdk.org <openjfx-dev at openjdk.org>
Subject: RichTextArea: How to return existing paragraphs from SyntaxDecorator?
I noticed that paragraphs in CodeArea are updated too aggressively. For example, when I press a single key like '1', about 100 paragraphs get refreshed.
To improve performance, I decided to implement it this way: I store information for each paragraph indicating whether its styles are up-to-date or not.
From SyntaxDecorator, I return the existing paragraph from the model if the styles are valid. If the styles are outdated, I build a new paragraph.
Something like this:
codeArea.setSyntaxDecorator(new SyntaxDecorator() {
@Override
public RichParagraph createRichParagraph(CodeTextModel model, int index) {
if (paragraphStylesAreValid) {
return codeArea.getModel().getParagraph(index);
} else {
RichParagraph.Builder b = RichParagraph.builder();
...
return b.build();
}
}
@Override
public void handleChange(CodeTextModel m, TextPos start, TextPos end, int charsTop,
int linesAdded, int charsBottom) {
}
});
However, it didn't work because of this line - https://github.com/openjdk/jfx/blob/3fdd21386d6db96294fcecd80afc25d09732c067/modules/jfx.incubator.richtext/src/main/java/jfx/incubator/scene/control/richtext/model/CodeTextModel.java#L83
As I result I get StackOverflowError. Could anyone say how to do it?
Best regards, Pavel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20250429/7d2843fa/attachment.htm>
More information about the openjfx-dev
mailing list