Skip to content

Commit 7b9a901

Browse files
gselzerclaude
andcommitted
code-lsp: run server processes from ServerProcessPlugins
A server process is now offered by a ServerProcessPlugin, in the core package: it starts the process for an LSP language and an environment, and says how to initialize it (options, workspace root). The launch package (LspServerPlugin, LspServerSpec, LspConnection) and the separate LspService are gone; LanguageServerService#launch serves the processes itself. The implementation follows the new names: ServerProcesses keeps a language's ServerProcess per environment, and a ProcessLanguageServer shows one as an lsp4j LanguageServer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent eba6480 commit 7b9a901

18 files changed

Lines changed: 187 additions & 509 deletions

‎code-lsp/README.md‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ scripts.
1919
LanguageServerService ──► the LanguageServerPlugin serving the language
2020
│ a server of the session's own, and its ScriptDialect
2121
├──► in-process servers (e.g. Groovy, Jython), built on ScriptLanguageServer
22-
└──► server processes (e.g. jedi-language-server), via LspServerSpec
22+
└──► server processes (e.g. jedi-language-server), via ServerProcessPlugin
2323
```
2424

25-
It does three things, each in a package of its own.
25+
It does three things.
2626

2727
## 1. Brokering between scripts and servers (`org.scijava.code.lsp`)
2828

@@ -49,14 +49,15 @@ a `ScriptDialect` says. Each `LanguageServerPlugin` names the dialect its
4949
servers read; a session shows its server the script in that dialect, and maps
5050
positions in the server's answers back.
5151

52-
## 2. Running server processes (`org.scijava.code.lsp.launch`)
52+
## 2. Running server processes (`org.scijava.code.lsp`)
5353

5454
For servers that are not Java in the SciJava process (e.g. jedi-language-server,
55-
for Python). An `LspServerPlugin` offers an `LspServerSpec` for an LSP
56-
language id (e.g. `python`), saying how to start a server for an environment
57-
and returning its streams (an `LspConnection`). Plugins get such a server via
58-
`LanguageServerService.launch`: started when first needed, one per
59-
environment, restarted if it stops, and stopped when idle.
55+
for Python). A `ServerProcessPlugin` says how to start one for an LSP language
56+
id (e.g. `python`) and an environment: the process, speaking LSP over its
57+
standard streams, and the options and workspace root to initialize it with.
58+
Plugins get such a server via `LanguageServerService.launch`: started when
59+
first needed, one per environment, restarted if it stops, and stopped when
60+
idle.
6061

6162
## 3. A toolkit for in-process servers (`org.scijava.code.lsp.jvm`)
6263

@@ -77,6 +78,6 @@ javadoc.
7778
| scripting-jython | ✓ | ✓ | ✓ |
7879
| scripting-appose-python | ✓ | ✓ | |
7980

80-
The specialized packages depend only on `org.scijava.code.lsp`, never on each
81-
other. The module exports all three; its implementation
81+
The toolkit (`org.scijava.code.lsp.jvm`) depends only on
82+
`org.scijava.code.lsp`. The module exports both; its implementation
8283
(`org.scijava.code.lsp.impl`) is internal.

‎code-lsp/src/main/java/module-info.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
open module org.scijava.code.lsp {
3434
exports org.scijava.code.lsp;
3535
exports org.scijava.code.lsp.jvm;
36-
exports org.scijava.code.lsp.launch;
3736

3837
// NB: SciJava Common, whose plugins and services these are.
3938
requires transitive org.scijava;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public interface LanguageServerService extends SciJavaService {
7070
/**
7171
* Starts (or reuses) a server process for an LSP language (e.g.
7272
* {@code python}), as a plugin offers it (see
73-
* {@link org.scijava.code.lsp.launch.LspServerPlugin}), for scripts running
73+
* {@link ServerProcessPlugin}), for scripts running
7474
* in the given environment; or returns null if no plugin
7575
* offers one. For {@link LanguageServerPlugin}s whose servers are
7676
* processes.

code-lsp/src/main/java/org/scijava/code/lsp/launch/LspServerSpec.java renamed to code-lsp/src/main/java/org/scijava/code/lsp/ServerProcessPlugin.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,39 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.code.lsp.launch;
30+
package org.scijava.code.lsp;
3131

3232
import java.io.IOException;
33-
import org.scijava.code.lsp.Environment;
3433

34+
import org.scijava.plugin.SciJavaPlugin;
3535

3636
/**
37-
* How to start a language server for scripts that run in a given environment,
38-
* e.g. jedi-language-server, told to use the environment's Python.
37+
* Offers a language server that runs as a process, for an LSP language (e.g.
38+
* jedi-language-server, for {@code python}): how to start it for scripts
39+
* running in a given environment. {@link LanguageServerPlugin}s get such a
40+
* server via {@link LanguageServerService#launch}: started when first needed,
41+
* one per environment, restarted if it stops, and stopped when idle.
42+
* <p>
43+
* For example, scripting-appose-python offers jedi-language-server, which
44+
* scripting-jython's server uses too.
45+
* </p>
3946
*
4047
* @author Gabriel Selzer
4148
*/
42-
public interface LspServerSpec {
49+
public interface ServerProcessPlugin extends SciJavaPlugin {
4350

4451
/** The LSP language identifier of the documents, e.g. {@code python}. */
4552
String languageId();
4653

4754
/**
48-
* Starts a server. Called on a background thread; may be slow (e.g. build
49-
* the environment the server runs in).
55+
* Starts a server, which speaks LSP over its standard input and output. (Its
56+
* standard error must be redirected, lest it block once the pipe fills.)
57+
* Called on a background thread; may be slow (e.g. build the environment
58+
* the server runs in).
5059
*
5160
* @param env Where the scripts the server will analyze run.
5261
*/
53-
LspConnection start(Environment env) throws IOException;
62+
Process start(Environment env) throws IOException;
5463

5564
/**
5665
* The {@code initializationOptions} to send the server, e.g. the

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

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

3030
package org.scijava.code.lsp.impl;
3131

32+
import java.util.HashMap;
3233
import java.util.List;
3334
import java.util.Map;
3435

@@ -41,7 +42,9 @@
4142
import org.scijava.code.lsp.ScriptSession;
4243
import org.scijava.code.lsp.ScriptDialect;
4344
import org.scijava.code.lsp.ScriptDocument;
45+
import org.scijava.code.lsp.ServerProcessPlugin;
4446
import org.scijava.log.LogService;
47+
import org.scijava.log.StderrLogService;
4548
import org.scijava.plugin.Parameter;
4649
import org.scijava.plugin.Plugin;
4750
import org.scijava.plugin.PluginService;
@@ -54,7 +57,8 @@
5457
* <p>
5558
* Each session gets a server of its own, from the highest-priority plugin
5659
* serving its language, and shows it scripts as plain code in the plugin's
57-
* dialect. (Server processes behind them are shared; see {@link #launch}.)
60+
* dialect. Server processes behind them (see {@link #launch}) are shared, and
61+
* stopped when the context is disposed.
5862
* </p>
5963
*
6064
* @author Gabriel Selzer
@@ -64,15 +68,25 @@ public class DefaultLanguageServerService extends AbstractService implements
6468
LanguageServerService
6569
{
6670

71+
/** How long a server process may go unused before it stops, in ms. */
72+
private static final long IDLE = Long.getLong("scijava.lsp.idle", 10 * 60 *
73+
1000);
74+
75+
/**
76+
* How many server processes of a language may run at once (one per
77+
* environment scripts run in).
78+
*/
79+
private static final int MAX = Integer.getInteger("scijava.lsp.max", 4);
80+
6781
@Parameter
6882
private PluginService pluginService;
6983

70-
@Parameter(required = false)
71-
private LspService lspService;
72-
7384
@Parameter(required = false)
7485
private LogService log;
7586

87+
/** Server processes, by LSP language identifier; null if none offered. */
88+
private final Map<String, ServerProcesses> processes = new HashMap<>();
89+
7690
private final ScriptParameters parameters = new ScriptParameters(
7791
this::context);
7892

@@ -124,18 +138,47 @@ public boolean supports(final ScriptLanguage language) {
124138
public LanguageServer launch(final String languageId,
125139
final Environment environment)
126140
{
127-
final LspServers s = lspService == null ? null : lspService.servers(
128-
languageId);
129-
return s == null ? null : new LaunchedLanguageServer(s, environment);
141+
final ServerProcesses s = processes(languageId);
142+
return s == null ? null : new ProcessLanguageServer(s, environment);
130143
}
131144

132145
@Override
133146
public Map<String, Class<?>> scriptParameters(final String text) {
134147
return parameters.of(text);
135148
}
136149

150+
// -- Disposable methods --
151+
152+
@Override
153+
public synchronized void dispose() {
154+
for (final ServerProcesses s : processes.values()) {
155+
if (s != null) s.shutdown();
156+
}
157+
processes.clear();
158+
}
159+
137160
// -- Helper methods --
138161

162+
/**
163+
* The server processes for an LSP language, as the first plugin offering
164+
* them says; or null if none does.
165+
*/
166+
private synchronized ServerProcesses processes(final String languageId) {
167+
if (processes.containsKey(languageId)) return processes.get(languageId);
168+
ServerProcesses s = null;
169+
for (final ServerProcessPlugin p : pluginService.createInstancesOfType(
170+
ServerProcessPlugin.class))
171+
{
172+
if (languageId.equals(p.languageId())) {
173+
s = new ServerProcesses(p, log != null ? log : new StderrLogService(),
174+
IDLE, MAX);
175+
break;
176+
}
177+
}
178+
processes.put(languageId, s);
179+
return s;
180+
}
181+
139182
/**
140183
* The highest-priority plugin serving a language, or null if none.
141184
*

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

Lines changed: 0 additions & 94 deletions
This file was deleted.

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)