Skip to content

Commit ddc78d8

Browse files
committed
display nice request mapping symbol labels in the jmolecules struture view
1 parent f7e9346 commit ddc78d8

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
2424
import org.springframework.ide.vscode.boot.java.Annotations;
2525
import org.springframework.ide.vscode.boot.java.commands.ToolsJsonNodeHandler.Node;
26+
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexElement;
2627
import org.springframework.ide.vscode.boot.java.stereotypes.IndexBasedStereotypeFactory;
2728
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeCatalogRegistry;
2829
import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeClassElement;
@@ -38,7 +39,11 @@ public class SpringIndexCommands {
3839
private static final String SPRING_STRUCTURE_CMD = "sts/spring-boot/structure";
3940
private static final String SPRING_STRUCTURE_CMD_2 = "sts/spring-boot/structure2";
4041

42+
private final SpringMetamodelIndex springIndex;
43+
4144
public SpringIndexCommands(SimpleLanguageServer server, SpringMetamodelIndex springIndex, JavaProjectFinder projectFinder, StereotypeCatalogRegistry stereotypeCatalogRegistry) {
45+
this.springIndex = springIndex;
46+
4247
server.onCommand(SPRING_STRUCTURE_CMD, params -> server.getAsync().invoke(() -> springIndex.getProjects()));
4348
server.onCommand(SPRING_STRUCTURE_CMD_2, params -> server.getAsync().invoke(() -> {
4449
return projectFinder.all().stream().map(project -> nodeFrom(stereotypeCatalogRegistry, springIndex, project)).filter(Objects::nonNull).collect(Collectors.toList());
@@ -55,7 +60,7 @@ private Node nodeFrom(StereotypeCatalogRegistry stereotypeCatalogRegistry, Sprin
5560
var labelProvider = new SimpleLabelProvider<>(StereotypePackageElement::getPackageName, StereotypePackageElement::getPackageName, StereotypeClassElement::getType,
5661
(StereotypeMethodElement m, StereotypeClassElement __) -> m.getMethodName(), Object::toString)
5762
.withTypeLabel(it -> abbreviate(mainApplicationPackage, it))
58-
.withMethodLabel((m, c) -> getMethodLabel(m, c));
63+
.withMethodLabel((m, c) -> getMethodLabel(project, m, c));
5964

6065
var structureProvider = new ToolsStructureProvider(springIndex, project);
6166

@@ -86,9 +91,20 @@ private String abbreviate(StereotypePackageElement mainApplicationPackage, Stere
8691
}
8792
}
8893

89-
private String getMethodLabel(StereotypeMethodElement method, StereotypeClassElement clazz) {
94+
private String getMethodLabel(IJavaProject project, StereotypeMethodElement method, StereotypeClassElement clazz) {
9095
// TODO: special treatment for methods that have specific index elements with specific labels (e.g. mapping methods)
91-
return method.getMethodLabel();
96+
97+
Optional<RequestMappingIndexElement> mapping = springIndex.getNodesOfType(project.getElementName(), RequestMappingIndexElement.class).stream()
98+
.filter(mappingElement -> mappingElement.getMethodSignature() != null && mappingElement.getMethodSignature().equals(method.getMethodSignature()))
99+
.findAny();
100+
101+
if (mapping.isPresent()) {
102+
return mapping.get().getDocumentSymbol().getName();
103+
}
104+
else {
105+
return method.getMethodLabel();
106+
}
107+
92108
}
93109

94110
public StereotypePackageElement identifyMainApplicationPackage(IJavaProject project, SpringMetamodelIndex springIndex) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
import org.eclipse.lsp4j.Range;
1414

1515
public class RequestMappingIndexElement extends WebEndpointIndexElement {
16+
17+
private String methodSignature;
1618

17-
public RequestMappingIndexElement(String path, String[] httpMethods, String[] contentTypes, String[] acceptTypes, Range range, String symbolLabel) {
19+
public RequestMappingIndexElement(String path, String[] httpMethods, String[] contentTypes, String[] acceptTypes, Range range, String symbolLabel, String methodSignature) {
1820
super(path, httpMethods, contentTypes, acceptTypes, range, symbolLabel);
21+
this.methodSignature = methodSignature;
1922
}
23+
24+
public String getMethodSignature() {
25+
return methodSignature;
26+
}
27+
2028
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public static void indexRequestMapping(Bean controller, Annotation node, SpringI
8787

8888
if (node.getParent() instanceof MethodDeclaration) {
8989
try {
90+
String methodSignature = ASTUtils.getMethodSignature((MethodDeclaration) node.getParent(), true);
91+
9092
Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength()));
9193
String[] path = getPath(node, context);
9294
String[] parentPath = getParentPath(node, context);
@@ -105,7 +107,7 @@ public static void indexRequestMapping(Bean controller, Annotation node, SpringI
105107
WorkspaceSymbol symbol = RouteUtils.createRouteSymbol(location, p, methods, contentTypes, acceptTypes);
106108

107109
// index element for request mapping
108-
RequestMappingIndexElement requestMappingIndexElement = new RequestMappingIndexElement(p, methods, contentTypes, acceptTypes, location.getRange(), symbol.getName());
110+
RequestMappingIndexElement requestMappingIndexElement = new RequestMappingIndexElement(p, methods, contentTypes, acceptTypes, location.getRange(), symbol.getName(), methodSignature);
109111
controller.addChild(requestMappingIndexElement);
110112
});
111113

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class WebfluxHandlerMethodIndexElement extends RequestMappingIndexElement
1919

2020
public WebfluxHandlerMethodIndexElement(String handlerClass, String handlerMethod, String path, String[] httpMethods, String[] contentTypes, String[] acceptTypes,
2121
Range range, String symbolLabel) {
22-
super(path, httpMethods, contentTypes, acceptTypes, range, symbolLabel);
22+
super(path, httpMethods, contentTypes, acceptTypes, range, symbolLabel, null);
2323

2424
this.handlerClass = handlerClass;
2525
this.handlerMethod = handlerMethod;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class SpringIndexerJava implements SpringIndexer {
9494

9595
// whenever the implementation of the indexer changes in a way that the stored data in the cache is no longer valid,
9696
// we need to change the generation - this will result in a re-indexing due to no up-to-date cache data being found
97-
private static final String GENERATION = "GEN-24";
97+
private static final String GENERATION = "GEN-25";
9898
private static final String INDEX_FILES_TASK_ID = "index-java-source-files-task-";
9999

100100
private static final String SYMBOL_KEY = "symbols";

0 commit comments

Comments
 (0)