Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import java.util.concurrent.CompletableFuture;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ConstructorNode;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

Expand Down Expand Up @@ -56,7 +59,7 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
}

ASTNode definitionNode = GroovyASTUtils.getDefinition(offsetNode, true, ast);
if (definitionNode == null || definitionNode.getLineNumber() == -1 || definitionNode.getColumnNumber() == -1) {
if (definitionNode == null) {
return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
}

Expand All @@ -67,6 +70,15 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio

Location location = GroovyLanguageServerUtils.astNodeToLocation(definitionNode, definitionURI);
if (location == null) {
if (definitionNode instanceof ConstructorNode) {
// This will "fall-through" to the if-block below!
definitionNode = ((ConstructorNode) definitionNode).getDeclaringClass();
definitionURI = ast.getURI(definitionNode);
}
if (definitionNode instanceof ClassNode) {
location = new Location(definitionURI.toString(), new Range(new Position(), new Position(1, 1)));
return CompletableFuture.completedFuture(Either.forLeft(Collections.singletonList(location)));
}
return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public CompletableFuture<List<? extends Location>> provideReferences(TextDocumen
List<ASTNode> references = GroovyASTUtils.getReferences(offsetNode, ast);
List<Location> locations = references.stream().map(node -> {
URI uri = ast.getURI(node);
if (uri == null)
return null;
return GroovyLanguageServerUtils.astNodeToLocation(node, uri);
}).filter(location -> location != null).collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,50 @@ void testClassDefinitionFromImport() throws Exception {
Assertions.assertEquals(1, location.getRange().getEnd().getCharacter());
}

@Test
void testImplicitClassDefinitionFromConstructorCall() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, "new Definitions()");
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
Position position = new Position(0, 10);
List<? extends Location> locations = services.definition(new DefinitionParams(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 testImplicitClassDefinitionFromImport() throws Exception {
Path filePath = srcRoot.resolve("Definitions.groovy");
String uri = filePath.toUri().toString();
TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, "");
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));

Path filePath2 = srcRoot.resolve("Definitions2.groovy");
String uri2 = filePath2.toUri().toString();
TextDocumentItem textDocumentItem2 = new TextDocumentItem(uri2, LANGUAGE_GROOVY, 1, "import Definitions");
services.didOpen(new DidOpenTextDocumentParams(textDocumentItem2));

TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri2);
Position position = new Position(0, 10);
List<? extends Location> locations = services.definition(new DefinitionParams(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());
}

// --- parameters

@Test
Expand Down