4242import java .util .List ;
4343import java .util .Map ;
4444import java .util .WeakHashMap ;
45- import java .util .concurrent .CopyOnWriteArrayList ;
4645
4746import org .apposed .appose .Appose ;
4847import org .apposed .appose .Environment ;
4948import org .apposed .appose .Service ;
5049import org .apposed .appose .util .Environments ;
50+ import org .eclipse .lsp4j .TextDocumentItem ;
5151import org .scijava .plugins .scripting .appose .python .ScriptEnvironment ;
5252import org .scijava .plugins .scripting .appose .python ._internal .InlineMetadata ;
5353import org .scijava .plugins .scripting .appose .python ._internal .ResidentWorker ;
54- import org .scijava .plugins .scripting .appose .python ._internal .BuildListener ;
5554import 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