Skip to content

Commit 047532e

Browse files
gselzerclaude
andcommitted
Offer hover and diagnostics from jedi-language-server
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ca96614 commit 047532e

4 files changed

Lines changed: 132 additions & 5 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/appose/python/ApposePythonCodeCompleter.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import org.scijava.code.api.Completion;
6161
import org.scijava.code.api.CompletionRequest;
6262
import org.scijava.code.api.CompletionResult;
63+
import org.scijava.code.api.Diagnostic;
64+
import org.scijava.code.api.Hover;
6365
import org.scijava.code.api.EnvironmentContext;
6466
import org.scijava.code.api.ScriptDialect;
6567
import org.scijava.code.api.ScriptDocument;
@@ -116,6 +118,9 @@ public class ApposePythonCodeCompleter extends AbstractCodeCompleterPlugin {
116118
private static final long BUDGET = Long.getLong(
117119
"scijava.appose.completion.budget", 25);
118120

121+
/** How long to wait for the server to find problems, in milliseconds. */
122+
private static final long DIAGNOSE_TIMEOUT = 30_000;
123+
119124
/** How many completions get signatures, once a word is begun. */
120125
private static final int SIGNATURES = 30;
121126

@@ -228,6 +233,43 @@ public ScriptDialect dialect() {
228233
return DIALECT;
229234
}
230235

236+
@Override
237+
public Hover hover(final CompletionRequest request) {
238+
final String text = request.text();
239+
final int at = request.offset();
240+
if (PythonSyntax.analyze(text, at).kind == Kind.NONE) {
241+
return Hover.NONE; // NB: In a comment or string.
242+
}
243+
final LspClient client = lspClient();
244+
if (client == null) return Hover.NONE;
245+
final CompletableFuture<Hover> hover = client.hover(document(request,
246+
true, false), uri(request), at).handle((h, error) -> h);
247+
try {
248+
final Hover h = hover.get(BUDGET, TimeUnit.MILLISECONDS);
249+
return h == null ? Hover.NONE : h;
250+
}
251+
catch (final TimeoutException exc) {
252+
return Hover.NONE.withUpdate(hover);
253+
}
254+
catch (final InterruptedException exc) {
255+
Thread.currentThread().interrupt();
256+
}
257+
catch (final ExecutionException | CancellationException exc) {
258+
// NB: The server failed.
259+
}
260+
return Hover.NONE;
261+
}
262+
263+
@Override
264+
public CompletableFuture<List<Diagnostic>> diagnose(
265+
final CompletionRequest request)
266+
{
267+
final LspClient client = lspClient();
268+
if (client == null) return super.diagnose(request);
269+
return client.diagnose(document(request, true, false), uri(request),
270+
DIAGNOSE_TIMEOUT);
271+
}
272+
231273
@Override
232274
public SignatureHelp signatureHelp(final CompletionRequest request) {
233275
final String text = request.text();

‎src/main/java/org/scijava/plugins/scripting/appose/python/completion/JediLanguageServerSpec.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ public Object initializationOptions(final EnvironmentContext env) {
104104
workspaceOptions.put("extraPaths", env.searchPaths());
105105
final Map<String, Object> options = new LinkedHashMap<>();
106106
options.put("workspace", workspaceOptions);
107-
options.put("diagnostics", map("enable", false));
107+
// NB: Jedi's diagnostics are syntax errors, found as the script changes.
108+
final Map<String, Object> diagnostics = map("enable", true);
109+
diagnostics.put("didOpen", true);
110+
diagnostics.put("didChange", true);
111+
diagnostics.put("didSave", false);
112+
options.put("diagnostics", diagnostics);
108113
return options;
109114
}
110115

‎src/test/java/org/scijava/plugins/scripting/appose/python/ApposePythonCodeCompleterTest.java‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ public void testPrepareAndClose() throws Exception {
268268
fake.closed);
269269
}
270270

271+
@Test
272+
public void testHoverAndDiagnostics() throws Exception {
273+
final FakeLsp fake = fake(q -> Collections.emptyList());
274+
final String code = "#@ double sigma\nimport json\njson.dumps(sigma)";
275+
org.scijava.code.api.Hover hover = completer.hover(new CompletionRequest(
276+
code, code.indexOf("dumps") + 1, language, null, null, null));
277+
if (hover.update() != null) hover = hover.update().get(5,
278+
TimeUnit.SECONDS);
279+
assertEquals("<pre>dumps</pre>", hover.text());
280+
assertEquals(code.indexOf("dumps") + 1, last(fake).offset);
281+
// Not in comments or strings.
282+
assertTrue(completer.hover(new CompletionRequest("# json.dumps", 4,
283+
language, null, null, null)).isEmpty());
284+
285+
final List<org.scijava.code.api.Diagnostic> problems = completer.diagnose(
286+
new CompletionRequest(code, language, null)).get(5, TimeUnit.SECONDS);
287+
assertEquals(1, problems.size());
288+
assertTrue(last(fake).doc.text().endsWith(code));
289+
}
290+
271291
@Test
272292
public void testServerQuery() {
273293
final FakeLsp fake = fake(q -> Collections.emptyList());
@@ -291,13 +311,15 @@ public void testServerOptions() {
291311
final JediLanguageServerSpec spec = new JediLanguageServerSpec(() -> null,
292312
new java.io.File("workspace"), new java.io.File("server.log"));
293313
assertEquals("python", spec.languageId());
314+
final String diagnostics = "diagnostics={enable=true, didOpen=true, " +
315+
"didChange=true, didSave=false}";
294316
assertEquals("{workspace={environmentPath=/env/bin/python, " +
295-
"extraPaths=[/overlay]}, diagnostics={enable=false}}", String.valueOf(
296-
spec.initializationOptions(new EnvironmentContext("/env/bin/python",
317+
"extraPaths=[/overlay]}, " + diagnostics + "}", String.valueOf(spec
318+
.initializationOptions(new EnvironmentContext("/env/bin/python",
297319
Collections.singletonList("/overlay"), null))));
298320
// Without a script environment: the server's own.
299-
assertEquals("{workspace={extraPaths=[]}, diagnostics={enable=false}}",
300-
String.valueOf(spec.initializationOptions(EnvironmentContext.EMPTY)));
321+
assertEquals("{workspace={extraPaths=[]}, " + diagnostics + "}", String
322+
.valueOf(spec.initializationOptions(EnvironmentContext.EMPTY)));
301323
assertTrue(spec.rootUri(EnvironmentContext.EMPTY).startsWith("file:"));
302324
new java.io.File("workspace").delete();
303325
}
@@ -552,6 +574,25 @@ public CompletableFuture<SignatureHelp> signatureHelp(
552574
active, callStart));
553575
}
554576

577+
@Override
578+
public CompletableFuture<org.scijava.code.api.Hover> hover(
579+
final ScriptDocument doc, final String uri, final int offset)
580+
{
581+
asked.add(new Asked(doc, uri, offset, 0));
582+
return CompletableFuture.completedFuture(new org.scijava.code.api.Hover(
583+
"<pre>dumps</pre>", offset, offset));
584+
}
585+
586+
@Override
587+
public CompletableFuture<List<org.scijava.code.api.Diagnostic>> diagnose(
588+
final ScriptDocument doc, final String uri, final long timeout)
589+
{
590+
asked.add(new Asked(doc, uri, 0, 0));
591+
return CompletableFuture.completedFuture(Collections.singletonList(
592+
new org.scijava.code.api.Diagnostic(0, 1,
593+
org.scijava.code.api.Diagnostic.Severity.ERROR, "Oops", "fake")));
594+
}
595+
555596
@Override
556597
public void prepare(final ScriptDocument doc, final String uri) {
557598
asked.add(new Asked(doc, uri, doc.original().length(), 0));

‎src/test/java/org/scijava/plugins/scripting/appose/python/ApposePythonCompletionIntegrationTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.scijava.code.api.Completion;
5151
import org.scijava.code.api.CompletionRequest;
5252
import org.scijava.code.api.CompletionResult;
53+
import org.scijava.code.api.Diagnostic;
54+
import org.scijava.code.api.Hover;
5355
import org.scijava.code.api.SignatureHelp;
5456
import org.scijava.module.ModuleService;
5557
import org.scijava.script.ScriptInfo;
@@ -151,6 +153,43 @@ public void testSignatureHelp() throws Exception {
151153
assertEquals(code.indexOf('('), help.callStart());
152154
}
153155

156+
@Test
157+
public void testHover() throws Exception {
158+
final String code = "import json\njson.dumps(1)";
159+
final CompletionRequest request = new CompletionRequest(code, code
160+
.indexOf("dumps") + 2, language, null, null, null);
161+
final long deadline = System.currentTimeMillis() + BUILD_TIMEOUT;
162+
Hover hover = Hover.NONE;
163+
while (hover.isEmpty() && System.currentTimeMillis() < deadline) {
164+
hover = completer().hover(request);
165+
if (hover.update() != null) {
166+
final Hover better = hover.update().get(1, TimeUnit.MINUTES);
167+
if (better != null) hover = better;
168+
}
169+
}
170+
assertTrue(hover.text(), hover.text().contains("JSON"));
171+
assertEquals(code.indexOf("dumps"), hover.start());
172+
assertEquals(code.indexOf("dumps") + "dumps".length(), hover.end());
173+
}
174+
175+
@Test
176+
public void testDiagnostics() throws Exception {
177+
// A syntax error, on the script's third line.
178+
final String code = "#@ double sigma\nx = sigma\ny = (1 +\n";
179+
final long deadline = System.currentTimeMillis() + BUILD_TIMEOUT;
180+
List<Diagnostic> problems = Collections.emptyList();
181+
while (problems.isEmpty() && System.currentTimeMillis() < deadline) {
182+
problems = completer().diagnose(new CompletionRequest(code, language,
183+
null)).get(2, TimeUnit.MINUTES);
184+
}
185+
assertFalse(problems.isEmpty());
186+
assertTrue(problems.toString(), problems.get(0).start() >= code.indexOf(
187+
"y = "));
188+
// Fixed: no more problems.
189+
assertTrue(completer().diagnose(new CompletionRequest(code.replace("(1 +",
190+
"(1 + 2)"), language, null)).get(1, TimeUnit.MINUTES).isEmpty());
191+
}
192+
154193
@Test
155194
public void testNewDependency() throws Exception {
156195
// A script declaring a dependency that is installed nowhere: its

0 commit comments

Comments
 (0)