added cancel handling to definition requests

This commit is contained in:
Martin Lippert
2021-02-26 12:16:33 +01:00
parent 28aea97b71
commit 607781779e
9 changed files with 87 additions and 19 deletions

View File

@@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
@@ -22,6 +23,7 @@ import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.definition.SimpleDefinitionFinder;
@@ -73,18 +75,31 @@ public class BoshDefintionFinder extends SimpleDefinitionFinder {
}
@Override
public List<LocationLink> handle(DefinitionParams params) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams params) {
try {
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(params);
if (doc != null) {
cancelToken.checkCanceled();
YamlFileAST ast = asts.getSafeAst(doc, false);
if (ast!=null) {
if (ast != null) {
cancelToken.checkCanceled();
Node refNode = ast.findNode(doc.toOffset(params.getPosition()));
if (refNode!=null) {
cancelToken.checkCanceled();
YType type = astTypes.getType(ast, refNode);
if (type!=null) {
if (type != null) {
cancelToken.checkCanceled();
Handler handler = handlers.get(type);
if (handler!=null) {
int start = refNode.getStartMark().getIndex();
int end = refNode.getEndMark().getIndex();
@@ -97,6 +112,8 @@ public class BoshDefintionFinder extends SimpleDefinitionFinder {
}
}
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
log.error("", e);;
}

View File

@@ -16,6 +16,7 @@ import java.util.List;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.util.DefinitionHandler;
@@ -39,7 +40,7 @@ public class SimpleDefinitionFinder implements DefinitionHandler {
}
@Override
public List<LocationLink> handle(DefinitionParams params) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams params) {
try {
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(params.getTextDocument().getUri());

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2020 Pivotal, Inc.
* Copyright (c) 2017, 2021 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
@@ -14,8 +14,9 @@ import java.util.List;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
@FunctionalInterface
public interface DefinitionHandler {
List<LocationLink> handle(DefinitionParams definitionParams);
List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams definitionParams);
}

View File

@@ -319,7 +319,10 @@ public class SimpleTextDocumentService implements TextDocumentService, DocumentE
DefinitionHandler h = this.definitionHandler;
if (h != null) {
return CompletableFutures.computeAsync(cancelToken -> {
List<LocationLink> locations = h.handle(definitionParams);
cancelToken.checkCanceled();
List<LocationLink> locations = h.handle(cancelToken, definitionParams);
if (locations == null) {
// vscode client does not like to receive null result. See: https://github.com/spring-projects/sts4/issues/309
locations = ImmutableList.of();

View File

@@ -88,14 +88,14 @@ public class LanguageServerAutoConf {
}
ImmutableMap<LanguageId, DefinitionHandler> immutableMap = ImmutableMap.copyOf(handlers);
return () -> documents.onDefinition((position) -> {
return () -> documents.onDefinition((cancelToken, position) -> {
TextDocument doc = documents.getLatestSnapshot(position);
if (doc != null) {
LanguageId language = doc.getLanguageId();
DefinitionHandler handler = immutableMap.get(language);
if (handler != null) {
return handler.handle(position);
return handler.handle(cancelToken, position);
}
}
return null;

View File

@@ -14,12 +14,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.definition.SimpleDefinitionFinder;
@@ -90,16 +92,26 @@ public class ConcourseDefinitionFinder extends SimpleDefinitionFinder {
}
@Override
public List<LocationLink> handle(DefinitionParams params) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams params) {
try {
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(params);
if (doc!=null) {
if (doc != null) {
cancelToken.checkCanceled();
YamlFileAST ast = asts.getSafeAst(doc, false);
if (ast!=null) {
if (ast != null) {
Node refNode = ast.findNode(doc.toOffset(params.getPosition()));
if (refNode!=null) {
if (refNode != null) {
cancelToken.checkCanceled();
YType type = astTypes.getType(ast, refNode);
if (type!=null) {
if (type != null) {
cancelToken.checkCanceled();
Handler handler = handlers.get(type);
if (handler!=null) {
int start = refNode.getStartMark().getIndex();
@@ -113,6 +125,8 @@ public class ConcourseDefinitionFinder extends SimpleDefinitionFinder {
}
}
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
log.error("", e);;
}

View File

@@ -17,6 +17,7 @@ import java.util.stream.Collectors;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -61,23 +62,32 @@ public class PropertiesJavaDefinitionHandler implements DefinitionHandler, Langu
private BootLanguageServerParams params;
@Override
public List<LocationLink> handle(DefinitionParams definitionParams) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams definitionParams) {
try {
TextDocument doc = documents.getLatestSnapshot(definitionParams);
TypeUtil typeUtil = params.typeUtilProvider.getTypeUtil(sourceLinks, doc);
cancelToken.checkCanceled();
FuzzyMap<PropertyInfo> index = params.indexProvider.getIndex(doc).getProperties();
int offset;
offset = doc.toOffset(definitionParams.getPosition());
return getDefinitions(index, typeUtil, doc, offset);
cancelToken.checkCanceled();
return getDefinitions(cancelToken, index, typeUtil, doc, offset);
} catch (BadLocationException e) {
return ImmutableList.of();
}
}
private List<LocationLink> getDefinitions(FuzzyMap<PropertyInfo> index, TypeUtil typeUtil, TextDocument doc, int offset) {
private List<LocationLink> getDefinitions(CancelChecker cancelToken, FuzzyMap<PropertyInfo> index, TypeUtil typeUtil, TextDocument doc, int offset) {
IJavaProject project = typeUtil.getJavaProject();
PropertyFinder propertyFinder = new PropertyFinder(index, typeUtil, doc, offset);
Node node = propertyFinder.findNode();
cancelToken.checkCanceled();
try {
Range selectionRange = doc.toRange(node.getOffset(), node.getLength());
if (node instanceof Key) {

View File

@@ -46,6 +46,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMDocument;
@@ -57,6 +58,7 @@ import org.eclipse.lemminx.dom.parser.XMLScanner;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.boot.java.links.JavaElementLocationProvider;
@@ -140,17 +142,22 @@ public class XmlBeansConfigDefinitionHandler implements DefinitionHandler, Langu
}
@Override
public List<LocationLink> handle(DefinitionParams params) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams params) {
try {
if (config.isSpringXMLSupportEnabled() && config.areXmlHyperlinksEnabled()) {
TextDocument doc = documents.getLatestSnapshot(params);
if (doc != null) {
cancelToken.checkCanceled();
String content = doc.get();
DOMParser parser = DOMParser.getInstance();
DOMDocument dom = parser.parse(content, "", null);
int offset = doc.toOffset(params.getPosition());
cancelToken.checkCanceled();
DOMNode node = dom.findNodeBefore(offset);
@@ -159,6 +166,7 @@ public class XmlBeansConfigDefinitionHandler implements DefinitionHandler, Langu
Scanner scanner = XMLScanner.createScanner(content, node.getStart(), false);
TokenType token = scanner.scan();
while (token != TokenType.EOS && scanner.getTokenOffset() <= offset) {
switch (token) {
case AttributeValue:
@@ -200,6 +208,8 @@ public class XmlBeansConfigDefinitionHandler implements DefinitionHandler, Langu
}
}
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
log.error("{}", e);
}

View File

@@ -12,12 +12,14 @@ package org.springframework.ide.vscode.boot.app;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -68,18 +70,26 @@ public class YamlPropertiesJavaDefinitionHandler implements DefinitionHandler, L
}
@Override
public List<LocationLink> handle(DefinitionParams definitionParams) {
public List<LocationLink> handle(CancelChecker cancelToken, DefinitionParams definitionParams) {
try {
TextDocument doc = documents.getLatestSnapshot(definitionParams);
int offset = doc.toOffset(definitionParams.getPosition());
cancelToken.checkCanceled();
YamlFileAST ast = getAst(doc);
if (ast != null) {
YamlDocument ymlDoc = new YamlDocument(doc, structureProvider);
YamlAssistContext assistContext = assistContextProvider.getGlobalAssistContext(ymlDoc);
if (assistContext != null) {
List<NodeRef<?>> astPath = ast.findPath(offset);
final YamlPath path = YamlPath.fromASTPath(astPath);
if (path != null) {
cancelToken.checkCanceled();
YamlPath assistPath = path;
if (assistPath.pointsAtKey()) {
// When a path points at a key we must tramsform it to a
@@ -119,6 +129,8 @@ public class YamlPropertiesJavaDefinitionHandler implements DefinitionHandler, L
}
}
}
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
log.error("", e);
}