Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

MarkdownSyntaxHighlighter: use StyleSpansBuilder instead of invoking clearStyle() and setStyle() because: - clearStyle(0, textLength) has the problem that it always scrolls caret to the top of the view - probably more performat

AuthorKarl Tauber <email>
Date2015-07-26 13:57:56 GMT+0200
Commitb3ec4814c1d92db77943506e49e75dbed75e50c7
Parent57e73bb
Delta25 lines added, 18 lines removed, 7-line increase
src/main/java/org/markdownwriterfx/editor/MarkdownSyntaxHighlighter.java
package org.markdownwriterfx.editor;
+import java.util.Collection;
import java.util.Collections;
import javafx.application.Platform;
import org.fxmisc.richtext.StyleClassedTextArea;
+import org.fxmisc.richtext.StyleSpans;
+import org.fxmisc.richtext.StyleSpansBuilder;
import org.pegdown.ast.*;
implements Visitor
{
- private final StyleClassedTextArea textArea;
- private final int textLength;
+ private int textLength;
+ private StyleSpansBuilder<Collection<String>> spansBuilder;
+ private int nextIndex;
static void highlight(StyleClassedTextArea textArea, RootNode astRoot) {
- new MarkdownSyntaxHighlighter(textArea).toStyles(astRoot);
+ assert Platform.isFxApplicationThread();
+
+ textArea.setStyleSpans(0, new MarkdownSyntaxHighlighter()
+ .computeHighlighting(astRoot, textArea.getLength()));
}
- private MarkdownSyntaxHighlighter(StyleClassedTextArea textArea) {
- this.textArea = textArea;
- this.textLength = textArea.getLength();
+ private MarkdownSyntaxHighlighter() {
}
- private void toStyles(RootNode astRoot) {
- assert Platform.isFxApplicationThread();
+ private StyleSpans<Collection<String>> computeHighlighting(RootNode astRoot, int textLength) {
+ this.textLength = textLength;
- textArea.clearStyle(0, textLength);
+ spansBuilder = new StyleSpansBuilder<>();
+ nextIndex = 0;
astRoot.accept(this);
+ spansBuilder.add(Collections.emptyList(), textLength - nextIndex);
+ return spansBuilder.create();
}
private void setStyleClass(Node node, String styleClass) {
- try {
- // because PegDownProcessor.prepareSource() adds two trailing newlines
- // to the text before parsing, we need to limit the end index
- textArea.setStyle(node.getStartIndex(),
- Math.min(node.getEndIndex(), textLength),
- Collections.singleton(styleClass));
- } catch(IllegalArgumentException ex) {
- ex.printStackTrace();
- }
+ // because PegDownProcessor.prepareSource() adds two trailing newlines
+ // to the text before parsing, we need to limit the end index
+ int startIndex = node.getStartIndex();
+ int endIndex = Math.min(node.getEndIndex(), textLength);
+
+ spansBuilder.add(Collections.emptyList(), startIndex - nextIndex);
+ spansBuilder.add(Collections.singleton(styleClass), endIndex - startIndex);
+ nextIndex = endIndex;
}
}