Skip to content

Commit c354e4e

Browse files
gselzerclaude
andcommitted
code-lsp: pass fewer nulls around
A ScriptSession is made with its constructor (no more owning factories), from a server and a transform alone: the document says its LSP language (ScriptDocument.languageId), and untitled scripts have no extension. Environment.live() is never null (Environment.NO_STATE, if not typed into an interpreter). The service's lookups throw, rather than return null, when nothing serves a language. ScriptDialect.environment takes the script's file as an Optional. Also, ScriptSession and LanguageServerService are described anew. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 07e28d7 commit c354e4e

18 files changed

Lines changed: 272 additions & 184 deletions

File tree

‎code-lsp-groovy/src/main/java/org/scijava/code/lsp/groovy/GroovyLanguageServer.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,7 @@ private static CompletionItem field(final String name, final String type) {
651651
private Map<String, Class<?>> liveVariables() {
652652
final Map<String, Class<?>> vars = new LinkedHashMap<>();
653653
final ScriptContext live = environment().live();
654-
final Bindings bindings = live == null ? null : live.getBindings(
655-
ScriptContext.ENGINE_SCOPE);
654+
final Bindings bindings = live.getBindings(ScriptContext.ENGINE_SCOPE);
656655
if (bindings != null) {
657656
for (final Map.Entry<String, Object> entry : bindings.entrySet()) {
658657
final String name = entry.getKey();

‎code-lsp-groovy/src/test/java/org/scijava/code/lsp/groovy/GroovyLanguageServerTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.eclipse.lsp4j.SignatureHelp;
4949
import org.junit.jupiter.api.Test;
5050
import org.scijava.code.lsp.Environment;
51+
import org.scijava.code.lsp.ScriptDocument;
5152
import org.scijava.code.lsp.ScriptSession;
5253
import org.scijava.code.lsp.jvm.ClassIndex;
5354

@@ -313,8 +314,8 @@ public void testImportOffset() {
313314

314315
/** A session about plain Groovy, with a server of its own. */
315316
private static ScriptSession session(final Environment env) {
316-
return ScriptSession.owning(new GroovyLanguageServer(env), null, "groovy",
317-
null);
317+
return new ScriptSession(new GroovyLanguageServer(env),
318+
ScriptDocument::of);
318319
}
319320

320321
/** The problems found in a script, e.g. {@code Error@0:4-6 message}. */

‎code-lsp-jedi/src/test/java/org/scijava/code/lsp/jedi/DependencyOverlaysTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void testSettledAndReused() throws Exception {
103103
@Test
104104
public void testEnvironment() throws Exception {
105105
final Environment env = new Environment("/env/python", Collections
106-
.singletonList("/stubs"), null);
106+
.singletonList("/stubs"));
107107
// A script declaring a dependency, just opened: its dependencies are
108108
// settled, so the overlay is installed; once ready, it is searched too.
109109
final TextDocumentItem doc = new TextDocumentItem("untitled:/a.py",

‎code-lsp-jedi/src/test/java/org/scijava/code/lsp/jedi/JediLauncherTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testLanguageInitializationOptions() {
7878
// analyze it with that environment's Python, also searching its extra
7979
// paths (e.g. a dependency overlay)...
8080
final Environment env = new Environment("/env/bin/python", List.of(
81-
"/overlay"), null);
81+
"/overlay"));
8282
assertEquals(Map.of( //
8383
"workspace", Map.of("environmentPath", "/env/bin/python", //
8484
"extraPaths", List.of("/overlay")), //

‎code-lsp-jedi/src/test/java/org/scijava/code/lsp/jedi/PythonLanguageServerIntegrationTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.scijava.code.lsp.Environment;
5151
import org.scijava.code.lsp.LanguageServerService;
5252
import org.scijava.code.lsp.Positions;
53+
import org.scijava.code.lsp.ScriptDocument;
5354
import org.scijava.code.lsp.ScriptSession;
5455

5556
/**
@@ -165,8 +166,8 @@ public void testNewDependency() throws Exception {
165166

166167
/** A session about plain Python, with a server of its own. */
167168
private static ScriptSession session() {
168-
return ScriptSession.owning(servers.launch("python", Environment.NONE),
169-
null, "python", null);
169+
return new ScriptSession(servers.launch("python", Environment.NONE),
170+
ScriptDocument::of);
170171
}
171172

172173
/** The problems found in a script, in a session of its own. */

‎code-lsp-jython/src/main/java/org/scijava/code/lsp/jython/JythonLanguageServer.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ private JythonAutoCompletions.Result completionsAt(final Document doc,
313313
private Map<String, DotAutocompletions> liveVariables() {
314314
final Map<String, DotAutocompletions> vars = new LinkedHashMap<>();
315315
final ScriptContext live = environment().live();
316-
final Bindings bindings = live == null ? null : live.getBindings(
317-
ScriptContext.ENGINE_SCOPE);
316+
final Bindings bindings = live.getBindings(ScriptContext.ENGINE_SCOPE);
318317
if (bindings != null) {
319318
for (final Map.Entry<String, Object> entry : bindings.entrySet()) {
320319
final String name = entry.getKey();

‎code-lsp-jython/src/main/java/org/scijava/code/lsp/jython/PythonCompanion.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static PythonCompanion launch(final LanguageServerService servers,
101101
/** Where the server runs: its own Python, searching the Java stubs. */
102102
static Environment environment(final JavaStubs stubs) {
103103
return new Environment(null, Collections.singletonList(stubs.dir()
104-
.getAbsolutePath()), null);
104+
.getAbsolutePath()));
105105
}
106106

107107
// -- Documents --

‎code-lsp-jython/src/test/java/org/scijava/code/lsp/jython/JythonLanguageServerTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.junit.jupiter.api.Test;
4848
import org.python.jsr223.PyScriptEngineFactory;
4949
import org.scijava.code.lsp.Environment;
50+
import org.scijava.code.lsp.ScriptDocument;
5051
import org.scijava.code.lsp.ScriptSession;
5152
import org.scijava.code.lsp.jvm.ClassIndex;
5253

@@ -194,8 +195,8 @@ public void testSignatureHelp() {
194195

195196
/** A session about plain Jython, with a server of its own. */
196197
private static ScriptSession session(final Environment env) {
197-
return ScriptSession.owning(new JythonLanguageServer(env), null, "jython",
198-
null);
198+
return new ScriptSession(new JythonLanguageServer(env),
199+
ScriptDocument::of);
199200
}
200201

201202
private static SignatureHelp help(final String code) {

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

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

3030
package org.scijava.code.lsp;
3131

32+
import java.io.Reader;
33+
import java.io.StringReader;
34+
import java.io.Writer;
3235
import java.util.ArrayList;
36+
import java.util.Arrays;
3337
import java.util.Collections;
3438
import java.util.List;
3539
import java.util.Objects;
3640

41+
import javax.script.Bindings;
3742
import javax.script.ScriptContext;
43+
import javax.script.SimpleBindings;
3844

3945

4046
/**
@@ -49,25 +55,43 @@
4955
*/
5056
public final class Environment {
5157

58+
/**
59+
* The state of no interpreter, for code not typed into one: no variables,
60+
* and none can be set.
61+
*/
62+
public static final ScriptContext NO_STATE = new NoState();
63+
5264
/** No particular environment. */
53-
public static final Environment NONE = new Environment(null, null, null);
65+
public static final Environment NONE = new Environment(null, null);
5466

5567
private final String interpreter;
5668
private final List<String> paths;
5769
private final ScriptContext live;
5870

71+
/**
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.
77+
*/
78+
public Environment(final String interpreter, final List<String> paths) {
79+
this(interpreter, paths, NO_STATE);
80+
}
81+
5982
/**
6083
* @param interpreter Path of the interpreter or executable parsing the code.
6184
* @param paths Extra places imports are resolved from, in order. Language-dependent.
62-
* @param live Existing state (e.g. from a REPL)
85+
* @param live The state of the interpreter the code is typed into (e.g. a
86+
* REPL's), or {@link #NO_STATE}.
6387
*/
6488
public Environment(final String interpreter, final List<String> paths,
6589
final ScriptContext live)
6690
{
6791
this.interpreter = interpreter;
6892
this.paths = paths == null ? Collections.emptyList() : Collections
6993
.unmodifiableList(new ArrayList<>(paths));
70-
this.live = live;
94+
this.live = Objects.requireNonNull(live, "live");
7195
}
7296

7397
/** Path of the interpreter or executable running the script, or null. */
@@ -84,7 +108,10 @@ public List<String> paths() {
84108
return paths;
85109
}
86110

87-
/** The state of the interpreter the code is typed into, or null. */
111+
/**
112+
* The state of the interpreter the code is typed into; or
113+
* {@link #NO_STATE}, if none.
114+
*/
88115
public ScriptContext live() {
89116
return live;
90117
}
@@ -112,6 +139,102 @@ public int hashCode() {
112139
@Override
113140
public String toString() {
114141
return "Environment[interpreter=" + interpreter + ", paths=" + paths +
115-
(live == null ? "" : ", live") + "]";
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+
}
116239
}
117240
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
import org.scijava.service.SciJavaService;
3939

4040
/**
41-
* Manages {@link LanguageServer}s and creates {@link ScriptSession}s
42-
* for mutating scripts, allowing for a stateful point of interaction.
41+
* Serves SciJava scripts with language servers: a {@link ScriptSession} about
42+
* a script shows servers of its own (from the highest-priority
43+
* {@link LanguageServerPlugin} serving its {@link ScriptDialect}'s language)
44+
* the script as plain code. Servers of an LSP language can also be
45+
* {@link #launch launched} directly.
4346
*
4447
* @author Gabriel Selzer
4548
*/

0 commit comments

Comments
 (0)