Skip to content

Commit 4cbc7e3

Browse files
gselzerclaude
andcommitted
Choose each script's environment as it is analyzed
ApposePythonEnvironments now gives the environment of each script as code-lsp asks, before each request, so the script moves to another jedi process once its environment is built or its overlay is ready. This replaces the router that relocated scripts between servers, and the listeners that told it to; PythonCompletionServer becomes ApposePythonLanguageServer in its place. Empty answers are no longer reused, as Jedi may know more once asked again (e.g. after a new dependency's overlay is ready). Everything but the two plugins is now package-private. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9552b91 commit 4cbc7e3

10 files changed

Lines changed: 532 additions & 1344 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ script's own line numbers.
138138
## Code completion
139139

140140
The Script Editor completes code in these scripts with a language server
141-
(`ApposePythonLanguageServer`, offered to `scijava-code-lsp`'s
142-
`LanguageServerService` by `ApposePythonServerPlugin`, so any other LSP client
143-
of it gets the same): [jedi-language-server](https://github.com/pappasam/jedi-language-server)
141+
(offered to `scijava-code-lsp`'s `LanguageServerService` by
142+
`ApposePythonServerPlugin`, so any other LSP client of it gets the same):
143+
[jedi-language-server](https://github.com/pappasam/jedi-language-server)
144144
(built on [Jedi](https://jedi.readthedocs.io/)). It completes names in scope,
145145
the script's `#@` parameters, `task`, builtins and keywords, and members of
146146
objects and modules; and it gives function signatures (with parameter names

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@
4444
import org.scijava.code.lsp.ScriptDialect;
4545

4646
/**
47-
* How Python scripts run via Appose see their script parameters: as the
48-
* Python values the Java values become (see
49-
* {@code ApposePythonScriptEngine#marshal}), plus Appose's {@code task}.
47+
* Describes how to turn Appose python scripts into plain python code.
5048
*
5149
* @author Gabriel Selzer
5250
*/
53-
public final class ApposePythonDialect implements ScriptDialect {
51+
final class ApposePythonDialect implements ScriptDialect {
5452

5553
/** The type of the {@code task} variable scripts can use. */
56-
public static final String TASK_TYPE = "appose.python_worker.Task";
54+
private static final String TASK_TYPE = "appose.python_worker.Task";
5755

5856
@Override
5957
public String runtimeType(final Class<?> type) {

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

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@
4242
import java.util.List;
4343
import java.util.Map;
4444
import java.util.WeakHashMap;
45-
import java.util.concurrent.CopyOnWriteArrayList;
4645

4746
import org.apposed.appose.Appose;
4847
import org.apposed.appose.Environment;
4948
import org.apposed.appose.Service;
5049
import org.apposed.appose.util.Environments;
50+
import org.eclipse.lsp4j.TextDocumentItem;
5151
import org.scijava.plugins.scripting.appose.python.ScriptEnvironment;
5252
import org.scijava.plugins.scripting.appose.python._internal.InlineMetadata;
5353
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorker;
54-
import org.scijava.plugins.scripting.appose.python._internal.BuildListener;
5554
import org.scijava.plugins.scripting.appose.python._internal.ResidentWorkerService;
5655

5756
/**
@@ -99,19 +98,8 @@ protected boolean removeEldestEntry(
9998

10099
private DependencyOverlays overlays;
101100

102-
/** Told when where scripts run may have changed. */
103-
private final List<Runnable> listeners = new CopyOnWriteArrayList<>();
104-
105101
private ApposePythonEnvironments(final ResidentWorkerService workers) {
106102
this.workers = workers;
107-
// NB: A script run builds its environment: its Python is known then.
108-
if (workers != null) workers.addBuildListener(new BuildListener() {
109-
110-
@Override
111-
public void buildFinished(final String envName, final Throwable error) {
112-
if (error == null) changed();
113-
}
114-
});
115103
}
116104

117105
/** Gets those of a context (by its resident worker service). */
@@ -122,43 +110,43 @@ static ApposePythonEnvironments of(final ResidentWorkerService workers) {
122110
}
123111

124112
/**
125-
* Gets where a script runs, for its language server: its environment's
126-
* Python (or null, if not built), and its overlay (if any, and ready).
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>
127121
*
128-
* @param uri The script's URI (a file path, if saved).
129-
* @param settled Whether the dependencies are known not to be changing
130-
* (e.g. on opening the script).
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>
131133
*/
132-
org.scijava.code.lsp.Environment environment(final String text,
133-
final String uri, final boolean settled)
134-
{
135-
final String path = path(uri);
134+
org.scijava.code.lsp.Environment environment(final TextDocumentItem script) {
135+
final String text = script.getText();
136+
final String path = path(script.getUri());
136137
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.
137142
final String overlay = overlay(text, path, python, settled);
138143
return new org.scijava.code.lsp.Environment(python, overlay == null ? null
139144
: Collections.singletonList(overlay), null);
140145
}
141146

142-
/**
143-
* Adds what to do when where scripts run may have changed without them
144-
* changing: when an environment is built, or an overlay is ready.
145-
*/
146-
void addListener(final Runnable listener) {
147-
listeners.add(listener);
148-
}
149-
150-
void removeListener(final Runnable listener) {
151-
listeners.remove(listener);
152-
}
153-
154-
private void changed() {
155-
for (final Runnable listener : listeners) listener.run();
156-
}
157-
158147
/** Sets the dependency overlays, e.g. to use fake ones in tests. */
159148
synchronized void setOverlays(final DependencyOverlays overlays) {
160149
this.overlays = overlays;
161-
if (overlays != null) overlays.setOnReady(this::changed);
162150
}
163151

164152
/** The file path of a {@code file:} URI, or null. */
@@ -210,7 +198,6 @@ private synchronized DependencyOverlays overlays() {
210198
dir) -> installOverlay(workers, python, dependencies, dir), new File(
211199
Environments.apposeEnvsDir(), TOOLING_ENV + "-overlays"),
212200
OVERLAY_SETTLE, System::currentTimeMillis);
213-
overlays.setOnReady(this::changed);
214201
return overlays;
215202
}
216203

@@ -243,7 +230,7 @@ private static String installOverlay(final ResidentWorkerService workers,
243230
* earlier one. Completion never builds it. Returns null if not built.
244231
*/
245232
String scriptPython(final String text, final String path) {
246-
// NB: Called on every keystroke, so remember what was found (but not
233+
// NB: Called before each request, so remember what was found (but not
247234
// what was not: the environment may be built at any moment).
248235
final String key;
249236
try {

0 commit comments

Comments
 (0)