Skip to content

Commit 9eec595

Browse files
committed
Improve syntax highlight stability
1 parent 8bbb6b1 commit 9eec595

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ protected void beforeParentInit() {
192192
.map(LanguageVersion::getLanguage)
193193
.values()
194194
.filter(Objects::nonNull)
195+
.distinct()
195196
.subscribe(nodeEditionCodeArea::updateSyntaxHighlighter);
196197

197198
((ASTManagerImpl) astManager).classLoaderProperty().bind(auxclasspathClassLoader);

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/NodeSelectionSource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import net.sourceforge.pmd.util.fxdesigner.util.controls.AstTreeView;
2020
import net.sourceforge.pmd.util.fxdesigner.util.reactfx.ReactfxUtil;
2121

22+
import javafx.application.Platform;
23+
2224

2325
/**
2426
* A control or controller that somehow displays nodes in a form that the user can select.
@@ -75,7 +77,7 @@ default Val<Node> initNodeSelectionHandling(DesignerRoot root,
7577
EventStream<NodeSelectionEvent> selection = channel.messageStream(alwaysHandleSelection, this);
7678
selection.subscribe(evt -> {
7779
try {
78-
setFocusNode(evt.selected, evt.options);
80+
Platform.runLater(() -> setFocusNode(evt.selected, evt.options));
7981
} catch (Exception e) {
8082
logInternalException(e);
8183
printShortStackTrace(e);

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/AuxLanguageRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public static LanguageVersion findLanguageVersionByTerseName(String string) {
4141
if (lang == null) {
4242
return null;
4343
}
44-
return lang.getVersion(split[1]);
44+
return split.length == 1 ? lang.getDefaultVersion()
45+
: lang.getVersion(split[1]);
4546
}
4647

4748
@NonNull

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/codearea/HighlightLayerCodeArea.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ private StyleSpans<Collection<String>> recomputePainting() {
148148
.map(UniformStyleCollection::toSpans)
149149
.collect(Collectors.toList());
150150

151+
ensureSyntaxUpToDate();
152+
151153
if (allSpans.isEmpty()) {
152154
return syntaxHighlight.getOrElse(emptySpan());
153155
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/codearea/PmdCoordinatesSystem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public static TextPos2D getPmdLineAndColumnFromOffset(CodeArea codeArea, int abs
7272
* </ul>
7373
*/
7474
public static int getOffsetFromPmdPosition(CodeArea codeArea, int line, int column) {
75+
line = max(line, 1);
76+
column = max(column, 1);
77+
7578
int parIdx = getRtfxParIndexFromPmdLine(line);
7679
int raw = codeArea.getAbsolutePosition(parIdx, getRtfxColumnIndexFromPmdColumn(codeArea, parIdx, column));
7780
return clip(raw, 0, codeArea.getLength() - 1);

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/codearea/SyntaxHighlightingCodeArea.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,20 @@ protected StyleSpans<Collection<String>> call() {
173173
return task;
174174
}
175175

176-
/** Removes the current syntax highlighting span. */
176+
177+
/**
178+
* Removes the current syntax highlighting span.
179+
*/
177180
protected void clearSyntaxHighlighting() {
178181
setCurrentSyntaxHighlight(null);
179182
}
180183

184+
protected void ensureSyntaxUpToDate() {
185+
if (!syntaxHighlighter.isPresent()) {
186+
clearSyntaxHighlighting();
187+
}
188+
}
189+
181190
/**
182191
* Update the syntax highlighting to the specified value.
183192
* If null, syntax highlighting is stripped off.

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/NodeEditionCodeArea.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ private void scrollToNode(Node node, boolean scrollToTop) {
164164
return;
165165
}
166166

167-
int visibleLength = lastVisibleParToAllParIndex() - firstVisibleParToAllParIndex();
167+
int visibleLength;
168+
try {
169+
visibleLength = lastVisibleParToAllParIndex() - firstVisibleParToAllParIndex();
170+
} catch (AssertionError e) {
171+
// may be thrown when many selection events occur in quick succession?
172+
// Something like, the paragraphs
173+
return;
174+
}
168175

169176
boolean fitsViewPort = node.getEndLine() - node.getBeginLine() <= visibleLength;
170177
int rtfxLine = getRtfxParIndexFromPmdLine(node.getBeginLine());
@@ -174,15 +181,13 @@ private void scrollToNode(Node node, boolean scrollToTop) {
174181
boolean isEndVisible = rtfxEndLine <= lastVisibleParToAllParIndex();
175182

176183

184+
if (!isStartVisible && scrollToTop) {
185+
showParagraphAtTop(max(rtfxLine - 2, 0));
186+
}
177187
if (fitsViewPort) {
178-
if (!isStartVisible && scrollToTop) {
179-
showParagraphAtTop(max(rtfxLine - 2, 0));
180-
}
181188
if (!isEndVisible) {
182189
showParagraphAtBottom(min(rtfxEndLine, getParagraphs().size()));
183190
}
184-
} else if (!isStartVisible && scrollToTop) {
185-
showParagraphAtTop(max(rtfxLine - 2, 0));
186191
}
187192
}
188193

@@ -298,27 +303,29 @@ public void moveCaret(int line, int column) {
298303
public void setFocusNode(final Node node, DataHolder options) {
299304

300305

306+
boolean changed = !Objects.equals(node, currentFocusNode.getValue());
307+
if (changed) {
308+
309+
currentFocusNode.setValue(node);
310+
311+
// editor is only restyled if the selection has changed
312+
Platform.runLater(() -> styleNodes(
313+
node == null ? emptyList() : singleton(node), StyleLayerIds.FOCUS, true));
314+
315+
if (node == null) {
316+
highlightRelatedNodes(emptyList());
317+
} else {
318+
Platform.runLater(() -> highlightRelatedNodes(relatedNodesSelector.getValue().getHighlightedNodesWhenSelecting(node)));
319+
}
320+
}
321+
301322
// editor must not be scrolled when finding a new selection in a
302323
// tree that is being edited
324+
// Editor must be scrolled after styling
303325
if (node != null && !options.hasData(SELECTION_RECOVERY)) {
304326
// don't randomly jump to top of eg ClassOrInterfaceBody
305327
// when selecting from a caret position
306-
scrollToNode(node, !options.hasData(CARET_POSITION));
307-
}
308-
309-
if (Objects.equals(node, currentFocusNode.getValue())) {
310-
return;
311-
}
312-
313-
currentFocusNode.setValue(node);
314-
315-
// editor is only restyled if the selection has changed
316-
Platform.runLater(() -> styleNodes(node == null ? emptyList() : singleton(node), StyleLayerIds.FOCUS, true));
317-
318-
if (node == null) {
319-
highlightRelatedNodes(emptyList());
320-
} else {
321-
Platform.runLater(() -> highlightRelatedNodes(relatedNodesSelector.getValue().getHighlightedNodesWhenSelecting(node)));
328+
Platform.runLater(() -> scrollToNode(node, !options.hasData(CARET_POSITION)));
322329
}
323330
}
324331

0 commit comments

Comments
 (0)