Skip to content

Commit 856b76f

Browse files
gselzerclaude
andcommitted
Offer only an Appose-Python dialect: completion moves to code-lsp-jedi
ApposePythonDialect is now a ScriptDialect plugin: it shows scripts as plain Python and says which built Appose environment each runs in. The tuned server, the overlays and the routing moved to code-lsp-jedi and code-lsp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3a784e5 commit 856b76f

14 files changed

Lines changed: 88 additions & 2307 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,20 @@ script's own line numbers.
137137

138138
## Code completion
139139

140-
The Script Editor completes code in these scripts with a language server
141-
(offered to `scijava-code-lsp`'s `LanguageServerService` by
142-
`ApposePythonServerPlugin`, so any other LSP client of it gets the same):
140+
The Script Editor completes code in these scripts with a Python language
141+
server, `scijava-code-lsp-jedi`'s:
143142
[jedi-language-server](https://github.com/pappasam/jedi-language-server)
144-
(built on [Jedi](https://jedi.readthedocs.io/)). It completes names in scope,
143+
(built on [Jedi](https://jedi.readthedocs.io/)). This plugin's
144+
`ApposePythonDialect` shows it each script as plain Python, and says where
145+
the script runs (so any other client of `scijava-code-lsp`'s
146+
`LanguageServerService` gets the same). It completes names in scope,
145147
the script's `#@` parameters, `task`, builtins and keywords, and members of
146148
objects and modules; and it gives function signatures (with parameter names
147149
and types), documentation, hover and syntax errors.
148150

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),
151-
which is built in the background the first time you complete code, and
152-
takes a while only then; until it is, there are no completions. The server
151+
The server runs in a small environment of its own (`code-lsp-jedi`: Python,
152+
Jedi, jedi-language-server, NumPy and uv), which is built in the background
153+
the first time you complete code, and takes a while only then; until it is, there are no completions. The server
153154
talks to the editor via the
154155
[Language Server Protocol](https://microsoft.github.io/language-server-protocol/)
155156
(see `scijava-code-lsp`); it sees the script as plain Python, with its `#@`
@@ -163,8 +164,8 @@ installed there are known too. Packages the script declares (in its
163164
`# /// script` block) but its environment lacks, e.g. a dependency you just
164165
added, are installed for completion only, into a separate directory, a moment
165166
after you stop editing the dependencies; so they are known before you run the
166-
script. (They are installed with uv, from another small environment,
167-
`appose-python-completion`.) This covers PyPI packages; conda packages (`[tool.pixi.dependencies]`)
167+
script. (`scijava-code-lsp-jedi` does this, for any Python script.) This
168+
covers PyPI packages; conda packages (`[tool.pixi.dependencies]`)
168169
and environments declared in files are known once the script has run.
169170
Completion never builds a script's environment itself, and never disturbs
170171
its worker. Script parameters are known by the Python types they have at run

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* <p>
5151
* Both running a script ({@link ApposePythonScriptEngine}) and completing code
5252
* in it ({@link
53-
* org.scijava.plugins.scripting.appose.python.completion.ApposePythonServerPlugin})
53+
* org.scijava.plugins.scripting.appose.python.completion.ApposePythonDialect})
5454
* resolve its environment here, so that they agree on the environment's name
5555
* and configuration.
5656
* </p>

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,48 @@
4141
import net.imglib2.RandomAccessibleInterval;
4242

4343
import org.apposed.appose.NDArray;
44+
import org.scijava.code.lsp.Environment;
4445
import org.scijava.code.lsp.ScriptDialect;
46+
import org.scijava.plugin.Parameter;
47+
import org.scijava.plugin.Plugin;
48+
import org.scijava.plugins.scripting.appose.python.ApposePythonScriptLanguage;
49+
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
50+
import org.scijava.script.ScriptLanguage;
4551

4652
/**
47-
* Describes how to turn Appose python scripts into plain python code.
53+
* Describes how to turn Appose python scripts into plain python code, and
54+
* where they run (see {@link ApposePythonEnvironments}).
4855
*
4956
* @author Gabriel Selzer
5057
*/
51-
final class ApposePythonDialect implements ScriptDialect {
58+
@Plugin(type = ScriptDialect.class, name = "Appose-Python")
59+
public class ApposePythonDialect implements ScriptDialect {
5260

5361
/** The type of the {@code task} variable scripts can use. */
5462
private static final String TASK_TYPE = "appose.python_worker.Task";
5563

64+
@Parameter(required = false)
65+
private ResidentWorkerService workers;
66+
67+
private ApposePythonEnvironments environments;
68+
69+
@Override
70+
public boolean supports(final ScriptLanguage language) {
71+
return language instanceof ApposePythonScriptLanguage;
72+
}
73+
74+
@Override
75+
public String languageId() {
76+
return "python";
77+
}
78+
79+
@Override
80+
public Environment environment(final String text, final File file) {
81+
// NB: As its #@script line, or its inline metadata, says.
82+
return environments().environment(text, file == null ? null : file
83+
.getPath());
84+
}
85+
5686
@Override
5787
public String runtimeType(final Class<?> type) {
5888
if (type == null) return null;
@@ -103,4 +133,12 @@ public String imported(final String statement) {
103133
public List<String> extraDeclarations() {
104134
return Collections.singletonList(declare("task", TASK_TYPE));
105135
}
136+
137+
/** Gets where scripts run, creating it on first use. */
138+
private synchronized ApposePythonEnvironments environments() {
139+
if (environments == null) {
140+
environments = new ApposePythonEnvironments(workers);
141+
}
142+
return environments;
143+
}
106144
}

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

Lines changed: 16 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,14 @@
3131

3232
import java.io.File;
3333
import java.io.IOException;
34-
import java.io.InputStream;
35-
import java.net.URI;
36-
import java.nio.charset.StandardCharsets;
3734
import java.util.ArrayList;
3835
import java.util.Arrays;
39-
import java.util.Collections;
40-
import java.util.HashMap;
4136
import java.util.LinkedHashMap;
4237
import java.util.List;
4338
import java.util.Map;
44-
import java.util.WeakHashMap;
4539

46-
import org.apposed.appose.Appose;
4740
import org.apposed.appose.Environment;
48-
import org.apposed.appose.Service;
4941
import org.apposed.appose.util.Environments;
50-
import org.eclipse.lsp4j.TextDocumentItem;
5142
import org.scijava.plugins.scripting.appose.python.ScriptEnvironment;
5243
import org.scijava.plugins.scripting.appose.python._internal.InlineMetadata;
5344
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorker;
@@ -56,31 +47,15 @@
5647
/**
5748
* Where Python scripts run via Appose, as their language servers need to
5849
* know: the Python of a script's environment, if built (in this session, or an
59-
* earlier one; never building it, nor disturbing its worker); the overlay of
60-
* packages it declares but its environment lacks (see
61-
* {@link DependencyOverlays}), and the tooling environment overlays are
62-
* installed from.
50+
* earlier one; never building it, nor disturbing its worker). (Packages a
51+
* script declares, but its environment lacks, are known to the servers too;
52+
* see scijava-code-lsp-jedi.)
6353
*
6454
* @author Gabriel Selzer
6555
*/
6656
final class ApposePythonEnvironments {
6757

68-
/** Name of the tooling environment that installs overlays. */
69-
static final String TOOLING_ENV = "appose-python-completion";
70-
71-
/**
72-
* How long a script's dependencies must stay unchanged before packages
73-
* missing from its environment are installed for completion, in ms.
74-
*/
75-
private static final long OVERLAY_SETTLE = 1500;
76-
77-
/** Task scripts (see overlay.py), by resource name. */
78-
private static final Map<String, String> SCRIPTS = new HashMap<>();
79-
80-
/** One per resident worker service (that is, per context). */
81-
private static final Map<ResidentWorkerService, ApposePythonEnvironments> ALL =
82-
new WeakHashMap<>();
83-
58+
/** The resident workers, to find built environments; or null if none. */
8459
private final ResidentWorkerService workers;
8560

8661
/** Python executables of script environments, by script and metadata. */
@@ -96,132 +71,25 @@ protected boolean removeEldestEntry(
9671
}
9772
};
9873

99-
private DependencyOverlays overlays;
100-
101-
private ApposePythonEnvironments(final ResidentWorkerService workers) {
74+
/** @param workers The resident workers, or null if none. */
75+
ApposePythonEnvironments(final ResidentWorkerService workers) {
10276
this.workers = workers;
10377
}
10478

105-
/** Gets those of a context (by its resident worker service). */
106-
static ApposePythonEnvironments of(final ResidentWorkerService workers) {
107-
synchronized (ALL) {
108-
return ALL.computeIfAbsent(workers, ApposePythonEnvironments::new);
109-
}
110-
}
111-
112-
/**
113-
* Derives an {@link org.scijava.code.lsp.Environment} for an Appose script.
114-
* <p>
115-
* The {@code python} executable is found using the script's text and file
116-
* path. For convenience, packages declared in the script but not yet
117-
* installed (i.e. between the time the dependency is added to the script
118-
* and the time when it is run) are provided to the language server in the
119-
* <em>overlay</em>.
120-
* </p>
121-
*
122-
* @param script the script
123-
* @return an {@link org.scijava.code.lsp.Environment} where:
124-
* <ul>
125-
* <li>the interpreter is the Python executable of the script's
126-
* declared environment, <b>if that has already been built</b>,
127-
* or {@code null} otherwise;</li>
128-
* <li>the paths hold the overlay, once installed; none if no package
129-
* is missing, or until it is installed (which starts once the
130-
* script's dependencies stop changing, or at once if the script was
131-
* just opened).</li>
132-
* </ul>
133-
*/
134-
org.scijava.code.lsp.Environment environment(final TextDocumentItem script) {
135-
final String text = script.getText();
136-
final String path = path(script.getUri());
137-
final String python = scriptPython(text, path);
138-
final boolean settled = script.getVersion() <= 1;
139-
// NB: An alternative design could have all the script's dependencies
140-
// in a constant overlay, but that would have the effect of installing them
141-
// twice - once for the overlay and once for the script's environment.
142-
final String overlay = overlay(text, path, python, settled);
143-
return new org.scijava.code.lsp.Environment(python, overlay == null ? null
144-
: Collections.singletonList(overlay), null);
145-
}
146-
147-
/** Sets the dependency overlays, e.g. to use fake ones in tests. */
148-
synchronized void setOverlays(final DependencyOverlays overlays) {
149-
this.overlays = overlays;
150-
}
151-
152-
/** The file path of a {@code file:} URI, or null. */
153-
static String path(final String uri) {
154-
try {
155-
final URI u = URI.create(uri);
156-
return "file".equals(u.getScheme()) ? new File(u).getPath() : null;
157-
}
158-
catch (final IllegalArgumentException exc) {
159-
return null;
160-
}
161-
}
162-
16379
/**
164-
* Gets the overlay of packages the script declares but its environment
165-
* lacks (see {@link DependencyOverlays}), if ready; starts preparing it
166-
* otherwise. Only for environments declared inline, for now.
80+
* Gets where an Appose script runs: the Python executable of its declared
81+
* environment, <b>if that has already been built</b>; or none (the
82+
* servers' own Python), otherwise.
16783
*
168-
* @param settled Whether the dependencies are known not to be changing.
169-
* @return The overlay directory, or null if none is needed or ready.
84+
* @param text The script's text.
85+
* @param path The script's file path, or null if it has none.
17086
*/
171-
String overlay(final String text, final String path,
172-
final String python, final boolean settled)
87+
org.scijava.code.lsp.Environment environment(final String text,
88+
final String path)
17389
{
174-
final DependencyOverlays overlays = overlays();
175-
if (overlays == null) return null;
176-
final List<String> dependencies;
177-
final String name;
178-
try {
179-
final String metadata = InlineMetadata.extract(text);
180-
if (metadata == null) return null;
181-
dependencies = InlineMetadata.dependencies(metadata);
182-
// NB: Unsaved scripts' environments are named after their metadata,
183-
// which changes as dependencies are edited: share one overlay.
184-
name = path == null ? "unsaved" : ScriptEnvironment.inlineName(metadata,
185-
path);
186-
}
187-
catch (final RuntimeException exc) {
188-
return null; // NB: E.g. half-typed metadata.
189-
}
190-
return overlays.overlay(name, python, dependencies, settled);
191-
}
192-
193-
/** Gets the dependency overlays, creating them on first use. */
194-
private synchronized DependencyOverlays overlays() {
195-
if (overlays != null) return overlays;
196-
if (workers == null) return null;
197-
overlays = new DependencyOverlays((python, dependencies,
198-
dir) -> installOverlay(workers, python, dependencies, dir), new File(
199-
Environments.apposeEnvsDir(), TOOLING_ENV + "-overlays"),
200-
OVERLAY_SETTLE, System::currentTimeMillis);
201-
return overlays;
202-
}
203-
204-
/**
205-
* Installs an overlay by running overlay.py on the tooling environment's
206-
* worker. Called on the overlays' background thread.
207-
*/
208-
private static String installOverlay(final ResidentWorkerService workers,
209-
final String python, final List<String> dependencies, final File dir)
210-
{
211-
final Map<String, Object> inputs = new HashMap<>();
212-
inputs.put("python", python);
213-
inputs.put("dependencies", dependencies);
214-
inputs.put("overlay", dir.getAbsolutePath());
215-
try {
216-
final Service.Task task = toolingWorker(workers).task(resource(
217-
"overlay.py"), inputs);
218-
task.waitFor();
219-
final Object overlay = task.outputs.get("path");
220-
return overlay instanceof String ? (String) overlay : null;
221-
}
222-
catch (final Exception exc) {
223-
throw new IllegalStateException("Overlay installation failed", exc);
224-
}
90+
final String python = scriptPython(text, path);
91+
return python == null ? org.scijava.code.lsp.Environment.NONE
92+
: new org.scijava.code.lsp.Environment(python, null, null);
22593
}
22694

22795
/**
@@ -340,56 +208,4 @@ private static String findPython(final List<File> dirs) {
340208
}
341209
return null;
342210
}
343-
344-
/** Gets the tooling environment's worker, building it first if needed. */
345-
private static ResidentWorker toolingWorker(
346-
final ResidentWorkerService workers)
347-
{
348-
final String config = toolingConfig();
349-
return workers.worker(TOOLING_ENV, config, () -> Appose.content(config)
350-
.scheme("pixi.toml"), Environment::python);
351-
}
352-
353-
/**
354-
* The tooling environment: Python and Appose, to run overlay.py; and uv, to
355-
* install packages for {@link DependencyOverlays}.
356-
*/
357-
static String toolingConfig() {
358-
return "[workspace]\n" + //
359-
"name = \"" + TOOLING_ENV + "\"\n" + //
360-
"channels = [\"conda-forge\"]\n" + //
361-
"platforms = [\"" + InlineMetadata.condaPlatform() + "\"]\n" + //
362-
"\n" + //
363-
"[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.
367-
"python = \"3.12.*\"\n" + //
368-
"\n" + //
369-
"[pypi-dependencies]\n" + //
370-
"appose = \"*\"\n" + //
371-
"uv = \"*\"\n";
372-
}
373-
374-
/** Gets a task script (e.g. overlay.py), by resource name. */
375-
private static String resource(final String name) throws IOException {
376-
synchronized (SCRIPTS) {
377-
final String known = SCRIPTS.get(name);
378-
if (known != null) return known;
379-
try (final InputStream in = ApposePythonEnvironments.class
380-
.getResourceAsStream(name))
381-
{
382-
if (in == null) throw new IOException(name + " not found");
383-
final java.io.ByteArrayOutputStream out =
384-
new java.io.ByteArrayOutputStream();
385-
final byte[] buf = new byte[8192];
386-
int n;
387-
while ((n = in.read(buf)) > 0) out.write(buf, 0, n);
388-
final String script = new String(out.toByteArray(),
389-
StandardCharsets.UTF_8);
390-
SCRIPTS.put(name, script);
391-
return script;
392-
}
393-
}
394-
}
395211
}

0 commit comments

Comments
 (0)