Skip to content

Commit cd8b723

Browse files
gselzerclaude
andcommitted
Get jedi from code-lsp-jedi; route scripts between its servers
jedi-language-server is now offered by scijava-code-lsp-jedi (a runtime dependency), so JediServerProcessPlugin goes; the tooling environment here only installs dependency overlays. code-lsp's servers now analyze scripts in one environment each. Since a script's environment is in its text, and changes as it is edited, the new RoutingLanguageServer keeps a Python server per environment and moves each script to the one where it runs now, as asked before each request (and closes servers left without scripts). Also follow code-lsp's API: plugins say which LSP language they serve (appose-python), and ask whether python is served rather than catching its absence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4cbc7e3 commit cd8b723

9 files changed

Lines changed: 799 additions & 253 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,25 @@ the script's `#@` parameters, `task`, builtins and keywords, and members of
146146
objects and modules; and it gives function signatures (with parameter names
147147
and types), documentation, hover and syntax errors.
148148

149-
The server runs in a small environment of its own
150-
(`appose-python-completion`: Python, jedi-language-server, Appose and uv),
149+
The server, offered by `scijava-code-lsp-jedi`, runs in a small environment
150+
of its own (`code-lsp-jedi`: Python, Jedi, jedi-language-server and NumPy),
151151
which is built in the background the first time you complete code, and
152152
takes a while only then; until it is, there are no completions. The server
153153
talks to the editor via the
154154
[Language Server Protocol](https://microsoft.github.io/language-server-protocol/)
155155
(see `scijava-code-lsp`); it sees the script as plain Python, with its `#@`
156156
parameters declared. One server runs per script environment; servers
157157
unused for 10 minutes stop. Their error output goes to
158-
`appose-python-completion-server.log`, next to Appose's environments.
158+
`code-lsp-jedi-server.log`, next to Appose's environments.
159159

160160
Once a script's environment is built, by running the script (in this session
161161
or an earlier one), the server analyzes the script against it, so that the packages
162162
installed there are known too. Packages the script declares (in its
163163
`# /// script` block) but its environment lacks, e.g. a dependency you just
164164
added, are installed for completion only, into a separate directory, a moment
165165
after you stop editing the dependencies; so they are known before you run the
166-
script. This covers PyPI packages; conda packages (`[tool.pixi.dependencies]`)
166+
script. (They are installed with uv, from another small environment,
167+
`appose-python-completion`.) This covers PyPI packages; conda packages (`[tool.pixi.dependencies]`)
167168
and environments declared in files are known once the script has run.
168169
Completion never builds a script's environment itself, and never disturbs
169170
its worker. Script parameters are known by the Python types they have at run

‎pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<scijava-common.version>2.101.0</scijava-common.version>
9797

9898
<scijava-code-lsp.version>0.1.0-SNAPSHOT</scijava-code-lsp.version>
99+
<scijava-code-lsp-jedi.version>0.1.0-SNAPSHOT</scijava-code-lsp-jedi.version>
99100
</properties>
100101

101102
<dependencies>
@@ -113,6 +114,13 @@
113114
<artifactId>scijava-code-lsp</artifactId>
114115
<version>${scijava-code-lsp.version}</version>
115116
</dependency>
117+
<dependency>
118+
<groupId>org.scijava</groupId>
119+
<artifactId>scijava-code-lsp-jedi</artifactId>
120+
<version>${scijava-code-lsp-jedi.version}</version>
121+
<!-- NB: Its server is found as a plugin, not used directly. -->
122+
<scope>runtime</scope>
123+
</dependency>
116124

