修复bug

This commit is contained in:
许轲 2023-09-28 02:51:40 +08:00
parent 03f663deba
commit 47da48ba20

View File

@ -103,7 +103,7 @@ public class LineNumberTextArea extends CodeArea {
this.getVisibleParagraphs().addModificationObserver this.getVisibleParagraphs().addModificationObserver
( (
new JavaKeywordsDemo.VisibleParagraphStyler<>(this, this::computeHighlighting) new LineNumberTextArea.VisibleParagraphStyler<>(this, this::computeHighlighting)
); );
// 自动缩进在按下回车键时插入上一行的缩进 // 自动缩进在按下回车键时插入上一行的缩进
@ -132,6 +132,16 @@ public class LineNumberTextArea extends CodeArea {
StyleSpansBuilder<Collection<String>> spansBuilder StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>(); = new StyleSpansBuilder<>();
while (matcher.find()) { while (matcher.find()) {
String styleClass = getStyleClass(matcher);
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
private static String getStyleClass(Matcher matcher) {
String styleClass = String styleClass =
matcher.group("KEYWORD") != null ? "keyword" : matcher.group("KEYWORD") != null ? "keyword" :
matcher.group("PAREN") != null ? "paren" : matcher.group("PAREN") != null ? "paren" :
@ -142,12 +152,7 @@ public class LineNumberTextArea extends CodeArea {
matcher.group("COMMENT") != null ? "comment" : matcher.group("COMMENT") != null ? "comment" :
null; /* 永远不会发生 */ null; /* 永远不会发生 */
assert styleClass != null; assert styleClass != null;
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); return styleClass;
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
} }
static class VisibleParagraphStyler<PS, SEG, S> implements Consumer<ListModification<? extends Paragraph<PS, SEG, S>>> { static class VisibleParagraphStyler<PS, SEG, S> implements Consumer<ListModification<? extends Paragraph<PS, SEG, S>>> {
@ -155,21 +160,24 @@ public class LineNumberTextArea extends CodeArea {
private final Function<String,StyleSpans<S>> computeStyles; private final Function<String,StyleSpans<S>> computeStyles;
private int prevParagraph, prevTextLength; private int prevParagraph, prevTextLength;
public VisibleParagraphStyler(GenericStyledArea<PS, SEG, S> area, Function<String, StyleSpans<S>> computeStyles) { public VisibleParagraphStyler( GenericStyledArea<PS, SEG, S> area, Function<String,StyleSpans<S>> computeStyles )
{
this.computeStyles = computeStyles; this.computeStyles = computeStyles;
this.area = area; this.area = area;
} }
@Override @Override
public void accept(ListModification<? extends Paragraph<PS, SEG, S>> lm) { public void accept( ListModification<? extends Paragraph<PS, SEG, S>> lm )
if (lm.getAddedSize() > 0) { {
Platform.runLater(() -> if ( lm.getAddedSize() > 0 ) Platform.runLater( () ->
{ {
int paragraph = Math.min( area.firstVisibleParToAllParIndex() + lm.getFrom(), area.getParagraphs().size()-1 ); int paragraph = Math.min( area.firstVisibleParToAllParIndex() + lm.getFrom(), area.getParagraphs().size()-1 );
String text = area.getText( paragraph, 0, paragraph, area.getParagraphLength( paragraph ) ); String text = area.getText( paragraph, 0, paragraph, area.getParagraphLength( paragraph ) );
if (paragraph != prevParagraph || text.length() != prevTextLength) { if ( paragraph != prevParagraph || text.length() != prevTextLength )
if (paragraph < area.getParagraphs().size() - 1) { {
if ( paragraph < area.getParagraphs().size()-1 )
{
int startPos = area.getAbsolutePosition( paragraph, 0 ); int startPos = area.getAbsolutePosition( paragraph, 0 );
area.setStyleSpans( startPos, computeStyles.apply( text ) ); area.setStyleSpans( startPos, computeStyles.apply( text ) );
} }
@ -179,7 +187,6 @@ public class LineNumberTextArea extends CodeArea {
}); });
} }
} }
}
private class DefaultContextMenu extends ContextMenu { private class DefaultContextMenu extends ContextMenu {
private MenuItem fold, unfold, print; private MenuItem fold, unfold, print;