Skip to content

Commit

Permalink
GH-1119: document symbol computation re-uses compilation unit cache i…
Browse files Browse the repository at this point in the history
…nstead of creating its own parser and AST
  • Loading branch information
martinlippert committed Oct 20, 2023
1 parent 58635fd commit 29f3974
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.boot.java.reconcilers.JdtReconciler;
import org.springframework.ide.vscode.boot.java.utils.CompilationUnitCache;
import org.springframework.ide.vscode.boot.java.utils.DocumentDescriptor;
import org.springframework.ide.vscode.boot.java.utils.SpringFactoriesIndexer;
import org.springframework.ide.vscode.boot.java.utils.SpringIndexer;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class SpringSymbolIndex implements InitializingBean, SpringIndex {
@Autowired FutureProjectFinder futureProjectFinder;
@Autowired SpringMetamodelIndex springIndex;
@Autowired JdtReconciler jdtReconciler;
@Autowired CompilationUnitCache cuCache;

private static final String QUERY_PARAM_LOCATION_PREFIX = "locationPrefix:";

Expand Down Expand Up @@ -246,10 +248,8 @@ public void removeSymbols(IJavaProject project, String docURI) {
namespaceHandler.put("http://www.springframework.org/schema/beans", new SpringIndexerXMLNamespaceHandlerBeans());
springIndexerXML = new SpringIndexerXML(handler, namespaceHandler, this.cache, projectFinder());


BiFunction<AtomicReference<TextDocument>, BiConsumer<String, Diagnostic>, IProblemCollector> problemCollectorFactory = (docRef, aggregator) -> server.createProblemCollector(docRef, aggregator);
springIndexerJava = new SpringIndexerJava(handler, specificProviders, this.cache, projectFinder(), server.getProgressService(), jdtReconciler, problemCollectorFactory, config.getJavaValidationSettingsJson());

springIndexerJava = new SpringIndexerJava(handler, specificProviders, this.cache, projectFinder(), server.getProgressService(), jdtReconciler, problemCollectorFactory, config.getJavaValidationSettingsJson(), cuCache);
factoriesIndexer = new SpringFactoriesIndexer(handler, cache);

this.indexers = new SpringIndexer[] {springIndexerJava, factoriesIndexer};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static enum SCAN_PASS {
private final IndexCache cache;
private final JavaProjectFinder projectFinder;
private final ProgressService progressService;
private final CompilationUnitCache cuCache;

private boolean scanTestJavaSources = false;
private JsonObject validationSeveritySettings;
Expand All @@ -113,11 +114,11 @@ public static enum SCAN_PASS {
private final SpringIndexerJavaDependencyTracker dependencyTracker = new SpringIndexerJavaDependencyTracker();
private final BiFunction<AtomicReference<TextDocument>, BiConsumer<String, Diagnostic>, IProblemCollector> problemCollectorCreator;


public SpringIndexerJava(SymbolHandler symbolHandler, AnnotationHierarchyAwareLookup<SymbolProvider> symbolProviders, IndexCache cache,
JavaProjectFinder projectFimder, ProgressService progressService, JdtReconciler jdtReconciler,
BiFunction<AtomicReference<TextDocument>, BiConsumer<String, Diagnostic>, IProblemCollector> problemCollectorCreator,
JsonObject validationSeveritySettings) {
JsonObject validationSeveritySettings, CompilationUnitCache cuCache) {
this.symbolHandler = symbolHandler;
this.symbolProviders = symbolProviders;
this.reconciler = jdtReconciler;
Expand All @@ -127,6 +128,7 @@ public SpringIndexerJava(SymbolHandler symbolHandler, AnnotationHierarchyAwareLo

this.problemCollectorCreator = problemCollectorCreator;
this.validationSeveritySettings = validationSeveritySettings;
this.cuCache = cuCache;
}

public SpringIndexerJavaDependencyTracker getDependencyTracker() {
Expand Down Expand Up @@ -322,44 +324,36 @@ public void accept(String docURI, Diagnostic diagnostic) {
}

public List<EnhancedSymbolInformation> computeSymbols(IJavaProject project, String docURI, String content) throws Exception {
final boolean ignoreMethodBodies = false;
ASTParser parser = createParser(project, ignoreMethodBodies);

if (content != null) {
String unitName = docURI.substring(docURI.lastIndexOf("/"));
parser.setUnitName(unitName);
log.debug("Scan file: {}", unitName);
parser.setSource(content.toCharArray());

CompilationUnit cu = (CompilationUnit) parser.createAST(null);

if (cu != null) {
URI uri = URI.create(docURI);

return cuCache.withCompilationUnit(project, uri, cu -> {
List<CachedSymbol> generatedSymbols = new ArrayList<CachedSymbol>();
List<CachedBean> generatedBeans = new ArrayList<CachedBean>();

IProblemCollector voidProblemCollector = new IProblemCollector() {
@Override
public void endCollecting() {
}

@Override
public void beginCollecting() {
}

@Override
public void accept(ReconcileProblem problem) {
}
};

AtomicReference<TextDocument> docRef = new AtomicReference<>();
String file = UriUtil.toFileString(docURI);
SpringIndexerJavaContext context = new SpringIndexerJavaContext(project, cu, docURI, file,
0, docRef, content, generatedSymbols, generatedBeans, voidProblemCollector, SCAN_PASS.ONE, new ArrayList<>(), !ignoreMethodBodies);
0, docRef, content, generatedSymbols, generatedBeans, voidProblemCollector, SCAN_PASS.ONE, new ArrayList<>(), true);

scanAST(context);

return generatedSymbols.stream().map(s -> s.getEnhancedSymbol()).collect(Collectors.toList());
}
});
}

return Collections.emptyList();
Expand Down

0 comments on commit 29f3974

Please sign in to comment.