Skip to content

Commit d8a163b

Browse files
committed
Add declaration finding capabilities to LS
1 parent 99dc03d commit d8a163b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

java/src/processing/mode/java/lsp/PdeLanguageServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
7777
completionOptions.setTriggerCharacters(List.of("."));
7878
capabilities.setCompletionProvider(completionOptions);
7979
capabilities.setDocumentFormattingProvider(true);
80+
capabilities.setDeclarationProvider(true);
8081
var result = new InitializeResult(capabilities);
8182
return CompletableFuture.completedFuture(result);
8283
}

java/src/processing/mode/java/lsp/PdeTextDocumentService.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@
88
import org.eclipse.lsp4j.CompletionItem;
99
import org.eclipse.lsp4j.CompletionList;
1010
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
11+
import org.eclipse.lsp4j.DeclarationParams;
1112
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
1213
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
1314
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
1415
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
1516
import org.eclipse.lsp4j.DocumentFormattingParams;
1617
import org.eclipse.lsp4j.TextEdit;
18+
import org.eclipse.lsp4j.Location;
19+
import org.eclipse.lsp4j.LocationLink;
1720

1821
import java.util.Collections;
1922
import java.net.URI;
23+
import java.util.Optional;
24+
25+
import processing.mode.java.PreprocSketch;
26+
27+
import static org.eclipse.lsp4j.jsonrpc.CompletableFutures.computeAsync;
28+
import static org.eclipse.lsp4j.jsonrpc.messages.Either.forLeft;
29+
2030

2131
class PdeTextDocumentService implements TextDocumentService {
2232
PdeLanguageServer pls;
@@ -82,4 +92,49 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting
8292
})
8393
.orElse(CompletableFuture.completedFuture(Collections.emptyList()));
8494
}
95+
96+
97+
@Override
98+
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> declaration(DeclarationParams params) {
99+
System.out.println("searching for declaration");
100+
101+
java.net.URI uri = java.net.URI.create(params.getTextDocument().getUri());
102+
int lineNumber = params.getPosition().getLine();
103+
int colNumber = params.getPosition().getCharacter();
104+
105+
Optional<PdeAdapter> adapterOptional =
106+
pls.getAdapter(uri);
107+
108+
if(adapterOptional.isEmpty()){
109+
System.out.println("pde adapter not found");
110+
return CompletableFutures.computeAsync(_x -> Either
111+
.forLeft(Collections.emptyList()));
112+
}
113+
114+
PdeAdapter adapter = adapterOptional.get();
115+
PreprocSketch preprocSketch = adapter.ps;
116+
Optional<Integer> optionalJavaOffset = adapter.findJavaOffset(uri,
117+
lineNumber, colNumber);
118+
119+
if(optionalJavaOffset.isEmpty()){
120+
System.out.println("javaOffset not found");
121+
return CompletableFutures.computeAsync(_x -> Either
122+
.forLeft(Collections.emptyList()));
123+
}
124+
int javaOffset = optionalJavaOffset.get();
125+
126+
List<? extends Location> locations;
127+
locations = PdeSymbolFinder.searchDeclaration(preprocSketch, javaOffset);
128+
129+
Optional<CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>>>
130+
OptCompFutEit = Optional.ofNullable(CompletableFutures
131+
.computeAsync(_x -> locations))
132+
.map(_x -> _x.thenApply(Either::forLeft)
133+
);
134+
135+
return OptCompFutEit.orElse(
136+
computeAsync(_x -> forLeft(Collections.emptyList()))
137+
);
138+
}
139+
85140
}

0 commit comments

Comments
 (0)