117125
<!-- Third-party dependencies -->
118126
<dependency>

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@
5858
* know: the Python of a script's environment, if built (in this session, or an
5959
* earlier one; never building it, nor disturbing its worker); the overlay of
6060
* packages it declares but its environment lacks (see
61-
* {@link DependencyOverlays}); and the tooling environment jedi-language-server
62-
* runs in.
61+
* {@link DependencyOverlays}), and the tooling environment overlays are
62+
* installed from.
6363
*
6464
* @author Gabriel Selzer
6565
*/
6666
final class ApposePythonEnvironments {
6767

68-
/** Name of the tooling environment in which the language server runs. */
68+
/** Name of the tooling environment that installs overlays. */
6969
static final String TOOLING_ENV = "appose-python-completion";
7070

7171
/**
@@ -341,20 +341,6 @@ private static String findPython(final List<File> dirs) {
341341
return null;
342342
}
343343

344-
/**
345-
* Gets the Python of the tooling environment, in which jedi-language-server
346-
* runs (see {@link JediServerProcessPlugin}): built first if needed (but
347-
* starting no worker).
348-
*/
349-
static String toolingPython(final ResidentWorkerService workers)
350-
throws Exception
351-
{
352-
final String python = pythonExecutable(toolingWorker(workers).environment()
353-
.get());
354-
if (python == null) throw new IOException("No Python in " + TOOLING_ENV);
355-
return python;
356-
}
357-
358344
/** Gets the tooling environment's worker, building it first if needed. */
359345
private static ResidentWorker toolingWorker(
360346
final ResidentWorkerService workers)
@@ -365,8 +351,8 @@ private static ResidentWorker toolingWorker(
365351
}
366352

367353
/**
368-
* The tooling environment: Python, Jedi, jedi-language-server, NumPy and
369-
* Appose; and uv, to install packages for {@link DependencyOverlays}.
354+
* The tooling environment: Python and Appose, to run overlay.py; and uv, to
355+
* install packages for {@link DependencyOverlays}.
370356
*/
371357
static String toolingConfig() {
372358
return "[workspace]\n" + //
@@ -375,15 +361,13 @@ static String toolingConfig() {
375361
"platforms = [\"" + InlineMetadata.condaPlatform() + "\"]\n" + //
376362
"\n" + //
377363
"[dependencies]\n" + //
364+
// NB: The overlays of scripts whose environment is not built are
365+
// installed for this Python, for jedi-language-server's Python (see
366+
// code-lsp-jedi) to find: the versions must match.
378367
"python = \"3.12.*\"\n" + //
379-
"jedi = \">=0.19.2\"\n" + //
380-
// NB: Script parameters are typed as NumPy arrays (see
381-
// ApposePythonDialect): known even before a script's environment is.
382-
"numpy = \"*\"\n" + //
383368
"\n" + //
384369
"[pypi-dependencies]\n" + //
385370
"appose = \"*\"\n" + //
386-
"jedi-language-server = \">=0.47\"\n" + //
387371
"uv = \"*\"\n";
388372
}
389373

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,38 @@ public class ApposePythonServerPlugin implements LanguageServerPlugin {
6262
@Parameter
6363
private ResidentWorkerService workers;
6464

65+
@Override
66+
public boolean supports(final String languageId) {
67+
// NB: Not "python", which this builds on. Without jedi, there is
68+
// nothing to serve.
69+
return "appose-python".equals(languageId) && jediOffered();
70+
}
71+
6572
@Override
6673
public boolean supports(final ScriptLanguage language) {
6774
// NB: Without jedi, there is nothing to serve.
6875
return language instanceof ApposePythonScriptLanguage && jediOffered();
6976
}
7077

7178
@Override
72-
public LanguageServer create(final Environment environment) {
73-
return new ApposePythonLanguageServer(servers.launch("python",
74-
ApposePythonEnvironments.of(workers)::environment), SIGNATURES);
79+
public LanguageServer create(final String languageId,
80+
final Environment environment)
81+
{
82+
// NB: Where each script runs is as its text says (see
83+
// ApposePythonEnvironments), whatever the caller thinks; and may change
84+
// as it is edited, moving it to another Python server.
85+
return new ApposePythonLanguageServer(new RoutingLanguageServer(
86+
env -> servers.launch("python", env), ApposePythonEnvironments.of(
87+
workers)::environment), SIGNATURES);
7588
}
7689

7790
@Override
7891
public ScriptDialect dialect() {
7992
return DIALECT;
8093
}
8194

82-
/** Whether jedi-language-server is offered. */
95+
/** Whether jedi-language-server (or another Python server) is offered. */
8396
boolean jediOffered() {
84-
return servers.launch("python", Environment.NONE) != null;
97+
return servers.supports("python");
8598
}
8699
}

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

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

0 commit comments

Comments
 (0)