Allow Document symbol handler to detect whether ...

hierarchical document symbols are supported by the client.
This commit is contained in:
Kris De Volder
2019-02-08 11:26:35 -08:00
parent 9ac173feb6
commit b391a111a5
4 changed files with 21 additions and 26 deletions

View File

@@ -14,7 +14,6 @@ import java.lang.management.ManagementFactory;
import java.net.URI;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -133,10 +132,9 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
private LanguageServerTestListener testListener;
private boolean hasCompletionSnippetSupport;
private boolean hasExecuteCommandSupport;
private boolean hasFileWatcherRegistrationSupport;
private boolean hasHierarchicalDocumentSymbolSupport;
private Consumer<InitializeParams> initializeHandler;
private CompletableFuture<Void> initialized = new CompletableFuture<Void>();
@@ -157,6 +155,7 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
private String completionTriggerCharacters = null;
@Override
public void connect(LanguageClient _client) {
this.client = (STS4LanguageClient) _client;
@@ -260,6 +259,7 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
this.hasCompletionSnippetSupport = safeGet(false, () -> params.getCapabilities().getTextDocument().getCompletion().getCompletionItem().getSnippetSupport());
this.hasExecuteCommandSupport = safeGet(false, () -> params.getCapabilities().getWorkspace().getExecuteCommand()!=null);
this.hasFileWatcherRegistrationSupport = safeGet(false, () -> params.getCapabilities().getWorkspace().getDidChangeWatchedFiles().getDynamicRegistration());
this.hasHierarchicalDocumentSymbolSupport = safeGet(false, () -> params.getCapabilities().getTextDocument().getDocumentSymbol().getHierarchicalDocumentSymbolSupport());
log.debug("workspaceRoots = "+getWorkspaceService().getWorkspaceRoots());
log.debug("hasCompletionSnippetSupport = "+hasCompletionSnippetSupport);
log.debug("hasExecuteCommandSupport = "+hasExecuteCommandSupport);
@@ -425,6 +425,9 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
public final boolean hasLazyCompletionResolver() {
return completionResolver!=null;
}
public boolean hasHierarchicalDocumentSymbolSupport() {
return hasHierarchicalDocumentSymbolSupport;
}
private boolean hasDocumentSymbolHandler() {
return getTextDocumentService().hasDocumentSymbolHandler();
@@ -479,25 +482,6 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
return getWorkspaceService().getWorkspaceRoots();
}
// /**
// * Deprecated, shouldn't use and should be removed. Anyone calling this
// * will have problems handling multi-root workspaces.
// * <p>
// * Use getWorkspaceRoots instead.
// */
// @Deprecated
// public Path getWorkspaceRoot() {
// try {
// Optional<WorkspaceFolder> firstRoot = getWorkspaceRoots().stream().findFirst();
// if (firstRoot.isPresent()) {
// return new File(new URI(firstRoot.get().getUri())).toPath();
// }
// } catch (Exception e) {
// Log.log(e);
// }
// return null;
// }
@Override
public synchronized SimpleTextDocumentService getTextDocumentService() {
if (tds==null) {
@@ -728,4 +712,5 @@ public final class SimpleLanguageServer implements Sts4LanguageServer, LanguageC
public void setCompletionTriggerCharacters(String completionTriggerCharacters) {
this.completionTriggerCharacters = completionTriggerCharacters;
}
}

View File

@@ -15,10 +15,15 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Provider;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.util.DocumentSymbolHandler;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.util.Assert;
@@ -30,6 +35,7 @@ import org.springframework.ide.vscode.commons.yaml.ast.NodeUtil;
import org.springframework.ide.vscode.commons.yaml.schema.YType;
import org.yaml.snakeyaml.nodes.Node;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
@@ -44,15 +50,19 @@ import com.google.common.collect.ImmutableSet;
*/
public class TypeBasedYamlSymbolHandler implements DocumentSymbolHandler {
final static Logger logger = LoggerFactory.getLogger(TypeBasedYamlSymbolHandler.class);
private ASTTypeCache astTypeCache;
private Set<YType> definitionTypes;
private SimpleTextDocumentService documents;
private Supplier<Boolean> hiearchicalSymbolSupport;
public TypeBasedYamlSymbolHandler(SimpleTextDocumentService documents, ASTTypeCache astTypeCache, Collection<YType> definitionTypes) {
public TypeBasedYamlSymbolHandler(SimpleTextDocumentService documents, ASTTypeCache astTypeCache, Collection<YType> definitionTypes, Supplier<Boolean> hasHierarchicalSymbolSupport) {
Assert.isTrue(!definitionTypes.isEmpty()); // If there's no interesting types then you are better of using DocumentSymbolHandler.NO_SYMBOLS
this.documents = documents;
this.astTypeCache = astTypeCache;
this.definitionTypes = ImmutableSet.copyOf(definitionTypes);
this.hiearchicalSymbolSupport = hasHierarchicalSymbolSupport;
for (YType yType : definitionTypes) {
astTypeCache.addInterestingType(yType);
}
@@ -67,7 +77,7 @@ public class TypeBasedYamlSymbolHandler implements DocumentSymbolHandler {
try {
builder.add(createSymbol(doc, entry.getKey(), entry.getValue()));
} catch (Exception e) {
Log.log(e);
logger.error("", e);
}
}
}

View File

@@ -79,7 +79,7 @@ public class ConcourseLanguageServerInitializer implements InitializingBean {
reconcileEngine.setTypeCollector(models.getAstTypeCache());
this.symbolHandler = CollectionUtil.hasElements(definitionTypes)
? new TypeBasedYamlSymbolHandler(server.getTextDocumentService(), models.getAstTypeCache(), definitionTypes)
? new TypeBasedYamlSymbolHandler(server.getTextDocumentService(), models.getAstTypeCache(), definitionTypes, server::hasHierarchicalDocumentSymbolSupport)
: DocumentSymbolHandler.NO_SYMBOLS;
}

View File

@@ -93,7 +93,7 @@ public class ManifestYamlLanguageServerInitializer implements InitializingBean {
ASTTypeCache astTypeCache = new ASTTypeCache();
engine.setTypeCollector(astTypeCache);
documents.onDocumentSymbol(new TypeBasedYamlSymbolHandler(documents, astTypeCache, schema.getDefinitionTypes()));
documents.onDocumentSymbol(new TypeBasedYamlSymbolHandler(documents, astTypeCache, schema.getDefinitionTypes(), server::hasHierarchicalDocumentSymbolSupport));
documents.onDidChangeContent(params -> {
validateOnDocumentChange(engine, params.getDocument());