Skip to content

Commit 525bc2a

Browse files
gselzerclaude
andcommitted
code-lsp: give each document its own environment
LanguageServerService.launch can now take a function from a document to its environment, which ProcessLanguageServer consults before each request: a document whose environment changes is closed on its old server process and opened on its new one. Completions remember which process answered them, so that they are resolved by it. Also note in ScriptDialect's javadoc why a dialect describes the translation of scripts, rather than doing it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7b9a901 commit 525bc2a

5 files changed

Lines changed: 236 additions & 55 deletions

File tree

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131

3232
import java.util.List;
3333
import java.util.Map;
34+
import java.util.function.Function;
3435

3536
import javax.script.ScriptContext;
3637

3738
import org.eclipse.lsp4j.Diagnostic;
39+
import org.eclipse.lsp4j.TextDocumentItem;
3840
import org.eclipse.lsp4j.services.LanguageServer;
3941
import org.scijava.script.ScriptLanguage;
4042
import org.scijava.service.SciJavaService;
@@ -68,14 +70,33 @@ public interface LanguageServerService extends SciJavaService {
6870
boolean supports(ScriptLanguage language);
6971

7072
/**
71-
* Starts (or reuses) a server process for an LSP language (e.g.
72-
* {@code python}), as a plugin offers it (see
73-
* {@link ServerProcessPlugin}), for scripts running
74-
* in the given environment; or returns null if no plugin
75-
* offers one. For {@link LanguageServerPlugin}s whose servers are
76-
* processes.
73+
* Starts (or reuses) server processes for an LSP language (e.g.
74+
* {@code python}), as a plugin offers them (see
75+
* {@link ServerProcessPlugin}), one per environment scripts run in; or
76+
* returns null if no plugin offers one. For {@link LanguageServerPlugin}s
77+
* whose servers are processes.
78+
* <p>
79+
* Each document is analyzed by the process of the environment its script
80+
* runs in, as {@code environment} says just before each request about it:
81+
* so that, as where it runs changes (e.g. as its dependencies are edited),
82+
* the document moves to another process.
83+
* </p>
84+
*
85+
* @param environment Where the script of a document runs (e.g. as its
86+
* text says).
87+
*/
88+
LanguageServer launch(String languageId,
89+
Function<TextDocumentItem, Environment> environment);
90+
91+
/**
92+
* As {@link #launch(String, Function)}, for scripts that all run in the
93+
* given environment.
7794
*/
78-
LanguageServer launch(String languageId, Environment environment);
95+
default LanguageServer launch(final String languageId,
96+
final Environment environment)
97+
{
98+
return launch(languageId, document -> environment);
99+
}
79100

80101
/**
81102
* Finds the problems in a script, waiting for them: for tools that ask

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@
3535

3636
/**
3737
* What a script language knows about turning a SciJava script into plain code
38-
* of that language (a {@link ScriptDocument}): how its script parameters look
39-
* at run time, how to declare them, and where the script runs.
38+
* of that language (a {@link ScriptDocument})
4039
* <p>
41-
* For example, CPython scripts run via Appose receive images as NumPy arrays,
42-
* declared {@code image: numpy.ndarray}; Groovy scripts receive the Java
43-
* objects themselves, declared {@code net.imglib2.img.Img image;}.
40+
* A dialect describes the translation, rather than doing it: a
41+
* {@link ScriptDocument} translates every script the same way, prepending one
42+
* line of declarations and blanking lines in place, and the dialect supplies
43+
* only the pieces (types, declarations, imports). So, for every language,
44+
* positions in a script and in its document differ by just that line's length,
45+
* which is how a session maps servers' answers back to the script (and leaves
46+
* out those about the line); and the translation is written, and tested, once.
47+
* A language whose scripts need more (e.g. wrapping in a class) would extend
48+
* that rule in {@link ScriptDocument}, for all languages.
4449
* </p>
4550
*
4651
* @author Gabriel Selzer

‎code-lsp/src/main/java/org/scijava/code/lsp/impl/DefaultLanguageServerService.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import java.util.HashMap;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.function.Function;
3536

3637
import javax.script.ScriptContext;
3738

39+
import org.eclipse.lsp4j.TextDocumentItem;
3840
import org.eclipse.lsp4j.services.LanguageServer;
3941
import org.scijava.code.lsp.Environment;
4042
import org.scijava.code.lsp.LanguageServerPlugin;
@@ -105,8 +107,7 @@ public ScriptSession session(final ScriptLanguage language,
105107
// TODO: Consider adding a "merged" LanguageServer that can wrangle all
106108
// matching servers. This could allow custom auto-completions (e.g.
107109
// SciJava Ops) and/or simplify servers combining sources themselves
108-
// (e.g. scripting-jython's PythonCompanion, scripting-appose-python's
109-
// ApposePythonLanguageServer).
110+
// (e.g. scripting-jython's PythonCompanion).
110111
final LanguageServerPlugin plugin = plugin(language);
111112
LanguageServer server = null;
112113
ScriptDialect dialect = null;
@@ -136,7 +137,7 @@ public boolean supports(final ScriptLanguage language) {
136137

137138
@Override
138139
public LanguageServer launch(final String languageId,
139-
final Environment environment)
140+
final Function<TextDocumentItem, Environment> environment)
140141
{
141142
final ServerProcesses s = processes(languageId);
142143
return s == null ? null : new ProcessLanguageServer(s, environment);

0 commit comments

Comments
 (0)