Skip to content

Commit f5a1132

Browse files
gselzerclaude
andcommitted
code-lsp: a completion front for Python language servers
PythonCompletionServer wraps a Python language server (e.g. jedi-language-server) as editors want it: it asks at a word's first character and caches the answer, cancels superseded requests, filters by the word typed, and gives the top completions their signatures. DelegatingLanguageServer forwards everything, for such decorators. Cancelling a completion now cancels the request it waits on, through TransformingLanguageServer and LaunchedLanguageServer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 31c39e1 commit f5a1132

6 files changed

Lines changed: 697 additions & 5 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*-
2+
* #%L
3+
* Language Server Protocol bridge for SciJava code intelligence.
4+
* %%
5+
* Copyright (C) 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.code.lsp;
31+
32+
import java.util.List;
33+
import java.util.concurrent.CompletableFuture;
34+
35+
import org.eclipse.lsp4j.CompletionItem;
36+
import org.eclipse.lsp4j.CompletionList;
37+
import org.eclipse.lsp4j.CompletionParams;
38+
import org.eclipse.lsp4j.DidChangeConfigurationParams;
39+
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
40+
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
41+
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
42+
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
43+
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
44+
import org.eclipse.lsp4j.Hover;
45+
import org.eclipse.lsp4j.HoverParams;
46+
import org.eclipse.lsp4j.InitializeParams;
47+
import org.eclipse.lsp4j.InitializeResult;
48+
import org.eclipse.lsp4j.InitializedParams;
49+
import org.eclipse.lsp4j.SignatureHelp;
50+
import org.eclipse.lsp4j.SignatureHelpParams;
51+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
52+
import org.eclipse.lsp4j.services.LanguageClient;
53+
import org.eclipse.lsp4j.services.LanguageClientAware;
54+
import org.eclipse.lsp4j.services.LanguageServer;
55+
import org.eclipse.lsp4j.services.TextDocumentService;
56+
import org.eclipse.lsp4j.services.WorkspaceService;
57+
58+
/**
59+
* A language server passing everything on to another: a base for decorators
60+
* changing some of its answers.
61+
*
62+
* @author Gabriel Selzer
63+
*/
64+
public class DelegatingLanguageServer implements LanguageServer,
65+
TextDocumentService, WorkspaceService, LanguageClientAware
66+
{
67+
68+
protected final LanguageServer inner;
69+
70+
public DelegatingLanguageServer(final LanguageServer inner) {
71+
this.inner = inner;
72+
}
73+
74+
@Override
75+
public void connect(final LanguageClient client) {
76+
if (inner instanceof LanguageClientAware) {
77+
((LanguageClientAware) inner).connect(client);
78+
}
79+
}
80+
81+
@Override
82+
public CompletableFuture<InitializeResult> initialize(
83+
final InitializeParams params)
84+
{
85+
return inner.initialize(params);
86+
}
87+
88+
@Override
89+
public void initialized(final InitializedParams params) {
90+
inner.initialized(params);
91+
}
92+
93+
@Override
94+
public CompletableFuture<Object> shutdown() {
95+
return inner.shutdown();
96+
}
97+
98+
@Override
99+
public void exit() {
100+
inner.exit();
101+
}
102+
103+
@Override
104+
public TextDocumentService getTextDocumentService() {
105+
return this;
106+
}
107+
108+
@Override
109+
public WorkspaceService getWorkspaceService() {
110+
return this;
111+
}
112+
113+
@Override
114+
public void didOpen(final DidOpenTextDocumentParams params) {
115+
inner.getTextDocumentService().didOpen(params);
116+
}
117+
118+
@Override
119+
public void didChange(final DidChangeTextDocumentParams params) {
120+
inner.getTextDocumentService().didChange(params);
121+
}
122+
123+
@Override
124+
public void didClose(final DidCloseTextDocumentParams params) {
125+
inner.getTextDocumentService().didClose(params);
126+
}
127+
128+
@Override
129+
public void didSave(final DidSaveTextDocumentParams params) {
130+
inner.getTextDocumentService().didSave(params);
131+
}
132+
133+
@Override
134+
public CompletableFuture<Either<List<CompletionItem>, CompletionList>>
135+
completion(final CompletionParams params)
136+
{
137+
return inner.getTextDocumentService().completion(params);
138+
}
139+
140+
@Override
141+
public CompletableFuture<CompletionItem> resolveCompletionItem(
142+
final CompletionItem item)
143+
{
144+
return inner.getTextDocumentService().resolveCompletionItem(item);
145+
}
146+
147+
@Override
148+
public CompletableFuture<SignatureHelp> signatureHelp(
149+
final SignatureHelpParams params)
150+
{
151+
return inner.getTextDocumentService().signatureHelp(params);
152+
}
153+
154+
@Override
155+
public CompletableFuture<Hover> hover(final HoverParams params) {
156+
return inner.getTextDocumentService().hover(params);
157+
}
158+
159+
@Override
160+
public void didChangeConfiguration(final DidChangeConfigurationParams p) {
161+
inner.getWorkspaceService().didChangeConfiguration(p);
162+
}
163+
164+
@Override
165+
public void didChangeWatchedFiles(final DidChangeWatchedFilesParams p) {
166+
inner.getWorkspaceService().didChangeWatchedFiles(p);
167+
}
168+
}

‎code-lsp/src/main/java/org/scijava/code/lsp/LaunchedLanguageServer.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,24 @@ private <T> CompletableFuture<T> request(final String uri,
221221
final Doc doc = docs.get(uri);
222222
if (doc == null) return CompletableFuture.completedFuture(null);
223223
final String text = doc.text;
224-
return servers.server(environment).thenCompose(s -> s.request(uri, text,
225-
request));
224+
// NB: Cancelling the answer cancels the request, which tells the process
225+
// ($/cancelRequest).
226+
final CompletableFuture<T> answer = new CompletableFuture<>();
227+
servers.server(environment).thenAccept(s -> {
228+
if (answer.isDone()) return;
229+
final CompletableFuture<T> raw = s.request(uri, text, request);
230+
answer.whenComplete((r, error) -> {
231+
if (answer.isCancelled()) raw.cancel(true);
232+
});
233+
raw.whenComplete((r, error) -> {
234+
if (error != null) answer.completeExceptionally(error);
235+
else answer.complete(r);
236+
});
237+
}).exceptionally(error -> {
238+
answer.completeExceptionally(error);
239+
return null;
240+
});
241+
return answer;
226242
}
227243

228244
/** Notes a document's new text; asks for its problems after a pause. */

0 commit comments

Comments
 (0)