Skip to content

Commit

Permalink
GH-1431: let bean index elements create document symbols on-demand
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlippert committed Feb 20, 2025
1 parent 888669a commit bb51280
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 VMware, Inc.
* Copyright (c) 2023, 2025 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,11 +12,13 @@

import java.util.Set;

import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolKind;

import com.google.gson.Gson;

public class Bean extends AbstractSpringIndexElement {
public class Bean extends AbstractSpringIndexElement implements SymbolElement {

private final String name;
private final String type;
Expand All @@ -25,6 +27,7 @@ public class Bean extends AbstractSpringIndexElement {
private final Set<String> supertypes;
private final AnnotationMetadata[] annotations;
private final boolean isConfiguration;
private final String symbolLabel;

public Bean(
String name,
Expand All @@ -33,12 +36,14 @@ public Bean(
InjectionPoint[] injectionPoints,
Set<String> supertypes,
AnnotationMetadata[] annotations,
boolean isConfiguration) {
boolean isConfiguration,
String symbolLabel) {

this.name = name;
this.type = type;
this.location = location;
this.isConfiguration = isConfiguration;
this.symbolLabel = symbolLabel;

if (injectionPoints != null && injectionPoints.length == 0) {
this.injectionPoints = DefaultValues.EMPTY_INJECTION_POINTS;
Expand Down Expand Up @@ -93,14 +98,30 @@ public boolean isConfiguration() {
return isConfiguration;
}

public Set<String> getSupertypes() {
return supertypes;
}

public String getSymbolLabel() {
return symbolLabel;
}

@Override
public DocumentSymbol getDocumentSymbol() {
DocumentSymbol symbol = new DocumentSymbol();

symbol.setName(this.symbolLabel);
symbol.setKind(SymbolKind.Interface);
symbol.setRange(this.location.getRange());
symbol.setSelectionRange(this.location.getRange());

return symbol;
}

@Override
public String toString() {
Gson gson = new Gson();
return gson.toJson(this);
}

public Set<String> getSupertypes() {
return supertypes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,10 @@ public Bean deserialize(JsonElement json, Type type, JsonDeserializationContext

JsonElement isConfigurationObject = parsedObject.get("isConfiguration");
boolean isConfiguration = context.deserialize(isConfigurationObject, boolean.class);

String symbolLabel = parsedObject.get("symbolLabel").getAsString();

return new Bean(beanName, beanType, location, injectionPoints, supertypes, annotations, isConfiguration);
return new Bean(beanName, beanType, location, injectionPoints, supertypes, annotations, isConfiguration, symbolLabel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,14 @@ public Bean deserialize(JsonElement json, Type type, JsonDeserializationContext
JsonElement isConfigurationObject = parsedObject.get("isConfiguration");
boolean isConfiguration = context.deserialize(isConfigurationObject, boolean.class);

String symbolLabel = parsedObject.get("symbolLabel").getAsString();

JsonElement childrenObject = parsedObject.get("children");
Type childrenListType = TypeToken.getParameterized(List.class, SpringIndexElement.class).getType();
List<SpringIndexElement> children = context.deserialize(childrenObject, childrenListType);

Bean bean = new Bean(beanName, beanType, location, injectionPoints, supertypes, annotations, isConfiguration);
Bean bean = new Bean(beanName, beanType, location, injectionPoints, supertypes, annotations, isConfiguration, symbolLabel);

for (SpringIndexElement springIndexElement : children) {
bean.addChild(springIndexElement);
}
Expand All @@ -685,6 +688,7 @@ public JsonElement serialize(Bean src, Type typeOfSrc, JsonSerializationContext
bean.add("annotations", context.serialize(src.getAnnotations()));

bean.addProperty("isConfiguration", src.isConfiguration());
bean.addProperty("symbolLabel", src.getSymbolLabel());

Type childrenListType = TypeToken.getParameterized(List.class, SpringIndexElement.class).getType();
bean.add("children", context.serialize(src.getChildren(), childrenListType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void addSymbols(Annotation node, ITypeBinding typeBinding, Collection<ITy
Collection<Annotation> annotationsOnMethod = ASTUtils.getAnnotations(method);
AnnotationMetadata[] annotations = ASTUtils.getAnnotationsMetadata(annotationsOnMethod, doc);

Bean beanDefinition = new Bean(nameAndRegion.getT1(), beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, false);
Bean beanDefinition = new Bean(nameAndRegion.getT1(), beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, false, symbol.getName());
if (childElements.size() > 0) {
for (SpringIndexElement springIndexElement : childElements) {
beanDefinition.addChild(springIndexElement);
Expand Down Expand Up @@ -174,7 +174,7 @@ protected void addSymbolsPass1(TypeDeclaration typeDeclaration, SpringIndexerJav

InjectionPoint[] injectionPoints = ASTUtils.findInjectionPoints(typeDeclaration, doc);

Bean beanDefinition = new Bean(beanName, concreteBeanType.getQualifiedName(), beanLocation, injectionPoints, supertypes, annotations, false);
Bean beanDefinition = new Bean(beanName, concreteBeanType.getQualifiedName(), beanLocation, injectionPoints, supertypes, annotations, false, symbol.getName());
context.getBeans().add(new CachedBean(context.getDocURI(), beanDefinition));

} catch (BadLocationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.InjectionPoint;
import org.springframework.ide.vscode.commons.protocol.spring.SimpleSymbolElement;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.text.DocumentRegion;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
Expand All @@ -65,6 +66,7 @@ protected void addSymbolsPass1(Annotation node, ITypeBinding annotationType, Col
else if (Annotations.NAMED_ANNOTATIONS.contains(annotationType.getQualifiedName())) {
WorkspaceSymbol symbol = DefaultSymbolProvider.provideDefaultSymbol(node, doc);
context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), symbol));
context.getBeans().add(new CachedBean(context.getDocURI(), new SimpleSymbolElement(symbol)));
}
}
catch (Exception e) {
Expand Down Expand Up @@ -107,7 +109,7 @@ protected void createSymbol(Annotation node, ITypeBinding annotationType, Collec
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null, null)))
.toArray(AnnotationMetadata[]::new);

Bean beanDefinition = new Bean(beanName, beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, isConfiguration);
Bean beanDefinition = new Bean(beanName, beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, isConfiguration, symbol.getName());

// type implements event listener - move those already created event index elements under the bean node
List<CachedBean> alreadyCreatedEventListenerChilds = context.getBeans().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private Two<WorkspaceSymbol, Bean> createSymbol(Annotation node, ITypeBinding an
.map(an -> new AnnotationMetadata(an.getQualifiedName(), true, null, null)))
.toArray(AnnotationMetadata[]::new);

Bean beanDefinition = new Bean(beanName, beanType == null ? "" : beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, false);
Bean beanDefinition = new Bean(beanName, beanType == null ? "" : beanType.getQualifiedName(), location, injectionPoints, supertypes, annotations, false, symbol.getName());

return Tuple.two(symbol, beanDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void addSymbolsPass1(TypeDeclaration typeDeclaration, SpringIndexerJav
Collection<Annotation> annotationsOnMethod = ASTUtils.getAnnotations(typeDeclaration);
AnnotationMetadata[] annotations = ASTUtils.getAnnotationsMetadata(annotationsOnMethod, doc);

Bean beanDefinition = new Bean(beanName, concreteRepoType, location, injectionPoints, supertypes, annotations, false);
Bean beanDefinition = new Bean(beanName, concreteRepoType, location, injectionPoints, supertypes, annotations, false, symbol.getName());

context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), symbol));
context.getBeans().add(new CachedBean(context.getDocURI(), beanDefinition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ else if (name != null && name.equals("class")) {
generatedSymbols.add(cachedSymbol);

// TODO: bean index
generatedBeans.add(new CachedBean(docURI, new Bean(beanID, fqBeanClass, location, null, null, null, false)));
generatedBeans.add(new CachedBean(docURI, new Bean(beanID, fqBeanClass, location, null, null, null, false, symbol.getName())));
}
}

Expand Down
Loading

0 comments on commit bb51280

Please sign in to comment.