Skip to content

Commit

Permalink
GH-1431: compute document symbols directly instead of going through w…
Browse files Browse the repository at this point in the history
…orkspace symbols
  • Loading branch information
martinlippert committed Feb 20, 2025
1 parent eb99148 commit f92a7a5
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2024 Pivotal, Inc.
* Copyright (c) 2018, 2025 Pivotal, 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 Down Expand Up @@ -27,14 +27,14 @@
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SemanticTokensLegend;
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -192,7 +192,7 @@ public List<InlayHint> handle(TextDocument doc, Range r, CancelChecker token) {
this.docSymbolHandler = new DocumentSymbolHandler() {

@Override
public List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params) {
public List<? extends DocumentSymbol> handle(DocumentSymbolParams params) {
TextDocument doc = getDoc(appContext, params.getTextDocument().getUri());
LanguageId language = doc.getLanguageId();
List<LanguageServerComponents> subComponents = componentsByLanguageId.get(language);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2025 Pivotal, 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,8 +12,8 @@

import java.util.List;

import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.WorkspaceSymbol;

import com.google.common.collect.ImmutableList;

Expand All @@ -22,6 +22,6 @@ public interface DocumentSymbolHandler {

DocumentSymbolHandler NO_SYMBOLS = (params) -> ImmutableList.of();

List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params);
List<? extends DocumentSymbol> handle(DocumentSymbolParams params);

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2024 VMware Inc.
* Copyright (c) 2016, 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 Down Expand Up @@ -74,7 +74,6 @@
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
Expand Down Expand Up @@ -433,21 +432,12 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume
//
// cancelToken.checkCanceled();
//
if (server.hasHierarchicalDocumentSymbolSupport() && h instanceof HierarchicalDocumentSymbolHandler) {
List<? extends DocumentSymbol> r = ((HierarchicalDocumentSymbolHandler)h).handleHierarchic(params);
List<? extends DocumentSymbol> r = h.handle(params);
//handle it when symbolHandler is sloppy and returns null instead of empty list.
return r == null
? ImmutableList.of()
: r.stream().map(symbolInfo -> Either.<SymbolInformation, DocumentSymbol>forRight(symbolInfo))
.collect(Collectors.toList());
} else {
List<? extends WorkspaceSymbol> r = h.handle(params);
//handle it when symbolHandler is sloppy and returns null instead of empty list.
return r == null
? ImmutableList.of()
: r.stream().map(symbolInfo -> Either.<SymbolInformation, DocumentSymbol>forLeft(new SymbolInformation(symbolInfo.getName(), symbolInfo.getKind(), symbolInfo.getLocation().getLeft(), symbolInfo.getContainerName())))
.collect(Collectors.toList());
}
: r.stream().map(symbol -> Either.<SymbolInformation, DocumentSymbol>forRight(symbol))
.collect(Collectors.toList());
});
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Pivotal, Inc.
* Copyright (c) 2019, 2025 Pivotal, 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 @@ -19,10 +19,9 @@
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.util.HierarchicalDocumentSymbolHandler;
import org.springframework.ide.vscode.commons.languageserver.util.DocumentSymbolHandler;
import org.springframework.ide.vscode.commons.util.Assert;
import org.springframework.ide.vscode.commons.util.text.DocumentRegion;
import org.springframework.ide.vscode.commons.util.text.IDocument;
Expand All @@ -40,7 +39,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public class TypeBasedYamlHierarchicalSymbolHandler implements HierarchicalDocumentSymbolHandler, ITypeCollector {
public class TypeBasedYamlHierarchicalSymbolHandler implements DocumentSymbolHandler, ITypeCollector {

private static final Logger log = LoggerFactory.getLogger(TypeBasedYamlHierarchicalSymbolHandler.class);

Expand Down Expand Up @@ -153,12 +152,7 @@ public TypeBasedYamlHierarchicalSymbolHandler(TypeBasedYamlSymbolHandler baseHan
}

@Override
public List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params) {
return baseHandler.handle(params);
}

@Override
public List<? extends DocumentSymbol> handleHierarchic(DocumentSymbolParams params) {
public List<? extends DocumentSymbol> handle(DocumentSymbolParams params) {
return outlineByUri.get(params.getTextDocument().getUri());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2021 Pivotal, Inc.
* Copyright (c) 2017, 2025 Pivotal, 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 @@ -15,11 +15,10 @@
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.util.DocumentSymbolHandler;
Expand Down Expand Up @@ -63,8 +62,8 @@ public TypeBasedYamlSymbolHandler(SimpleTextDocumentService documents, ASTTypeCa
}

@Override
public List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params) {
Builder<WorkspaceSymbol> builder = ImmutableList.builder();
public List<? extends DocumentSymbol> handle(DocumentSymbolParams params) {
Builder<DocumentSymbol> builder = ImmutableList.builder();

TextDocument doc = documents.getLatestSnapshot(params.getTextDocument().getUri());
if (doc != null) {
Expand All @@ -81,14 +80,17 @@ public List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params) {
return builder.build();
}

protected WorkspaceSymbol createSymbol(TextDocument doc, Node node, YType type) throws BadLocationException {
protected DocumentSymbol createSymbol(TextDocument doc, Node node, YType type) throws BadLocationException {
DocumentRegion region = NodeUtil.region(doc, node);
Location location = new Location(doc.getUri(), doc.toRange(region.getStart(), region.getLength()));
WorkspaceSymbol symbol = new WorkspaceSymbol();

Range range = doc.toRange(region.getStart(), region.getLength());

DocumentSymbol symbol = new DocumentSymbol();
symbol.setName(region.toString());
symbol.setKind(symbolKind(type));
symbol.setLocation(Either.forLeft(location));
symbol.setContainerName(containerName(type));
symbol.setRange(range);
symbol.setSelectionRange(range);

return symbol;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2019 Pivotal, Inc.
* Copyright (c) 2018, 2025 Pivotal, 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 @@ -14,7 +14,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.ide.vscode.commons.languageserver.LanguageServerRunner;
import org.springframework.ide.vscode.commons.languageserver.util.HierarchicalDocumentSymbolHandler;
import org.springframework.ide.vscode.commons.languageserver.util.DocumentSymbolHandler;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.util.LogRedirect;
Expand Down Expand Up @@ -48,7 +48,7 @@ public static void main(String[] args) throws Exception {
return new ASTTypeCache();
}

@Bean HierarchicalDocumentSymbolHandler documentSymbolHandler(SimpleTextDocumentService documents, ASTTypeCache astTypeCache, PipelineYmlSchema schema) {
@Bean DocumentSymbolHandler documentSymbolHandler(SimpleTextDocumentService documents, ASTTypeCache astTypeCache, PipelineYmlSchema schema) {
TypeBasedYamlSymbolHandler baseHandler = new TypeBasedYamlSymbolHandler(documents, astTypeCache, schema.getDefinitionTypes());
return new TypeBasedYamlHierarchicalSymbolHandler(baseHandler, schema.getHierarchicalDefinitionTypes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -42,6 +44,7 @@
import java.util.stream.Stream;

import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceSymbol;
Expand All @@ -51,6 +54,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ide.vscode.boot.index.SpringIndexToSymbolsConverter;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.index.cache.IndexCache;
import org.springframework.ide.vscode.boot.java.BootJavaLanguageServerComponents;
Expand Down Expand Up @@ -80,6 +84,7 @@
import org.springframework.ide.vscode.commons.languageserver.util.SimpleWorkspaceService;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.BeansParams;
import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement;
import org.springframework.ide.vscode.commons.protocol.spring.MatchingBeansParams;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndex;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
Expand Down Expand Up @@ -732,7 +737,99 @@ public List<? extends WorkspaceSymbol> getSymbols(String docURI) {
return Collections.emptyList();
}
}

public List<? extends DocumentSymbol> getDocumentSymbols(String docURI) {
List<DocumentSymbol> result = new ArrayList<>();

List<? extends WorkspaceSymbol> symbols = getSymbols(docURI);
for (WorkspaceSymbol symbol : symbols) {
DocumentSymbol docSymbol = new DocumentSymbol();
docSymbol.setName(symbol.getName());
docSymbol.setKind(symbol.getKind());
docSymbol.setRange(symbol.getLocation().getLeft().getRange());
docSymbol.setSelectionRange(symbol.getLocation().getLeft().getRange());
docSymbol.setTags(symbol.getTags());

result.add(docSymbol);
}

return result;
}

/*
public List<? extends WorkspaceSymbol> getSymbols(String docURI) {
List<WorkspaceSymbol> result = new ArrayList<>();
Deque<DocumentSymbol> remainingSymbols = new ArrayDeque<>();
List<? extends DocumentSymbol> documentSymbols = getDocumentSymbols(docURI);
remainingSymbols.addAll(documentSymbols);
while (!remainingSymbols.isEmpty()) {
DocumentSymbol documentSymbol = remainingSymbols.poll();
WorkspaceSymbol workspaceSymbol = new WorkspaceSymbol();
workspaceSymbol.setName(documentSymbol.getName());
workspaceSymbol.setKind(documentSymbol.getKind());
workspaceSymbol.setTags(documentSymbol.getTags());
Location location = new Location(docURI, documentSymbol.getRange());
workspaceSymbol.setLocation(Either.forLeft(location));
result.add(workspaceSymbol);
if (documentSymbol.getChildren() != null) {
remainingSymbols.addAll(documentSymbol.getChildren());
}
}
return result;
}
public List<? extends DocumentSymbol> getDocumentSymbols(String docURI) {
try {
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(docURI);
URI uri = URI.create(docURI);
CompletableFuture<IJavaProject> projectInitialized = futureProjectFinder.findFuture(uri).thenCompose(project -> projectInitializedFuture(project));
IJavaProject project = projectInitialized.get(15, TimeUnit.SECONDS);
ImmutableList.Builder<DocumentSymbol> builder = ImmutableList.builder();
if (project != null && doc != null) {
// Collect symbols from the opened document
synchronized(this) {
for (SpringIndexer indexer : this.indexers) {
if (indexer.isInterestedIn(docURI)) {
try {
List<DocumentSymbol> adhocDocumentSymbols = indexer.computeDocumentSymbols(project, docURI, doc.get());
builder.addAll(adhocDocumentSymbols);
} catch (Exception e) {
log.error("{}", e);
}
}
}
}
} else {
// Take symbols from the index if there is no opened document.
DocumentElement document = springIndex.getDocument(docURI);
if (document != null) {
List<SpringIndexElement> children = document.getChildren();
builder.addAll(SpringIndexToSymbolsConverter.createDocumentSymbols(children));
}
}
return builder.build();
} catch (Exception e) {
log.warn("", e);
return Collections.emptyList();
}
}
*/

@Override
public CompletableFuture<List<Bean>> beans(BeansParams params) {
String projectName = params.getProjectName();
Expand Down
Loading

0 comments on commit f92a7a5

Please sign in to comment.