GH-1431: let request mapping index elements create document symbols on-demand

This commit is contained in:
Martin Lippert
2025-02-20 14:46:07 +01:00
parent bb51280782
commit 6f66310f02
2 changed files with 49 additions and 8 deletions

View File

@@ -10,20 +10,28 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.requestmapping;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolKind;
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement;
import org.springframework.ide.vscode.commons.protocol.spring.SymbolElement;
public class RequestMappingIndexElement extends AbstractSpringIndexElement {
public class RequestMappingIndexElement extends AbstractSpringIndexElement implements SymbolElement {
private final String path;
private final String[] httpMethods;
private final String[] contentTypes;
private final String[] acceptTypes;
private final String symbolLabel;
private final Range range;
public RequestMappingIndexElement(String path, String[] httpMethods, String[] contentTypes, String[] acceptTypes) {
public RequestMappingIndexElement(String path, String[] httpMethods, String[] contentTypes, String[] acceptTypes, Range range, String symbolLabel) {
this.path = path;
this.httpMethods = httpMethods;
this.contentTypes = contentTypes;
this.acceptTypes = acceptTypes;
this.range = range;
this.symbolLabel = symbolLabel;
}
public String getPath() {
@@ -42,4 +50,16 @@ public class RequestMappingIndexElement extends AbstractSpringIndexElement {
return acceptTypes;
}
@Override
public DocumentSymbol getDocumentSymbol() {
DocumentSymbol symbol = new DocumentSymbol();
symbol.setName(symbolLabel);
symbol.setKind(SymbolKind.Method);
symbol.setRange(range);
symbol.setSelectionRange(range);
return symbol;
}
}

View File

@@ -39,6 +39,8 @@ import org.springframework.ide.vscode.boot.java.handlers.AbstractSymbolProvider;
import org.springframework.ide.vscode.boot.java.utils.ASTUtils;
import org.springframework.ide.vscode.boot.java.utils.CachedSymbol;
import org.springframework.ide.vscode.boot.java.utils.SpringIndexerJavaContext;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
/**
@@ -77,20 +79,39 @@ public class RequestMappingSymbolProvider extends AbstractSymbolProvider {
context.getGeneratedSymbols().add(new CachedSymbol(context.getDocURI(), context.getLastModified(), symbol));
// index element for request mapping
List<CachedBean> beans = context.getBeans();
if (beans.size() > 0 ) {
CachedBean cachedBean = beans.get(beans.size() - 1);
if (cachedBean.getDocURI().equals(doc.getUri())) {
cachedBean.getBean().addChild(new RequestMappingIndexElement(p, methods, contentTypes, acceptTypes));
}
SpringIndexElement parent = getPotentialParentIndexNode(context);
if (parent != null) {
parent.addChild(new RequestMappingIndexElement(p, methods, contentTypes, acceptTypes, location.getRange(), symbol.getName()));
}
else {
context.getBeans().add(new CachedBean(doc.getUri(), new RequestMappingIndexElement(p, methods, contentTypes, acceptTypes, location.getRange(), symbol.getName())));
}
});
} catch (Exception e) {
log.error("problem occured while scanning for request mapping symbols from " + doc.getUri(), e);
}
}
}
private SpringIndexElement getPotentialParentIndexNode(SpringIndexerJavaContext context) {
List<CachedBean> beans = context.getBeans();
for (int i = beans.size() - 1; i >= 0; i--) {
CachedBean cachedBean = beans.get(i);
if (!cachedBean.getDocURI().equals(context.getDocURI())) {
return null;
}
if (cachedBean.getBean() instanceof Bean bean) {
return bean;
}
}
return null;
}
private String combinePath(String parent, String path) {
String separator = !parent.endsWith("/") && !path.startsWith("/") && !path.isEmpty() ? "/" : "";
String resultPath = parent + separator + path;