Skip to content

Commit f70f80d

Browse files
gselzerclaude
andcommitted
code-lsp: one server per session, from one plugin per language
Drop MergedLanguageServer: a language is served by the highest-priority plugin supporting it, and a server combining several sources (e.g. an in-process analysis and jedi) combines them itself. Each session gets a server of its own, which it initializes (awaiting the answer, as LSP says) and shuts down when closed. Server processes stay shared, one per environment. With it go the service's shared servers and diagnostics routing, moving scripts between servers as where they run changes (LanguageServerPlugin.environment, environmentsChanged), and waiting for several servers' problems. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5f4e417 commit f70f80d

10 files changed

Lines changed: 264 additions & 1242 deletions

File tree

‎code-lsp/README.md‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ scripts.
1515
client (e.g. Script Editor)
1616
│ ScriptSession: a script's text, and offsets into it
1717
▼
18-
LanguageServerService ──► the LanguageServerPlugins serving the language
19-
│ one server standing for all of them (merged answers)
18+
LanguageServerService ──► the LanguageServerPlugin serving the language
19+
│ a server of the session's own
2020
├──► in-process servers (e.g. Groovy, Jython), built on ScriptLanguageServer
2121
└──► server processes (e.g. jedi-language-server), via LspServerSpec
2222
seeing the script as plain code (TransformingLanguageServer)
@@ -27,17 +27,16 @@ It does four things, each in a package of its own.
2727
## 1. Brokering between scripts and servers (`org.scijava.code.lsp`)
2828

