Skip to content

Commit 68fc9e9

Browse files
gselzerclaude
andcommitted
Offer jedi-language-server as the Python language server
ApposePythonLspServerPlugin offers it via code-lsp's LspService, for this completer and others (e.g. Jython's); PythonLspClient moved to code-lsp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 047532e commit 68fc9e9

4 files changed

Lines changed: 42 additions & 203 deletions

File tree

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,15 @@
6969
import org.scijava.code.api.Signatures;
7070
import org.scijava.code.lsp.LspClient;
7171
import org.scijava.code.lsp.LspServers;
72-
import org.scijava.log.LogService;
73-
import org.scijava.log.StderrLogService;
72+
import org.scijava.code.lsp.LspService;
73+
import org.scijava.code.lsp.PythonLspClient;
7474
import org.scijava.plugin.Plugin;
7575
import org.scijava.plugins.scripting.appose.python._internal.InlineMetadata;
7676
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorker;
7777
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
7878
import org.scijava.plugins.scripting.appose.python.completion.ApposePythonDialect;
7979
import org.scijava.plugins.scripting.appose.python.completion.DependencyOverlays;
8080
import org.scijava.plugins.scripting.appose.python.completion.JediLanguageServerSpec;
81-
import org.scijava.plugins.scripting.appose.python.completion.PythonLspClient;
8281
import org.scijava.plugins.scripting.appose.python.completion.PythonSyntax;
8382
import org.scijava.plugins.scripting.appose.python.completion.PythonSyntax.Kind;
8483
import org.scijava.script.ScriptLanguage;
@@ -124,14 +123,6 @@ public class ApposePythonCodeCompleter extends AbstractCodeCompleterPlugin {
124123
/** How many completions get signatures, once a word is begun. */
125124
private static final int SIGNATURES = 30;
126125

127-
/** How long a language server may go unused before it stops, in ms. */
128-
private static final long SERVER_IDLE = 10 * 60 * 1000;
129-
130-
/**
131-
* How many language servers may run at once (one per script environment
132-
* and overlay).
133-
*/
134-
private static final int MAX_SERVERS = 3;
135126

136127
/** Python executables of script environments, by script and metadata. */
137128
private final Map<String, String> pythons = new LinkedHashMap<String, String>(
@@ -609,27 +600,36 @@ private static String findPython(final List<File> dirs) {
609600
return null;
610601
}
611602

612-
/** Gets the language server client, creating it on first use. */
603+
/**
604+
* Gets the language server client, creating it on first use; for the
605+
* Python servers of the context (see {@link ApposePythonLspServerPlugin}).
606+
*/
613607
private synchronized LspClient lspClient() {
614608
if (lsp != null) return lsp;
615-
final ResidentWorkerService workers = service(
616-
ResidentWorkerService.class);
617-
if (workers == null) return null;
618-
LogService log = service(LogService.class);
619-
if (log == null) log = new StderrLogService();
609+
final LspService service = service(LspService.class);
610+
final LspServers servers = service == null ? null : service.servers(
611+
"python");
612+
if (servers == null) return null;
613+
lsp = new PythonLspClient(servers);
614+
return lsp;
615+
}
616+
617+
/**
618+
* How to start jedi-language-server: with the Python of the tooling
619+
* environment, built first if needed (but starting no worker).
620+
*/
621+
static JediLanguageServerSpec serverSpec(
622+
final ResidentWorkerService workers)
623+
{
620624
final File envs = new File(Environments.apposeEnvsDir());
621-
final JediLanguageServerSpec spec = new JediLanguageServerSpec(() -> {
622-
// NB: Builds the tooling environment, if needed (but starts no worker).
625+
return new JediLanguageServerSpec(() -> {
623626
final String python = pythonExecutable(toolingWorker(workers)
624627
.environment().get());
625628
if (python == null) throw new IOException("No Python in " +
626629
TOOLING_ENV);
627630
return python;
628631
}, new File(envs, TOOLING_ENV + "-workspace"), new File(envs, TOOLING_ENV +
629632
"-server.log"));
630-
lsp = new PythonLspClient(new LspServers(spec, log, SERVER_IDLE,
631-
MAX_SERVERS));
632-
return lsp;
633633
}
634634

635635
/** Gets the tooling environment's worker, building it first if needed. */

src/test/java/org/scijava/plugins/scripting/appose/python/completion/PythonLspClientTest.java renamed to src/main/java/org/scijava/plugins/scripting/appose/python/ApposePythonLspServerPlugin.java

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,34 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.plugins.scripting.appose.python.completion;
30+
package org.scijava.plugins.scripting.appose.python;
3131

32-
import static org.junit.Assert.assertEquals;
33-
import static org.junit.Assert.assertNull;
34-
35-
import org.junit.Test;
36-
import org.scijava.code.api.Completion;
32+
import org.scijava.code.lsp.LspServerPlugin;
33+
import org.scijava.code.lsp.LspServerSpec;
34+
import org.scijava.plugin.Parameter;
35+
import org.scijava.plugin.Plugin;
36+
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
3737

3838
/**
39-
* Tests {@link PythonLspClient}'s reading of signatures.
39+
* Offers jedi-language-server, from the completion tooling environment, as
40+
* the Python language server: for Python scripts run via Appose, and for any
41+
* other completer wanting one (e.g. Jython's).
4042
*
4143
* @author Gabriel Selzer
4244
*/
43-
public class PythonLspClientTest {
44-
45-
@Test
46-
public void testParse() {
47-
Completion c = parse("dumps",
48-
"def dumps(obj, *, skipkeys: bool=False, cls=None, **kw) -> str");
49-
assertEquals("[obj, bool skipkeys, cls, kw]", String.valueOf(c
50-
.parameters()));
51-
assertEquals("str", c.returnType());
52-
53-
// Brackets and strings in annotations and defaults; positional-only.
54-
c = parse("zeros", "def zeros(shape: tuple[int, ...], dtype: str=\"f,8\"" +
55-
", /, order: Literal['C', 'F']='C') -> NDArray[float64]");
56-
assertEquals("[tuple[int, ...] shape, str dtype, " +
57-
"Literal['C', 'F'] order]", String.valueOf(c.parameters()));
58-
assertEquals("NDArray[float64]", c.returnType());
45+
@Plugin(type = LspServerPlugin.class, name = "jedi-language-server")
46+
public class ApposePythonLspServerPlugin implements LspServerPlugin {
5947

60-
// Classes (their constructors), without parameters or return type.
61-
c = parse("Path", "class Path()");
62-
assertEquals(0, c.parameters().size());
63-
assertNull(c.returnType());
64-
c = parse("f", "def f(*args)");
65-
assertEquals("args", c.parameters().get(0).name());
48+
@Parameter
49+
private ResidentWorkerService workers;
6650

67-
// Not a signature.
68-
assertNull(PythonLspClient.parse("x", "instance x"));
51+
@Override
52+
public boolean supports(final String languageId) {
53+
return "python".equals(languageId);
6954
}
7055

71-
private static Completion parse(final String name, final String sig) {
72-
return PythonLspClient.parse(name, sig).kind(Completion.Kind.METHOD)
73-
.build();
56+
@Override
57+
public LspServerSpec spec() {
58+
return ApposePythonCodeCompleter.serverSpec(workers);
7459
}
7560
}

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

Lines changed: 0 additions & 146 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* <p>
5252
* This needs no Python, so it works instantly, before (or without) any Appose
5353
* environment. It knows nothing of types, though: members of objects come
54-
* from the language server (see {@link PythonLspClient}).
54+
* from the language server (see {@link org.scijava.code.lsp.PythonLspClient}).
5555
* </p>
5656
*
5757
* @author Gabriel Selzer

0 commit comments

Comments
 (0)