2121import java .util .Objects ;
2222import java .util .Optional ;
2323import java .util .function .IntFunction ;
24- import java .util .stream .Collectors ;
2524
2625import org .checkerframework .checker .nullness .qual .NonNull ;
2726import org .fxmisc .richtext .LineNumberFactory ;
3433import org .reactfx .value .Var ;
3534
3635import net .sourceforge .pmd .lang .Language ;
36+ import net .sourceforge .pmd .lang .LanguageVersion ;
37+ import net .sourceforge .pmd .lang .LanguageVersionHandler ;
3738import net .sourceforge .pmd .lang .ast .Node ;
38- import net .sourceforge .pmd .lang . symboltable . NameOccurrence ;
39- import net .sourceforge .pmd .lang . symboltable . ScopedNode ;
39+ import net .sourceforge .pmd .util . designerbindings . DesignerBindings ;
40+ import net .sourceforge .pmd .util . designerbindings . RelatedNodesSelector ;
4041import net .sourceforge .pmd .util .fxdesigner .SourceEditorController ;
4142import net .sourceforge .pmd .util .fxdesigner .app .DesignerRoot ;
4243import net .sourceforge .pmd .util .fxdesigner .app .NodeSelectionSource ;
@@ -84,28 +85,37 @@ public class NodeEditionCodeArea extends HighlightLayerCodeArea<StyleLayerIds> i
8485 private final Var <Node > currentFocusNode = Var .newSimpleVar (null );
8586 private final Var <List <Node >> currentRuleResults = Var .newSimpleVar (Collections .emptyList ());
8687 private final Var <List <Node >> currentErrorNodes = Var .newSimpleVar (Collections .emptyList ());
87- private final Var <List <NameOccurrence >> currentNameOccurrences = Var .newSimpleVar (Collections .emptyList ());
88+ private final Var <List <Node >> currentRelatedNodes = Var .newSimpleVar (Collections .emptyList ());
8889 private final DesignerRoot designerRoot ;
8990 private final EventSource <NodeSelectionEvent > selectionEvts = new EventSource <>();
9091
91-
92+ private final Val < RelatedNodesSelector > relatedNodesSelector ;
9293
9394 /** Only provided for scenebuilder, not used at runtime. */
9495 public NodeEditionCodeArea () {
9596 super (StyleLayerIds .class );
96- designerRoot = null ;
97+ this .designerRoot = null ;
98+ this .relatedNodesSelector = null ;
9799 }
98100
99101 public NodeEditionCodeArea (@ NamedArg ("designerRoot" ) DesignerRoot root ) {
100102 super (StyleLayerIds .class );
101103
102104 this .designerRoot = root ;
105+ this .relatedNodesSelector =
106+ root .getService (DesignerRoot .AST_MANAGER )
107+ .languageVersionProperty ()
108+ .map (LanguageVersion ::getLanguageVersionHandler )
109+ .map (LanguageVersionHandler ::getDesignerBindings )
110+ .map (DesignerBindings ::getRelatedNodesSelector )
111+ .orElseConst (DesignerUtil .getDefaultRelatedNodesSelector ());
112+
103113
104114 setParagraphGraphicFactory (defaultLineNumberFactory ());
105115
106116 currentRuleResultsProperty ().values ().subscribe (this ::highlightXPathResults );
107117 currentErrorNodesProperty ().values ().subscribe (this ::highlightErrorNodes );
108- currentNameOccurrences .values ().subscribe (this ::highlightNameOccurrences );
118+ currentRelatedNodesProperty () .values ().subscribe (this ::highlightRelatedNodes );
109119
110120 initNodeSelectionHandling (designerRoot , selectionEvts , true );
111121
@@ -251,8 +261,8 @@ public final Var<List<Node>> currentErrorNodesProperty() {
251261 }
252262
253263
254- public Var <List <NameOccurrence >> currentNameOccurrencesProperty () {
255- return currentNameOccurrences ;
264+ public Var <List <Node >> currentRelatedNodesProperty () {
265+ return currentRelatedNodes ;
256266 }
257267
258268
@@ -263,8 +273,8 @@ private void highlightXPathResults(Collection<? extends Node> nodes) {
263273
264274
265275 /** Highlights name occurrences (secondary highlight). */
266- private void highlightNameOccurrences (Collection <? extends NameOccurrence > occs ) {
267- styleNodes (occs . stream (). map ( NameOccurrence :: getLocation ). collect ( Collectors . toList ()) , StyleLayerIds .NAME_OCCURRENCE , true );
276+ private void highlightRelatedNodes (Collection <? extends Node > occs ) {
277+ styleNodes (occs , StyleLayerIds .NAME_OCCURRENCE , true );
268278 }
269279
270280
@@ -305,9 +315,10 @@ public void setFocusNode(final Node node, DataHolder options) {
305315 // editor is only restyled if the selection has changed
306316 Platform .runLater (() -> styleNodes (node == null ? emptyList () : singleton (node ), StyleLayerIds .FOCUS , true ));
307317
308- if (node instanceof ScopedNode ) {
309- // not null as well
310- Platform .runLater (() -> highlightNameOccurrences (DesignerUtil .getNameOccurrences ((ScopedNode ) node )));
318+ if (node == null ) {
319+ highlightRelatedNodes (emptyList ());
320+ } else {
321+ Platform .runLater (() -> highlightRelatedNodes (relatedNodesSelector .getValue ().getHighlightedNodesWhenSelecting (node )));
311322 }
312323 }
313324
0 commit comments