下面列出了怎么用org.eclipse.lsp4j.Location的API类实例代码及写法,或者点击链接到github查看源代码。
public List<? extends Location> getDefinitions(XtextResource resource, int offset,
IReferenceFinder.IResourceAccess resourceAccess, CancelIndicator cancelIndicator) {
EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
if (element == null) {
return Collections.emptyList();
}
List<Location> locations = new ArrayList<>();
TargetURIs targetURIs = collectTargetURIs(element);
for (URI targetURI : targetURIs) {
operationCanceledManager.checkCanceled(cancelIndicator);
doRead(resourceAccess, targetURI, (EObject obj) -> {
Location location = documentExtensions.newLocation(obj);
if (location != null) {
locations.add(location);
}
});
}
return locations;
}
@Test
void testLocalVariableTypeDefinitionFromDeclaration() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class TypeDefinitions {\n");
contents.append(" public TypeDefinitions() {\n");
contents.append(" TypeDefinitions localVar\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(2, 22);
List<? extends Location> locations = services
.typeDefinition(new TextDocumentPositionParams(textDocument, position)).get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(0, location.getRange().getStart().getLine());
Assertions.assertEquals(0, location.getRange().getStart().getCharacter());
Assertions.assertEquals(4, location.getRange().getEnd().getLine());
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}
private boolean shouldIncludeDefinition(ITypeRoot typeRoot, IRegion region, IJavaElement elementToSearch, List<Location> implementations) {
boolean isUnimplemented = false;
try {
isUnimplemented = isUnimplementedMember(elementToSearch);
} catch (JavaModelException e) {
// do nothing.
}
if (isUnimplemented && implementations != null && !implementations.isEmpty()) {
return false;
}
CompilationUnit ast = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, new NullProgressMonitor());
if (ast == null) {
return false;
}
ASTNode node = NodeFinder.perform(ast, region.getOffset(), region.getLength());
if (node instanceof SimpleName && !(node.getParent() instanceof MethodDeclaration || node.getParent() instanceof SuperMethodInvocation || node.getParent() instanceof AbstractTypeDeclaration)) {
return true;
}
return false;
}
@Test
void testMemberVariableTypeDefinitionFromAssignment() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class TypeDefinitions {\n");
contents.append(" TypeDefinitions memberVar\n");
contents.append(" public TypeDefinitions() {\n");
contents.append(" memberVar = null\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(3, 6);
List<? extends Location> locations = services
.typeDefinition(new TextDocumentPositionParams(textDocument, position)).get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(0, location.getRange().getStart().getLine());
Assertions.assertEquals(0, location.getRange().getStart().getCharacter());
Assertions.assertEquals(5, location.getRange().getEnd().getLine());
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}
@Test
void testLocalVariableDefinitionFromMethodCallObjectExpression() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public Definitions() {\n");
contents.append(" String localVar = \"hi\"\n");
contents.append(" localVar.charAt(0)\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(3, 6);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(2, location.getRange().getStart().getLine());
Assertions.assertEquals(11, location.getRange().getStart().getCharacter());
Assertions.assertEquals(2, location.getRange().getEnd().getLine());
Assertions.assertEquals(19, location.getRange().getEnd().getCharacter());
}
@Test
void testMemberVariableDefinitionFromDeclaration() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public int memberVar\n");
contents.append("}\n");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(1, 18);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(1, location.getRange().getStart().getLine());
Assertions.assertEquals(2, location.getRange().getStart().getCharacter());
Assertions.assertEquals(1, location.getRange().getEnd().getLine());
Assertions.assertEquals(22, location.getRange().getEnd().getCharacter());
}
@Test
void testMemberVariableDefinitionFromAssignment() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public int memberVar\n");
contents.append(" public Definitions() {\n");
contents.append(" memberVar = 123\n");
contents.append(" }\n");
contents.append("}\n");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(3, 6);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(1, location.getRange().getStart().getLine());
Assertions.assertEquals(2, location.getRange().getStart().getCharacter());
Assertions.assertEquals(1, location.getRange().getEnd().getLine());
Assertions.assertEquals(22, location.getRange().getEnd().getCharacter());
}
@Test
public void testMethodImplementation_includeDefinition() {
URI uri = project.getFile("src/org/sample/FooService.java").getRawLocationURI();
String fileURI = ResourceUtils.fixURI(uri);
TextDocumentPositionParams param = new TextDocumentPositionParams();
param.setPosition(new Position(11, 13)); //Position over someMethod()
param.setTextDocument(new TextDocumentIdentifier(fileURI));
List<? extends Location> implementations = handler.findImplementations(param, monitor);
assertNotNull("findImplementations should not return null", implementations);
assertEquals(implementations.toString(), 2, implementations.size());
Location foo = implementations.get(0);
assertTrue("Unexpected implementation : " + foo.getUri(), foo.getUri().contains("org/sample/Foo.java"));
//check range points to someMethod() position
assertEquals(new Position(8, 13), foo.getRange().getStart());
assertEquals(new Position(8, 23), foo.getRange().getEnd());
foo = implementations.get(1);
assertTrue("Unexpected implementation : " + foo.getUri(), foo.getUri().contains("org/sample/FooChild.java"));
//check range points to someMethod() position
assertEquals(new Position(4, 13), foo.getRange().getStart());
assertEquals(new Position(4, 23), foo.getRange().getEnd());
}
@Test
void testClassDefinitionFromDeclaration() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(0, 8);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(0, location.getRange().getStart().getLine());
Assertions.assertEquals(0, location.getRange().getStart().getCharacter());
Assertions.assertEquals(1, location.getRange().getEnd().getLine());
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}
@Test
void testConstructorDefinitionFromConstructorCall() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public Definitions() {\n");
contents.append(" new Definitions()\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(2, 10);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(1, location.getRange().getStart().getLine());
Assertions.assertEquals(2, location.getRange().getStart().getCharacter());
Assertions.assertEquals(3, location.getRange().getEnd().getLine());
Assertions.assertEquals(3, location.getRange().getEnd().getCharacter());
}
@Test
void testClassDefinitionFromClassExpression() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public static void staticMethod() {}\n");
contents.append(" public Definitions() {\n");
contents.append(" Definitions.staticMethod()\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(3, 6);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(0, location.getRange().getStart().getLine());
Assertions.assertEquals(0, location.getRange().getStart().getCharacter());
Assertions.assertEquals(5, location.getRange().getEnd().getLine());
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}
@Test
void testParameterDefinitionFromDeclarationInMethod() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class Definitions {\n");
contents.append(" public void memberMethod(int param) {\n");
contents.append(" }\n");
contents.append("}\n");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(1, 33);
List<? extends Location> locations = services.definition(new TextDocumentPositionParams(textDocument, position))
.get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(1, location.getRange().getStart().getLine());
Assertions.assertEquals(27, location.getRange().getStart().getCharacter());
Assertions.assertEquals(1, location.getRange().getEnd().getLine());
Assertions.assertEquals(36, location.getRange().getEnd().getCharacter());
}
@Test
public void testUsingVertxTest() throws Exception {
Module javaProject = createMavenModule("using-vertx", new File("projects/maven/using-vertx"));
// Test with JAR
// quarkus.datasource.url
Location location = PropertiesManager.getInstance().findPropertyLocation(javaProject,
"io.quarkus.reactive.pg.client.runtime.DataSourceConfig", "url", null, PsiUtilsImpl.getInstance());
Assert.assertNotNull("Definition from JAR", location);
// Test with deployment JAR
// quarkus.arc.auto-inject-fields
location = PropertiesManager.getInstance().findPropertyLocation(javaProject,
"io.quarkus.arc.deployment.ArcConfig", "autoInjectFields", null, PsiUtilsImpl.getInstance());
Assert.assertNotNull("Definition deployment from JAR", location);
// Test with Java sources
// myapp.schema.create
location = PropertiesManager.getInstance().findPropertyLocation(javaProject, "org.acme.vertx.FruitResource",
"schemaCreate", null, PsiUtilsImpl.getInstance());
Assert.assertNotNull("Definition from Java Sources", location);
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
DefinitionParams params) {
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
if (definitionLinkSupport) {
return Either.forRight(
getXMLLanguageService().findDefinition(xmlDocument, params.getPosition(), cancelChecker));
}
List<? extends Location> locations = getXMLLanguageService()
.findDefinition(xmlDocument, params.getPosition(), cancelChecker) //
.stream() //
.map(locationLink -> XMLPositionUtility.toLocation(locationLink)) //
.collect(Collectors.toList());
return Either.forLeft(locations);
});
}
@Test
public void testMethodImplementation() {
URI uri = project.getFile("src/org/sample/IFoo.java").getRawLocationURI();
String fileURI = ResourceUtils.fixURI(uri);
TextDocumentPositionParams param = new TextDocumentPositionParams();
param.setPosition(new Position(4, 14)); //Position over IFoo#someMethod
param.setTextDocument(new TextDocumentIdentifier(fileURI));
List<? extends Location> implementations = handler.findImplementations(param, monitor);
assertNotNull("findImplementations should not return null", implementations);
assertEquals(implementations.toString(), 1, implementations.size());
Location foo2 = implementations.get(0);
assertTrue("Unexpected implementation : " + foo2.getUri(), foo2.getUri().contains("org/sample/Foo2.java"));
//check range points to someMethod() position
assertEquals(new Position(4, 16), foo2.getRange().getStart());
assertEquals(new Position(4, 26), foo2.getRange().getEnd());
}
/***/
@Test
public void testDefinition_04() throws Exception {
testWorkspaceManager.createTestProjectOnDisk(Collections.emptyMap());
startAndWaitForLspServer();
TextDocumentPositionParams textDocumentPositionParams = new TextDocumentPositionParams();
textDocumentPositionParams.setTextDocument(new TextDocumentIdentifier("n4scheme:/builtin_js.n4ts"));
// see position from test above
textDocumentPositionParams.setPosition(new Position(838, 15));
CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definitionsFuture = languageServer
.definition(textDocumentPositionParams);
Either<List<? extends Location>, List<? extends LocationLink>> definitions = definitionsFuture.get();
File root = getRoot();
String actualSignatureHelp = new StringLSP4J(root).toString4(definitions);
assertEquals("(n4scheme:/builtin_js.n4ts, [838:15 - 838:21])", actualSignatureHelp.trim());
}
private void findSymbolInformations(DOMNode node, String container, List<SymbolInformation> symbols,
boolean ignoreNode, AtomicLong limit, CancelChecker cancelChecker) throws BadLocationException {
if (!isNodeSymbol(node)) {
return;
}
String name = "";
if (!ignoreNode) {
name = nodeToName(node);
DOMDocument xmlDocument = node.getOwnerDocument();
Range range = getSymbolRange(node);
Location location = new Location(xmlDocument.getDocumentURI(), range);
SymbolInformation symbol = new SymbolInformation(name, getSymbolKind(node), location, container);
checkLimit(limit);
symbols.add(symbol);
}
final String containerName = name;
node.getChildren().forEach(child -> {
try {
findSymbolInformations(child, containerName, symbols, false, limit, cancelChecker);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "XMLSymbolsProvider was given a BadLocation by the provided 'node' variable",
e);
}
});
}
private static Location fixLocation(IJavaElement element, Location location, IJavaProject javaProject) {
if (location == null) {
return null;
}
if (!javaProject.equals(element.getJavaProject()) && element.getJavaProject().getProject().getName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) {
// see issue at: https://github.com/eclipse/eclipse.jdt.ls/issues/842 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573
// for jdk classes, jdt will reuse the java model by altering project to share the model between projects
// so that sometimes the project for `element` is default project and the project is different from the project for `unit`
// this fix is to replace the project name with non-default ones since default project should be transparent to users.
if (location.getUri().contains(ProjectsManager.DEFAULT_PROJECT_NAME)) {
String patched = StringUtils.replaceOnce(location.getUri(), ProjectsManager.DEFAULT_PROJECT_NAME, javaProject.getProject().getName());
try {
IClassFile cf = (IClassFile) JavaCore.create(JDTUtils.toURI(patched).getQuery());
if (cf != null && cf.exists()) {
location.setUri(patched);
}
} catch (Exception ex) {
}
}
}
return location;
}
private List<? extends Location> actionScriptTypeDefinition(IASNode offsetNode, WorkspaceFolderData folderData)
{
if (offsetNode == null)
{
//we couldn't find a node at the specified location
return Collections.emptyList();
}
IDefinition definition = null;
if (offsetNode instanceof IIdentifierNode)
{
IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
definition = DefinitionUtils.resolveTypeWithExtras(identifierNode, folderData.project);
}
if (definition == null)
{
//VSCode may call typeDefinition() when there isn't necessarily a
//type definition referenced at the current position.
return Collections.emptyList();
}
List<Location> result = new ArrayList<>();
workspaceFolderManager.resolveDefinition(definition, folderData, result);
return result;
}
private List<? extends Location> mxmlTypeDefinition(IMXMLTagData offsetTag, int currentOffset, WorkspaceFolderData folderData)
{
IDefinition definition = MXMLDataUtils.getTypeDefinitionForMXMLNameAtOffset(offsetTag, currentOffset, folderData.project);
if (definition == null)
{
//VSCode may call definition() when there isn't necessarily a
//definition referenced at the current position.
return Collections.emptyList();
}
if (MXMLDataUtils.isInsideTagPrefix(offsetTag, currentOffset))
{
//ignore the tag's prefix
return Collections.emptyList();
}
List<Location> result = new ArrayList<>();
workspaceFolderManager.resolveDefinition(definition, folderData, result);
return result;
}
/**
* Finds where the definition referenced at the current position in a text
* document is defined.
*/
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(DefinitionParams params)
{
return CompletableFutures.computeAsync(compilerWorkspace.getExecutorService(), cancelToken ->
{
cancelToken.checkCanceled();
//make sure that the latest changes have been passed to
//workspace.fileChanged() before proceeding
if(realTimeProblemsChecker != null)
{
realTimeProblemsChecker.updateNow();
}
compilerWorkspace.startBuilding();
try
{
DefinitionProvider provider = new DefinitionProvider(workspaceFolderManager, fileTracker);
return provider.definition(params, cancelToken);
}
finally
{
compilerWorkspace.doneBuilding();
}
});
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
TextDocumentPositionParams params) {
URI uri = URI.create(params.getTextDocument().getUri());
recompileIfContextChanged(uri);
DefinitionProvider provider = new DefinitionProvider(astVisitor);
return provider.provideDefinition(params.getTextDocument(), params.getPosition());
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> typeDefinition(
TextDocumentPositionParams params) {
URI uri = URI.create(params.getTextDocument().getUri());
recompileIfContextChanged(uri);
TypeDefinitionProvider provider = new TypeDefinitionProvider(astVisitor);
return provider.provideTypeDefinition(params.getTextDocument(), params.getPosition());
}
@Override
public Location convert(IJavaElement element, int offset, int position) {
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
Location location = null;
try {
if (compilationUnit != null) {
if (useDefaultPosition || offset > 0 && position > 0) {
//builds location from offset and position directly
location = toLocation(compilationUnit, offset, position);
} else {
// opens file to determine location
location = toLocation(element);
}
} else if (includeClassFiles) {
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (cf != null) {
if (useDefaultPosition || offset > 0 && position > 0) {
//builds location from offset and position directly
location = toLocation(cf, offset, position);
} else {
//opens source to determine location
location = toLocation(element);
if (location == null) {//If no source was attached, return default location
location = toLocation(cf, 0, 0);
}
}
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Failed to convert " + element, e);
}
return location;
}
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
URI uri = getURI(params);
return openFilesManager.runInOpenFileContext(uri, "references", (ofc, ci) -> {
return references(ofc, params, ci);
});
}
@Test
void testLocalVariableTypeDefinitionFromMethodCall() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
StringBuilder contents = new StringBuilder();
contents.append("class TypeDefinitions {\n");
contents.append(" public void method() {\n");
contents.append(" }\n");
contents.append(" public TypeDefinitions() {\n");
contents.append(" TypeDefinitions localVar\n");
contents.append(" localVar.method()\n");
contents.append(" }\n");
contents.append("}");
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(5, 6);
List<? extends Location> locations = services
.typeDefinition(new TextDocumentPositionParams(textDocument, position)).get().getLeft();
Assertions.assertEquals(1, locations.size());
Location location = locations.get(0);
Assertions.assertEquals(uri, location.getUri());
Assertions.assertEquals(0, location.getRange().getStart().getLine());
Assertions.assertEquals(0, location.getRange().getStart().getCharacter());
Assertions.assertEquals(7, location.getRange().getEnd().getLine());
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
TextDocumentPositionParams params) {
TextDocumentIdentifier textDocument = params.getTextDocument();
LOGGER.debug("definition: {}", textDocument);
return CompletableFuture.completedFuture(null);
}
@Test
void testRetrieveASingleDirectReferenceFor_from() throws Exception {
Location res = testRetrieveReferences(SINGLE_REFERENCE, 1, new Position(8, 18)).get(0);
Range range = res.getRange();
assertThat(range.getStart().getLine()).isEqualTo(5);
assertThat(range.getEnd().getLine()).isEqualTo(5);
}
public List<? extends Location> getReferences(Document document, XtextResource resource, ReferenceParams params,
IReferenceFinder.IResourceAccess resourceAccess, IResourceDescriptions indexData,
CancelIndicator cancelIndicator) {
int offset = document.getOffSet(params.getPosition());
List<? extends Location> definitions = Collections.emptyList();
if (params.getContext().isIncludeDeclaration()) {
definitions = getDefinitions(resource, offset, resourceAccess, cancelIndicator);
}
List<? extends Location> references = getReferences(resource, offset, resourceAccess, indexData,
cancelIndicator);
List<Location> result = new ArrayList<>();
result.addAll(definitions);
result.addAll(references);
return result;
}
@Test
public void testClassImplementation() {
URI uri = project.getFile("src/org/sample/Foo2.java").getRawLocationURI();
String fileURI = ResourceUtils.fixURI(uri);
TextDocumentPositionParams param = new TextDocumentPositionParams();
param.setPosition(new Position(2, 14)); //Position over Foo2
param.setTextDocument(new TextDocumentIdentifier(fileURI));
List<? extends Location> implementations = handler.findImplementations(param, monitor);
assertNotNull("findImplementations should not return null", implementations);
assertEquals(implementations.toString(), 1, implementations.size());
Location foo3 = implementations.get(0);
assertTrue("Unexpected implementation : " + foo3.getUri(), foo3.getUri().contains("org/sample/Foo3.java"));
assertEquals(JDTUtils.newLineRange(5, 13, 17), foo3.getRange());
}