Skip to content

Commit 4aa31a8

Browse files
committed
added location information to class-level stereotype nodes
1 parent c6e5a39 commit 4aa31a8

File tree

7 files changed

+64
-44
lines changed

7 files changed

+64
-44
lines changed

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

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.function.BiConsumer;
2424
import java.util.function.Consumer;
2525

26-
import org.eclipse.lsp4j.Location;
27-
import org.eclipse.lsp4j.Range;
2826
import org.jmolecules.stereotype.api.Stereotype;
2927
import org.jmolecules.stereotype.tooling.LabelProvider;
3028
import org.jmolecules.stereotype.tooling.MethodNodeContext;
@@ -41,7 +39,7 @@
4139
* @author Oliver Drotbohm
4240
* @author Martin Lippert
4341
*/
44-
class ToolsJsonNodeHandler implements NodeHandler<StereotypePackageElement, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, Object> {
42+
public class ToolsJsonNodeHandler implements NodeHandler<StereotypePackageElement, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, Object> {
4543

4644
public static final String ICON = "icon";
4745
public static final String TEXT = "text";
@@ -58,21 +56,13 @@ public ToolsJsonNodeHandler(LabelProvider<StereotypePackageElement, StereotypePa
5856
this.current = root;
5957
}
6058

61-
/*
62-
* (non-Javadoc)
63-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handleApplication(java.lang.Object)
64-
*/
6559
@Override
6660
public void handleApplication(StereotypePackageElement application) {
6761
this.root
6862
.withAttribute(TEXT, labels.getApplicationLabel(application))
6963
.withAttribute(ICON, "fa-application");
7064
}
7165

72-
/*
73-
* (non-Javadoc)
74-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handlePackage(java.lang.Object, org.jmolecules.stereotype.tooling.NodeContext)
75-
*/
7666
@Override
7767
public void handlePackage(StereotypePackageElement pkg, NodeContext context) {
7868

@@ -81,55 +71,35 @@ public void handlePackage(StereotypePackageElement pkg, NodeContext context) {
8171
.withAttribute(TEXT, labels.getPackageLabel(pkg)));
8272
}
8373

84-
/*
85-
* (non-Javadoc)
86-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handleStereotype(org.jmolecules.stereotype.api.Stereotype, org.jmolecules.stereotype.tooling.NestingLevel)
87-
*/
8874
@Override
8975
public void handleStereotype(Stereotype stereotype, NodeContext context) {
9076

9177
addChild(node -> node.withAttribute(ICON, "fa-stereotype")
9278
.withAttribute(TEXT, labels.getSterotypeLabel(stereotype)));
9379
}
9480

95-
/*
96-
* (non-Javadoc)
97-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handleType(java.lang.Object, org.jmolecules.stereotype.tooling.NestingLevel)
98-
*/
9981
@Override
10082
public void handleType(StereotypeClassElement type, NodeContext context) {
10183
addChild(node -> node
10284
.withAttribute(TEXT, labels.getTypeLabel(type))
103-
.withAttribute("location", new Location("sampleUri", new Range()))
85+
.withAttribute("location", type.getLocation())
10486
);
10587
}
10688

10789
public Node createNested() {
10890
return new Node(this.current);
10991
}
11092

111-
/*
112-
* (non-Javadoc)
113-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handleMethod(java.lang.Object, org.jmolecules.stereotype.tooling.NestingLevel, boolean)
114-
*/
11593
@Override
11694
public void handleMethod(StereotypeMethodElement method, MethodNodeContext<StereotypeClassElement> context) {
11795
addChildFoo(node -> node.withAttribute("title", labels.getMethodLabel(method, context.getContextualType())));
11896
}
11997

120-
/*
121-
* (non-Javadoc)
122-
* @see org.jmolecules.stereotype.tooling.NodeHandler#handleCustom(java.lang.Object, org.jmolecules.stereotype.tooling.NodeContext)
123-
*/
12498
@Override
12599
public void handleCustom(Object custom, NodeContext context) {
126100
addChild(node -> customHandler.accept(node, custom));
127101
}
128102

129-
/*
130-
* (non-Javadoc)
131-
* @see org.jmolecules.stereotype.tooling.NodeHandler#postGroup()
132-
*/
133103
@Override
134104
public void postGroup() {
135105
this.current = this.current.parent;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,31 @@
1313
import java.util.List;
1414
import java.util.Set;
1515

16+
import org.eclipse.lsp4j.Location;
1617
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement;
1718

1819
public class StereotypeClassElement extends AbstractSpringIndexElement implements StereotypeAnnotatedElement {
1920

2021
private final String type;
22+
private final Location location;
23+
2124
private final Set<String> supertypes;
2225
private List<String> annotationTypes;
2326

24-
public StereotypeClassElement(String type, Set<String> supertypes, List<String> annotationTypes) {
27+
public StereotypeClassElement(String type, Location location, Set<String> supertypes, List<String> annotationTypes) {
2528
this.type = type;
29+
this.location = location;
2630
this.supertypes = supertypes;
2731
this.annotationTypes = annotationTypes;
2832
}
2933

3034
public String getType() {
3135
return type;
3236
}
37+
38+
public Location getLocation() {
39+
return location;
40+
}
3341

3442
public boolean doesImplement(String fqn) {
3543
if (type.equals(fqn)) {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.eclipse.jdt.core.dom.ITypeBinding;
2727
import org.eclipse.jdt.core.dom.MethodDeclaration;
2828
import org.eclipse.jdt.core.dom.PackageDeclaration;
29+
import org.eclipse.jdt.core.dom.SimpleName;
2930
import org.eclipse.jdt.core.dom.TypeDeclaration;
31+
import org.eclipse.lsp4j.Location;
3032
import org.jmolecules.stereotype.catalog.StereotypeDefinition.Assignment.Type;
3133
import org.slf4j.Logger;
3234
import org.slf4j.LoggerFactory;
@@ -36,6 +38,7 @@
3638
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
3739
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
3840
import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJavaContext;
41+
import org.springframework.ide.vscode.commons.util.BadLocationException;
3942
import org.springframework.ide.vscode.commons.util.text.TextDocument;
4043

4144
import com.google.common.collect.Streams;
@@ -68,8 +71,7 @@ public void addSymbols(Annotation node, ITypeBinding typeBinding, Collection<ITy
6871

6972
@Override
7073
public void addSymbols(MethodDeclaration methodDeclaration, SpringIndexerJavaContext context, TextDocument doc) {
71-
// TODO Auto-generated method stub
72-
SymbolProvider.super.addSymbols(methodDeclaration, context, doc);
74+
// TODO
7375
}
7476

7577
@Override
@@ -98,6 +100,15 @@ public void addSymbols(PackageDeclaration packageDeclaration, SpringIndexerJavaC
98100

99101
@Override
100102
public void addSymbols(TypeDeclaration typeDeclaration, SpringIndexerJavaContext context, TextDocument doc) {
103+
try {
104+
createStereotypeElementForType(typeDeclaration, context, doc);
105+
}
106+
catch (BadLocationException e) {
107+
log.error("error identifying location of type declaration", e);
108+
}
109+
}
110+
111+
private void createStereotypeElementForType(TypeDeclaration typeDeclaration, SpringIndexerJavaContext context, TextDocument doc) throws BadLocationException {
101112
ITypeBinding typeBinding = typeDeclaration.resolveBinding();
102113
if (typeBinding == null) {
103114
return;
@@ -141,10 +152,11 @@ public void addSymbols(TypeDeclaration typeDeclaration, SpringIndexerJavaContext
141152
.map(binding -> binding.getAnnotationType().getQualifiedName())
142153
.toList();
143154

144-
context.getBeans().add(new CachedBean(context.getDocURI(), new StereotypeClassElement(qualifiedName, supertypes, annotationTypes)));
145-
155+
SimpleName astNodeForLocation = typeDeclaration.getName();
156+
Location location = new Location(doc.getUri(), doc.toRange(astNodeForLocation.getStartPosition(), astNodeForLocation.getLength()));
146157

147-
// }
158+
StereotypeClassElement indexElement = new StereotypeClassElement(qualifiedName, location, supertypes, annotationTypes);
159+
context.getBeans().add(new CachedBean(context.getDocURI(), indexElement));
148160
}
149161

150162
private boolean isStereotype(TypeDeclaration typeDeclaration, ITypeBinding binding) {

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
@@ -93,7 +93,7 @@ public class SpringIndexerJava implements SpringIndexer {
9393

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

9999
private static final String SYMBOL_KEY = "symbols";

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/stereotypes/StereotypesIndexerTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static org.junit.Assert.assertEquals;
1414
import static org.junit.Assert.assertTrue;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1516

1617
import java.io.File;
1718
import java.util.Collection;
@@ -21,6 +22,7 @@
2122
import java.util.concurrent.TimeUnit;
2223
import java.util.function.BiConsumer;
2324

25+
import org.eclipse.lsp4j.Location;
2426
import org.eclipse.lsp4j.TextDocumentIdentifier;
2527
import org.jmolecules.stereotype.api.Stereotype;
2628
import org.jmolecules.stereotype.catalog.StereotypeDefinition;
@@ -82,6 +84,31 @@ void testStuff() throws Exception {
8284
// assertEquals(1, stereotypeNodes.size());
8385
}
8486

87+
@Test
88+
void testPackageElement() throws Exception {
89+
List<StereotypePackageElement> packages = springIndex.getNodesOfType(StereotypePackageElement.class);
90+
assertEquals(2, packages.size()); // package nodes are created for package declarations in package-info.java files only
91+
}
92+
93+
@Test
94+
void testLocationInformationForTypeElement() throws Exception {
95+
String docUri = directory.toPath().resolve("src/main/java/example/application/SampleController.java").toUri().toString();
96+
97+
StereotypeClassElement element = springIndex.getNodesOfType(StereotypeClassElement.class).stream()
98+
.filter(node -> node.getType().equals("example.application.SampleController"))
99+
.findFirst()
100+
.get();
101+
102+
assertNotNull(element);
103+
assertEquals("example.application.SampleController", element.getType());
104+
105+
Location location = element.getLocation();
106+
assertNotNull(location);
107+
assertEquals(docUri, location.getUri());
108+
assertEquals(7, location.getRange().getStart().getLine());
109+
assertEquals(7, location.getRange().getEnd().getLine());
110+
}
111+
85112
@Test
86113
void testFromPackageIndexBasedFactory() {
87114

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/stereotypes/ToolsJsonNodeHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@
1616

1717
package org.springframework.ide.vscode.boot.java.stereotypes;
1818

19-
import net.minidev.json.JSONObject;
20-
2119
import java.util.ArrayList;
2220
import java.util.LinkedHashMap;
2321
import java.util.List;
2422
import java.util.Map;
2523
import java.util.function.BiConsumer;
2624
import java.util.function.Consumer;
2725

28-
import org.eclipse.lsp4j.Location;
29-
import org.eclipse.lsp4j.Range;
3026
import org.jmolecules.stereotype.api.Stereotype;
3127
import org.jmolecules.stereotype.tooling.LabelProvider;
3228
import org.jmolecules.stereotype.tooling.MethodNodeContext;
3329
import org.jmolecules.stereotype.tooling.NodeContext;
3430
import org.jmolecules.stereotype.tooling.NodeHandler;
3531

32+
import net.minidev.json.JSONObject;
33+
3634
/**
3735
* @author Oliver Drotbohm
3836
* @author Martin Lippert
@@ -94,7 +92,7 @@ public void handleStereotype(Stereotype stereotype, NodeContext context) {
9492
public void handleType(StereotypeClassElement type, NodeContext context) {
9593
addChild(node -> node
9694
.withAttribute(TEXT, labels.getTypeLabel(type))
97-
.withAttribute("location", new Location("sampleUri", new Range()))
95+
.withAttribute("location", type.getLocation())
9896
);
9997
}
10098

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package example.application.empty;
2+
3+
public class EmptyClass {
4+
5+
}

0 commit comments

Comments
 (0)