2929
The core. `LanguageServerPlugin`s offer lsp4j `LanguageServer`s for script
30-
languages, and say where a script runs (an `Environment`: interpreter, search
31-
paths, classpath, and a running interpreter's state, if any). For each
32-
script, the `LanguageServerService` gathers every server of its language, in
33-
its environment, behind one server: it opens, changes and closes the script
34-
on each, merges their answers, and moves the script to other servers should
35-
where it runs change.
36-
37-
Clients ask about a script through a `ScriptSession` from the service: it
38-
keeps the servers' copy of the script up to date, names it (its file's URI,
39-
or an `untitled:` one), takes offsets into its text (see `Positions`), and
40-
reports the problems found in it. Tools that ask once (e.g. to check a
30+
languages; each language is served by the highest-priority plugin supporting
31+
it. A server combining several sources (e.g. Jython's own analysis and
32+
jedi's) combines them itself.
33+
34+
Clients ask about a script through a `ScriptSession` from the service, with
35+
a server of its own: it keeps the server's copy of the script up to date,
36+
names it (its file's URI, or an `untitled:` one), takes offsets into its text
37+
(see `Positions`), and reports the problems found in it. For code typed into
38+
a running interpreter, the server learns the interpreter's state through its
39+
`Environment`. Tools that ask once (e.g. to check a
4140
script) can call `LanguageServerService.diagnose`. For code typed into an
4241
interpreter whose language no plugin serves, a session completes the
4342
interpreter's variables.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
* modules or packages, classpath entries, and, for code typed into a running
4444
* interpreter, that interpreter's state. Every part is optional.
4545
* <p>
46-
* Language servers are kept one per environment (see
47-
* {@link LanguageServerService}): e.g. jedi-language-server learns the Python
48-
* to analyze against only when it starts.
46+
* Server processes are kept one per environment (see
47+
* {@link LanguageServerService#launch}): e.g. jedi-language-server learns the
48+
* Python to analyze against only when it starts.
4949
* </p>
5050
*
5151
* @author Gabriel Selzer

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

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

3030
package org.scijava.code.lsp;
3131

32-
import org.eclipse.lsp4j.TextDocumentItem;
3332
import org.eclipse.lsp4j.services.LanguageServer;
3433
import org.scijava.plugin.SciJavaPlugin;
3534
import org.scijava.script.ScriptLanguage;
@@ -39,9 +38,9 @@
3938
* implementations of lsp4j's {@link LanguageServer}, or proxies to server
4039
* processes (see {@link LanguageServerService#launch}).
4140
* <p>
42-
* Several plugins may serve one language (e.g. an instant in-process analysis
43-
* and a slower, smarter server process); {@link LanguageServerService} merges
44-
* their answers, in the plugins' priority order.
41+
* Each language is served by the highest-priority plugin that supports it. A
42+
* server combining several sources (e.g. an instant in-process analysis and a
43+
* slower, smarter server process) combines them itself.
4544
* </p>
4645
*
4746
* @author Gabriel Selzer
@@ -52,22 +51,12 @@ public interface LanguageServerPlugin extends SciJavaPlugin {
5251
boolean supports(ScriptLanguage language);
5352

5453
/**
55-
* Gets where the given script will run, as far as this plugin's servers need
56-
* to know: read from its text (e.g. inline metadata) and its URI (e.g. a
57-
* file path). Servers are kept one per environment. Called whenever the
58-
* script changes, so it must be quick; and when told that environments
59-
* changed (see {@link LanguageServerService#environmentsChanged()}), which
60-
* a plugin should call when the answer may change without the script
61-
* changing (e.g. once an environment is built). The default: no particular
62-
* environment (one server for all scripts).
63-
*/
64-
default Environment environment(final TextDocumentItem document) {
65-
return Environment.NONE;
66-
}
67-
68-
/**
69-
* Creates a server for scripts running in the given environment. The
70-
* service initializes it.
54+
* Creates a server for a session (see {@link LanguageServerService#session}):
55+
* the session initializes it, and shuts it down when closed.
56+
*
57+
* @param environment Where the session's script runs, as far as known: for
58+
* code typed into a running interpreter, its state (see
59+
* {@link Environment#live()}).
7160
*/
7261
LanguageServer create(Environment environment);
7362
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
import org.scijava.service.SciJavaService;
4141

4242
/**
43-
* Keeps the language servers of a context (see {@link LanguageServerPlugin}),
44-
* and hands callers one server per language that stands for all of them.
43+
* Hands out sessions about scripts (see {@link ScriptSession}), each with a
44+
* language server of its own, from the highest-priority
45+
* {@link LanguageServerPlugin} serving the script's language.
4546
*
4647
* @author Gabriel Selzer
4748
*/
@@ -63,15 +64,6 @@ public interface LanguageServerService extends SciJavaService {
6364
*/
6465
ScriptSession session(ScriptLanguage language, ScriptContext live);
6566

66-
/**
67-
* Tells the servers that where scripts run may have changed without the
68-
* scripts changing, e.g. as an environment finished building, or packages
69-
* finished installing: each open script moves to the servers of where it
70-
* runs now (see {@link LanguageServerPlugin#environment}), if that
71-
* changed. For plugins, when what their environments depend on changes.
72-
*/
73-
void environmentsChanged();
74-
7567
/** Whether any plugin serves scripts of the given language. */
7668
boolean supports(ScriptLanguage language);
7769

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

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.concurrent.TimeUnit;
3737
import java.util.concurrent.atomic.AtomicInteger;
3838
import java.util.function.Consumer;
39-
import java.util.function.Function;
4039
import java.util.function.Supplier;
4140

4241
import org.eclipse.lsp4j.CompletionItem;
@@ -48,6 +47,9 @@
4847
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
4948
import org.eclipse.lsp4j.Hover;
5049
import org.eclipse.lsp4j.HoverParams;
50+
import org.eclipse.lsp4j.InitializeParams;
51+
import org.eclipse.lsp4j.InitializeResult;
52+
import org.eclipse.lsp4j.InitializedParams;
5153
import org.eclipse.lsp4j.MessageActionItem;
5254
import org.eclipse.lsp4j.MessageParams;
5355
import org.eclipse.lsp4j.PublishDiagnosticsParams;
@@ -78,7 +80,8 @@
7880
* {@link Positions}). Answers are lsp4j's own types.
7981
* </p>
8082
* <p>
81-
* A session owns its server: it is the server's client. Get one from the
83+
* A session owns its server: it is the server's client, initializes it, and
84+
* shuts it down when closed. Get one from the
8285
* {@link LanguageServerService#session service}, which makes it a server of
8386
* its own.
8487
* </p>
@@ -89,9 +92,11 @@ public class ScriptSession implements AutoCloseable {
8992

9093
private static final AtomicInteger UNTITLED = new AtomicInteger();
9194

95+
/** How long to wait for a server to initialize, in milliseconds. */
96+
private static final long INITIALIZE_TIMEOUT = 10_000;
97+
9298
private final LanguageServer server;
9399
private final ScriptLanguage language;
94-
private final Function<String, CompletableFuture<List<Diagnostic>>> allReported;
95100
private final String untitled;
96101
private volatile Supplier<File> file = () -> null;
97102
private volatile Logger log;
@@ -107,15 +112,12 @@ public class ScriptSession implements AutoCloseable {
107112
/**
108113
* @param server The server, which the session owns.
109114
* @param language The script's language, or null if unknown.
110-
* @param allReported Gets the problems in a document once all its servers
111-
* reported them; or null if the server reports them at once.
112115
*/
113-
ScriptSession(final LanguageServer server, final ScriptLanguage language,
114-
final Function<String, CompletableFuture<List<Diagnostic>>> allReported)
116+
private ScriptSession(final LanguageServer server,
117+
final ScriptLanguage language)
115118
{
116119
this.server = server;
117120
this.language = language;
118-
this.allReported = allReported;
119121
final List<String> extensions = language == null ? Collections
120122
.emptyList() : language.getExtensions();
121123
untitled = "untitled:/script-" + UNTITLED.incrementAndGet() + (extensions
@@ -125,40 +127,36 @@ public class ScriptSession implements AutoCloseable {
125127
/**
126128
* Makes a session about a script, with the given server of its own (e.g. a
127129
* fake one, in tests): the session becomes its client, so the server must
128-
* not be shared. The server is taken to report each version's problems at
129-
* once.
130+
* not be shared. The server is initialized now, and shut down when the
131+
* session is closed.
130132
*
131133
* @param server The server, which the session owns.
132134
* @param language The script's language, or null if unknown.
133135
*/
134136
public static ScriptSession owning(final LanguageServer server,
135137
final ScriptLanguage language)
136138
{
137-
return owning(server, language, null);
139+
return new ScriptSession(server, language).connect();
138140
}
139141

140142
/**
141-
* As {@link #owning(LanguageServer, ScriptLanguage)}, for a server that
142-
* stands for several (e.g. one per plugin), which report each version's
143-
* problems separately.
144-
*
145-
* @param server The server, which the session owns.
146-
* @param language The script's language, or null if unknown.
147-
* @param allReported Gets the problems in a document once all its servers
148-
* reported them; or null if the server reports them at once.
143+
* Becomes the client of its server, and initializes it: as LSP says, it
144+
* awaits the server's answer before telling it anything else. (Servers in
145+
* process answer at once.)
149146
*/
150-
public static ScriptSession owning(final LanguageServer server,
151-
final ScriptLanguage language,
152-
final Function<String, CompletableFuture<List<Diagnostic>>> allReported)
153-
{
154-
return new ScriptSession(server, language, allReported).connect();
155-
}
156-
157-
/** Becomes the client of its server. */
158-
ScriptSession connect() {
147+
private ScriptSession connect() {
159148
if (server instanceof LanguageClientAware) {
160149
((LanguageClientAware) server).connect(new Client());
161150
}
151+
try {
152+
final CompletableFuture<InitializeResult> init = server.initialize(
153+
new InitializeParams());
154+
if (init != null) init.get(INITIALIZE_TIMEOUT, TimeUnit.MILLISECONDS);
155+
}
156+
catch (final Exception exc) {
157+
// NB: Not initialized (in time): asked anyway, answering what it can.
158+
}
159+
server.initialized(new InitializedParams());
162160
return this;
163161
}
164162

@@ -194,7 +192,7 @@ public ScriptSession setDiagnosticsListener(
194192
*/
195193
public synchronized ScriptSession update(final String newText) {
196194
final String current = currentUri();
197-
if (uri != null && !uri.equals(current)) close();
195+
if (uri != null && !uri.equals(current)) closeDocument();
198196
if (uri == null) {
199197
uri = current;
200198
version = 1;
@@ -262,8 +260,8 @@ public synchronized List<Diagnostic> diagnostics() {
262260
* Waits for the problems in the text last sent: for tools that ask once,
263261
* rather than editors.
264262
*
265-
* @param timeout How long to wait, in milliseconds; servers not done by
266-
* then are left out.
263+
* @param timeout How long to wait, in milliseconds; if the server is not
264+
* done by then, the problems reported so far.
267265
*/
268266
public List<Diagnostic> diagnostics(final long timeout) {
269267
final String u;
@@ -274,17 +272,30 @@ public List<Diagnostic> diagnostics(final long timeout) {
274272
}
275273
if (u == null) return Collections.emptyList();
276274
try {
277-
return (allReported == null ? next : allReported.apply(u)).get(timeout,
278-
TimeUnit.MILLISECONDS);
275+
return next.get(timeout, TimeUnit.MILLISECONDS);
279276
}
280277
catch (final Exception exc) {
281-
return diagnostics(); // NB: Not all in time: those that were.
278+
return diagnostics(); // NB: Not in time: those reported so far.
282279
}
283280
}
284281

285-
/** Closes the server's copy of the script, if open. */
282+
/** Closes the server's copy of the script, and shuts the server down. */
286283
@Override
287284
public synchronized void close() {
285+
closeDocument();
286+
try {
287+
final CompletableFuture<Object> done = server.shutdown();
288+
if (done != null) done.thenRun(server::exit);
289+
}
290+
catch (final RuntimeException | LinkageError exc) {
291+
if (log != null) log.debug("Language server shutdown failed", exc);
292+
}
293+
}
294+
295+
// -- Helper methods --
296+
297+
/** Closes the server's copy of the script, if open. */
298+
private void closeDocument() {
288299
if (uri == null) return;
289300
server.getTextDocumentService().didClose(new DidCloseTextDocumentParams(
290301
new TextDocumentIdentifier(uri)));
@@ -293,8 +304,6 @@ public synchronized void close() {
293304
diagnostics = Collections.emptyList();
294305
}
295306

296-
// -- Helper methods --
297-
298307
private TextDocumentIdentifier identifier() {
299308
return new TextDocumentIdentifier(uri);
300309
}

0 commit comments

Comments
 (0)