Skip to content

Commit 7f6e230

Browse files
committed
GH-1644: include additional information for type nodes as sub-nodes, derived from the Spring-specific document outline symbols
1 parent 43593e3 commit 7f6e230

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/commands/CachedSpringMetamodelIndex.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
1818
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeClassElement;
1919
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypePackageElement;
20+
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
2021
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
2122

2223
public class CachedSpringMetamodelIndex {
@@ -53,6 +54,10 @@ public <T extends SpringIndexElement> List<T> getNodesOfType(String projectName,
5354
return springIndex.getNodesOfType(projectName, type);
5455
}
5556

57+
public Bean[] getBeansOfDocument(String docUri) {
58+
return springIndex.getBeansOfDocument(docUri);
59+
}
60+
5661
private record ProjectCache(List<StereotypeClassElement> classes, ConcurrentMap<String, StereotypePackageElement> packages) {}
5762

5863
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/commands/JMoleculesStructureView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Node createTree(IJavaProject project, IndexBasedStereotypeFactory factory
5555
};
5656

5757
// create json nodes to display the structure in a nice way
58-
var jsonHandler = new JsonNodeHandler<StereotypePackageElement, Object>(labelProvider, consumer);
58+
var jsonHandler = new JsonNodeHandler<StereotypePackageElement, Object>(labelProvider, consumer, springIndex);
5959

6060
// create the project tree and apply all the groupers from the project
6161
// TODO: in the future, we need to trim this grouper arrays down to what is selected on the UI

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/commands/JsonNodeHandler.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
package org.springframework.ide.vscode.boot.java.commands;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
2022
import java.util.LinkedHashMap;
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.function.BiConsumer;
2426
import java.util.function.Consumer;
2527

28+
import org.eclipse.lsp4j.DocumentSymbol;
29+
import org.eclipse.lsp4j.Location;
2630
import org.jmolecules.stereotype.api.Stereotype;
2731
import org.jmolecules.stereotype.tooling.LabelProvider;
2832
import org.jmolecules.stereotype.tooling.MethodNodeContext;
@@ -31,6 +35,7 @@
3135
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeClassElement;
3236
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeMethodElement;
3337
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypePackageElement;
38+
import org.springframework.ide.vscode.commons.protocol.spring.SymbolElement;
3439

3540
import com.google.gson.Gson;
3641
import com.google.gson.GsonBuilder;
@@ -50,12 +55,17 @@ public class JsonNodeHandler<A, C> implements NodeHandler<A, StereotypePackageEl
5055
private final Node root;
5156
private final LabelProvider<A, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, C> labels;
5257
private final BiConsumer<Node, C> customHandler;
58+
private final CachedSpringMetamodelIndex springIndex;
59+
5360
private Node current;
5461

55-
public JsonNodeHandler(LabelProvider<A, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, C> labels, BiConsumer<Node, C> customHandler) {
62+
public JsonNodeHandler(LabelProvider<A, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, C> labels, BiConsumer<Node, C> customHandler,
63+
CachedSpringMetamodelIndex springIndex) {
5664
this.labels = labels;
57-
this.root = new Node(null);
65+
this.springIndex = springIndex;
5866
this.customHandler = customHandler;
67+
68+
this.root = new Node(null);
5969
this.current = root;
6070
}
6171

@@ -91,9 +101,37 @@ public void handleType(StereotypeClassElement type, NodeContext context) {
91101
.withAttribute(TEXT, labels.getTypeLabel(type))
92102
.withAttribute(LOCATION, type.getLocation())
93103
.withAttribute(ICON, StereotypeIcons.getIcon(StereotypeIcons.TYPE_KEY))
104+
.withChildren(createTypeSubnotes(node, type))
94105
);
95106
}
96107

108+
private List<Node> createTypeSubnotes(Node parent, StereotypeClassElement type) {
109+
if (System.getProperty("enable-structure-view-details") == null) {
110+
return Collections.emptyList();
111+
}
112+
113+
if (type.getLocation() == null) {
114+
return Collections.emptyList();
115+
}
116+
117+
String docUri = type.getLocation().getUri();
118+
ArrayList<Node> result = new ArrayList<Node>();
119+
120+
Arrays.stream(springIndex.getBeansOfDocument(docUri))
121+
.filter(bean -> bean.getType().equals(type.getType()))
122+
.flatMap(bean -> bean.getChildren().stream())
123+
.filter(child -> child instanceof SymbolElement)
124+
.map(child -> ((SymbolElement) child))
125+
.forEach(symbolElement -> {
126+
DocumentSymbol symbol = symbolElement.getDocumentSymbol();
127+
result.add(new Node(parent)
128+
.withAttribute(TEXT, symbol.getName())
129+
.withAttribute(LOCATION, new Location(docUri, symbol.getRange())));
130+
});
131+
132+
return result;
133+
}
134+
97135
@Override
98136
public void handleMethod(StereotypeMethodElement method, MethodNodeContext<StereotypeClassElement> context) {
99137
addChildFoo(node -> node
@@ -157,6 +195,12 @@ public Node withAttribute(String key, Object value) {
157195
this.attributes.put(key, value);
158196
return this;
159197
}
198+
199+
public Node withChildren(List<Node> children) {
200+
this.children.addAll(children);
201+
return this;
202+
}
203+
160204
}
161205

162206

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/commands/ModulithStructureView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Node createTree(IJavaProject project, IndexBasedStereotypeFactory factory
5656
};
5757

5858
// create json nodes to display the structure in a nice way
59-
var jsonHandler = new JsonNodeHandler<ApplicationModules, NamedInterfaceNode>(labelProvider, consumer);
59+
var jsonHandler = new JsonNodeHandler<ApplicationModules, NamedInterfaceNode>(labelProvider, consumer, springIndex);
6060

6161
// create the project tree and apply all the groupers from the project
6262
// TODO: in the future, we need to trim this grouper arrays down to what is selected on the UI

0 commit comments

Comments
 (0)