implemented first steps towards symbol provider for @Component

This commit is contained in:
Martin Lippert
2017-09-18 12:12:10 +02:00
parent 3e42c461da
commit 399d496b57
3 changed files with 61 additions and 1 deletions

View File

@@ -10,8 +10,13 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
@@ -22,6 +27,49 @@ public class ComponentSymbolProvider implements SymbolProvider {
@Override
public SymbolInformation getSymbol(Annotation node, TextDocument doc) {
try {
StringBuilder symbolLabel = new StringBuilder();
symbolLabel.append("@Bean");
String beanName = getBeanName(node);
String beanType = getBeanType(node);
symbolLabel.append(' ');
symbolLabel.append(beanName);
symbolLabel.append(' ');
symbolLabel.append(beanType);
SymbolInformation symbol = new SymbolInformation(symbolLabel.toString(), SymbolKind.Interface,
new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength())));
return symbol;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String getBeanName(Annotation node) {
ASTNode parent = node.getParent();
if (parent instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) parent;
String beanName = type.getName().toString();
if (beanName.length() > 0 && Character.isUpperCase(beanName.charAt(0))) {
beanName = Character.toLowerCase(beanName.charAt(0)) + beanName.substring(1);
}
return beanName;
}
return null;
}
private String getBeanType(Annotation node) {
ASTNode parent = node.getParent();
if (parent instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) parent;
String returnType = type.resolveBinding().getQualifiedName();
return returnType;
}
return null;
}

View File

@@ -65,6 +65,7 @@ public class AnnotationIndexer {
public void reset() {
this.symbols.clear();
this.symbolsByDoc.clear();
}
public List<? extends SymbolInformation> getAllSymbols() {
@@ -167,7 +168,6 @@ public class AnnotationIndexer {
}
private void extractSymbolInformation(Annotation node, String docURI) throws Exception {
System.out.println("annotation found: " + node.toString());
ITypeBinding typeBinding = node.resolveTypeBinding();
if (typeBinding != null) {

View File

@@ -63,6 +63,18 @@ public class AnnotationIndexerBeansTest {
assertTrue(containsSymbol(symbols, "@Bean simpleBean org.test.BeanClass", uriPrefix + "/src/main/java/org/test/SimpleConfiguration.java", 8, 1, 8, 8));
}
@Test
public void testScanSimpleComponentClass() throws Exception {
AnnotationIndexer indexer = new AnnotationIndexer(projectFinder, symbolProviders);
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-indexing-beans/").toURI());
indexer.scanFiles(directory);
String uriPrefix = "file://" + directory.getAbsolutePath();
List<? extends SymbolInformation> symbols = indexer.getSymbols(uriPrefix + "/src/main/java/org/test/SimpleComponent.java");
assertEquals(1, symbols.size());
assertTrue(containsSymbol(symbols, "@Bean simpleComponent org.test.SimpleComponent", uriPrefix + "/src/main/java/org/test/SimpleComponent.java", 4, 0, 4, 10));
}
private boolean containsSymbol(List<? extends SymbolInformation> symbols, String name, String uri, int startLine, int startCHaracter, int endLine, int endCharacter) {
for (Iterator<? extends SymbolInformation> iterator = symbols.iterator(); iterator.hasNext();) {
SymbolInformation symbol = iterator.next();