Skip to content

Commit 970da60

Browse files
gselzerclaude
andcommitted
code-lsp: shrink the API of sessions and environments
ScriptSession talks to its servers itself again, and asks less of callers: problems arrive only through the diagnostics listener (the diagnostics methods are gone), the setters and update return nothing, and setLogger is gone (the context's LogService is used). A closed session stays closed, and resolve asks only the server that made the completions. Environment is made by new Environment() and its with... methods, instead of constructors and NONE; its interpreter and live state are Optionals, so NO_STATE is gone. The service no longer offers scriptParameters (its tests moved onto ScriptProcessing), and ScriptDocument shows only what servers need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0f7adfc commit 970da60

13 files changed

Lines changed: 337 additions & 502 deletions

‎code-lsp/src/main/java/org/scijava/code/lsp/BindingsLanguageServer.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import javax.script.Bindings;
4242
import javax.script.ScriptContext;
43+
import javax.script.SimpleBindings;
4344

4445
import org.eclipse.lsp4j.CompletionItem;
4546
import org.eclipse.lsp4j.CompletionItemKind;
@@ -66,16 +67,15 @@ class BindingsLanguageServer extends AbstractLanguageServer {
6667
private static final Pattern WORD = Pattern.compile(
6768
"([A-Za-z_][A-Za-z0-9_]*)$");
6869

69-
/** @param live The interpreter's state. */
70-
BindingsLanguageServer(final ScriptContext live) {
71-
super(Environment.NONE.withLive(live));
70+
/** @param environment Where the code runs: its live interpreter, if any. */
71+
BindingsLanguageServer(final Environment environment) {
72+
super(environment);
7273
}
7374

7475
@Override
7576
protected CompletionList complete(final Document doc, final int offset) {
76-
final ScriptContext live = environment().live();
77-
final Bindings bindings = live.getBindings(ScriptContext.ENGINE_SCOPE);
78-
if (bindings == null) return new CompletionList();
77+
final Bindings bindings = environment().live().map(l -> l.getBindings(
78+
ScriptContext.ENGINE_SCOPE)).orElseGet(SimpleBindings::new);
7979
final String line = doc.lineTo(offset);
8080

8181
final Matcher member = MEMBER.matcher(line);

‎code-lsp/src/main/java/org/scijava/code/lsp/Environment.java‎

Lines changed: 50 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@
2929

3030
package org.scijava.code.lsp;
3131

32-
import java.io.Reader;
33-
import java.io.StringReader;
34-
import java.io.Writer;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
3732
import java.util.Collections;
3833
import java.util.List;
3934
import java.util.Objects;
35+
import java.util.Optional;
4036

41-
import javax.script.Bindings;
4237
import javax.script.ScriptContext;
43-
import javax.script.SimpleBindings;
4438

4539

4640
/**
@@ -55,47 +49,39 @@
5549
*/
5650
public final class Environment {
5751

52+
/** Path of the interpreter or executable running the script, if known. */
53+
private final Optional<String> interpreter;
5854
/**
59-
* The state of no interpreter, for code not typed into one: no variables,
60-
* and none can be set.
55+
* Extra places imports are resolved from, in order: e.g. directories added
56+
* to Python's {@code sys.path}, or a JVM classpath. Each server reads them as
57+
* its language does. Never null.
6158
*/
62-
public static final ScriptContext NO_STATE = new NoState();
63-
64-
/** No particular environment. */
65-
public static final Environment NONE = new Environment(null, null);
66-
67-
private final String interpreter;
6859
private final List<String> paths;
69-
private final ScriptContext live;
60+
private final Optional<ScriptContext> live;
7061

7162
/**
72-
* An environment for code not typed into an interpreter (see
73-
* {@link #NO_STATE}).
74-
*
75-
* @param interpreter Path of the interpreter or executable parsing the code.
76-
* @param paths Extra places imports are resolved from, in order. Language-dependent.
63+
* No particular environment: no interpreter, no extra paths, and no live
64+
* interpreter. Others are made from it, e.g.
65+
* {@code new Environment().withInterpreter(python)}.
7766
*/
78-
public Environment(final String interpreter, final List<String> paths) {
79-
this(interpreter, paths, NO_STATE);
67+
public Environment() {
68+
this(Optional.empty(), Collections.emptyList(), Optional.empty());
8069
}
8170

8271
/**
83-
* @param interpreter Path of the interpreter or executable parsing the code.
84-
* @param paths Extra places imports are resolved from, in order. Language-dependent.
85-
* @param live The state of the interpreter the code is typed into (e.g. a
86-
* REPL's), or {@link #NO_STATE}.
72+
* Since different environments may or may not have any of these,
73+
* a builder pattern is nice here.
8774
*/
88-
public Environment(final String interpreter, final List<String> paths,
89-
final ScriptContext live)
75+
private Environment(final Optional<String> interpreter,
76+
final List<String> paths, final Optional<ScriptContext> live)
9077
{
9178
this.interpreter = interpreter;
92-
this.paths = paths == null ? Collections.emptyList() : Collections
93-
.unmodifiableList(new ArrayList<>(paths));
94-
this.live = Objects.requireNonNull(live, "live");
79+
this.paths = paths;
80+
this.live = live;
9581
}
9682

97-
/** Path of the interpreter or executable running the script, or null. */
98-
public String interpreter() {
83+
/** Path of the interpreter or executable running the script, if known. */
84+
public Optional<String> interpreter() {
9985
return interpreter;
10086
}
10187

@@ -108,133 +94,55 @@ public List<String> paths() {
10894
return paths;
10995
}
11096

97+
/** The state of the interpreter the code is typed into, if any. */
98+
public Optional<ScriptContext> live() {
99+
return live;
100+
}
101+
111102
/**
112-
* The state of the interpreter the code is typed into; or
113-
* {@link #NO_STATE}, if none.
103+
* A copy of this environment, whose code is parsed by the given interpreter
104+
* or executable.
105+
*
106+
* @param path Path of the interpreter or executable.
114107
*/
115-
public ScriptContext live() {
116-
return live;
108+
public Environment withInterpreter(final String path) {
109+
return new Environment(Optional.of(path), paths, live);
110+
}
111+
112+
/**
113+
* A copy of this environment, whose imports are also resolved from the
114+
* given places, in order. Language-dependent.
115+
*/
116+
public Environment withPaths(final List<String> places) {
117+
return new Environment(interpreter, List.copyOf(places), live);
117118
}
118119

119-
/** A copy of this environment, for code typed into the given interpreter. */
120+
/**
121+
* A copy of this environment, for code typed into the given interpreter
122+
* (e.g. a REPL's).
123+
*/
120124
public Environment withLive(final ScriptContext context) {
121-
return context == live ? this : new Environment(interpreter, paths,
122-
context);
125+
return new Environment(interpreter, paths, Optional.of(context));
123126
}
124127

125128
@Override
126129
public boolean equals(final Object o) {
127130
if (!(o instanceof Environment)) return false;
128131
final Environment that = (Environment) o;
129132
// NB: Live interpreters are the same only if they are the same one.
130-
return Objects.equals(interpreter, that.interpreter) && paths.equals(
131-
that.paths) && live == that.live;
133+
return interpreter.equals(that.interpreter) && paths.equals(that.paths) &&
134+
live.orElse(null) == that.live.orElse(null);
132135
}
133136

134137
@Override
135138
public int hashCode() {
136-
return Objects.hash(interpreter, paths, System.identityHashCode(live));
139+
return Objects.hash(interpreter, paths, System.identityHashCode(live
140+
.orElse(null)));
137141
}
138142

139143
@Override
140144
public String toString() {
141-
return "Environment[interpreter=" + interpreter + ", paths=" + paths +
142-
(live == NO_STATE ? "" : ", live") + "]";
143-
}
144-
145-
/** The state of no interpreter: see {@link #NO_STATE}. */
146-
private static final class NoState implements ScriptContext {
147-
148-
private static final Bindings NONE = new SimpleBindings(Collections
149-
.emptyMap());
150-
151-
private static final Writer NO_OUTPUT = new Writer() {
152-
153-
@Override
154-
public void write(final char[] buf, final int off, final int len) {}
155-
156-
@Override
157-
public void flush() {}
158-
159-
@Override
160-
public void close() {}
161-
};
162-
163-
@Override
164-
public void setBindings(final Bindings bindings, final int scope) {
165-
throw unmodifiable();
166-
}
167-
168-
@Override
169-
public Bindings getBindings(final int scope) {
170-
return NONE;
171-
}
172-
173-
@Override
174-
public void setAttribute(final String name, final Object value,
175-
final int scope)
176-
{
177-
throw unmodifiable();
178-
}
179-
180-
@Override
181-
public Object getAttribute(final String name, final int scope) {
182-
return null;
183-
}
184-
185-
@Override
186-
public Object removeAttribute(final String name, final int scope) {
187-
return null;
188-
}
189-
190-
@Override
191-
public Object getAttribute(final String name) {
192-
return null;
193-
}
194-
195-
@Override
196-
public int getAttributesScope(final String name) {
197-
return -1;
198-
}
199-
200-
@Override
201-
public Writer getWriter() {
202-
return NO_OUTPUT;
203-
}
204-
205-
@Override
206-
public Writer getErrorWriter() {
207-
return NO_OUTPUT;
208-
}
209-
210-
@Override
211-
public void setWriter(final Writer writer) {
212-
throw unmodifiable();
213-
}
214-
215-
@Override
216-
public void setErrorWriter(final Writer writer) {
217-
throw unmodifiable();
218-
}
219-
220-
@Override
221-
public Reader getReader() {
222-
return new StringReader("");
223-
}
224-
225-
@Override
226-
public void setReader(final Reader reader) {
227-
throw unmodifiable();
228-
}
229-
230-
@Override
231-
public List<Integer> getScopes() {
232-
return Arrays.asList(ENGINE_SCOPE, GLOBAL_SCOPE);
233-
}
234-
235-
private static UnsupportedOperationException unmodifiable() {
236-
return new UnsupportedOperationException(
237-
"The state of no interpreter cannot be changed");
238-
}
145+
return "Environment[interpreter=" + interpreter.orElse("none") +
146+
", paths=" + paths + (live.isPresent() ? ", live" : "") + "]";
239147
}
240148
}

‎code-lsp/src/main/java/org/scijava/code/lsp/LanguageServerService.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
package org.scijava.code.lsp;
3131

32-
import java.util.Map;
3332

3433
import org.eclipse.lsp4j.services.LanguageServer;
3534
import org.scijava.script.ScriptLanguage;
@@ -82,14 +81,4 @@ public interface LanguageServerService extends SciJavaService {
8281
* @throws IllegalArgumentException If no plugin serves the language.
8382
*/
8483
LanguageServer launch(String languageId, Environment environment);
85-
86-
/**
87-
* Gets the variables a script's parameters ({@code #@} lines, and old-style
88-
* {@code // @} lines in its header) define before it runs, by name, with
89-
* their Java types ({@code Object} if unresolvable); in declaration order.
90-
* Sessions declare them for their servers (see
91-
* {@link ScriptDocument}). Half-typed declarations
92-
* are expected, and parsed quietly.
93-
*/
94-
Map<String, Class<?>> scriptParameters(String text);
9584
}

‎code-lsp/src/main/java/org/scijava/code/lsp/ScriptDialect.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public interface ScriptDialect extends SciJavaPlugin {
8383
* environment it declares), to analyze it there. Asked before each request
8484
* about the script, as that may change; its session then moves to a server
8585
* of where it runs now (see {@link ScriptSession#update}). The default
86-
* knows nothing: {@link Environment#NONE}.
86+
* knows nothing: a blank {@link Environment}.
8787
*
8888
* @param text The script, as written (e.g. with its {@code #@script} line,
8989
* which its servers do not see).
@@ -94,7 +94,7 @@ public interface ScriptDialect extends SciJavaPlugin {
9494
default Environment environment(final String text,
9595
final Optional<File> file)
9696
{
97-
return Environment.NONE;
97+
return new Environment();
9898
}
9999

100100
/**
@@ -106,7 +106,7 @@ default Environment environment(final String text,
106106
* type may be left out.
107107
*
108108
* @param parameters The script's parameters (see
109-
* {@link LanguageServerService#scriptParameters}), by name, with
109+
* {@link ScriptDocument}), by name, with
110110
* their Java types, in order.
111111
*/
112112
Header header(Map<String, Class<?>> parameters);

0 commit comments

Comments
 (0)