Skip to content

Commit bea114f

Browse files
gselzerclaude
andcommitted
code-lsp: find every server via LanguageServerPlugins
LanguageServerService.launch(languageId, environment) now gets a server for any LSP language a plugin serves, in-process or not: plugins say so via supports(String), and supports(ScriptLanguage) for sessions. Server processes are served by ProcessLanguageServerPlugin (internal), which owns them and stops them when disposed; LanguageServerService also tells whether a language is served at all, via supports(String). Servers analyze scripts in one environment, given when created: plugins whose scripts say where they run keep a server per environment themselves. ProcessLanguageServer no longer moves documents. Also: - Rename ScriptLanguageServer to AbstractLanguageServer. - Throw rather than return null when no plugin serves a language. - Remove LanguageServerService.diagnose: a session does as much. - Stop started server processes directly on shutdown, rather than via the starter, whose shutdown could drop the stop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7e22853 commit bea114f

13 files changed

Lines changed: 358 additions & 295 deletions

‎code-lsp/README.md‎

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

2525
It does three things.
2626

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

29-
The core. `LanguageServerPlugin`s offer lsp4j `LanguageServer`s for script
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
29+
The core. `LanguageServerPlugin`s offer lsp4j `LanguageServer`s for
30+
languages, by LSP language id (e.g. `groovy`), and so for the SciJava script
31+
languages of those names; each language is served by the highest-priority
32+
plugin supporting it. A server combining several sources (e.g. Jython's own analysis and
3233
jedi's) combines them itself.
3334

3435
Clients ask about a script through a `ScriptSession` from the service, with
3536
a server of its own: it keeps the server's copy of the script up to date,
3637
names it (its file's URI, or an `untitled:` one), takes offsets into its text
3738
(see `Positions`), and reports the problems found in it. For code typed into
3839
a running interpreter, the server learns the interpreter's state through its
39-
`Environment`. Tools that ask once (e.g. to check a
40-
script) can call `LanguageServerService.diagnose`. For code typed into an
40+
`Environment`. Tools that ask once (e.g. to check a script) can wait for a
41+
session's problems (`diagnostics(timeout)`). For code typed into an
4142
interpreter whose language no plugin serves, a session completes the
4243
interpreter's variables.
4344

45+
Those wanting a server itself, for plain code of a language, get one via
46+
`LanguageServerService.launch`, by LSP language id (e.g. `groovy`, or
47+
`python`): in-process, or a server process (see below), whichever the plugins
48+
offer, analyzing scripts as running in the environment given. (Plugins whose
49+
scripts say where they run, which may change as they are edited, keep a
50+
server per environment: e.g. scripting-appose-python's.)
51+
4452
Servers need know nothing of SciJava scripts. A SciJava script is not quite
4553
plain code of its language: its `#@` parameters define variables before it
4654
runs. `LanguageServerService.scriptParameters` parses them; a `ScriptDocument`
@@ -55,14 +63,14 @@ For servers that are not Java in the SciJava process (e.g. jedi-language-server,
5563
for Python). A `ServerProcessPlugin` says how to start one for an LSP language
5664
id (e.g. `python`) and an environment: the process, speaking LSP over its
5765
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.
66+
Plugins get such a server via `LanguageServerService.launch`, as code-lsp's
67+
own `LanguageServerPlugin` for server processes offers it: started when first
68+
needed, one per environment, restarted if it stops, and stopped when idle. `scijava-code-lsp-jedi` offers jedi-language-server this way.
6169

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

6472
For servers of JVM languages, written in Java (e.g. Groovy's and Jython's):
65-
`ScriptLanguageServer`, a base class keeping the open documents and turning
73+
`AbstractLanguageServer`, a base class keeping the open documents and turning
6674
requests into questions about a document and an offset; `Members`,
6775
completions for Java classes' members by reflection; `Callable`, something to
6876
call as a completion snippet; `Signatures`, rating overloads against the
@@ -77,6 +85,7 @@ javadoc.
7785
| scripting-groovy | ✓ | | ✓ |
7886
| scripting-jython | ✓ | ✓ | ✓ |
7987
| scripting-appose-python | ✓ | ✓ | |
88+
| code-lsp-jedi | | ✓ | |
8089

8190
The toolkit (`org.scijava.code.lsp.jvm`) depends only on
8291
`org.scijava.code.lsp`. The module exports both; its implementation

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,53 @@
3434
import org.scijava.script.ScriptLanguage;
3535

3636
/**
37-
* Offers language servers for scripts of some languages: in-process
38-
* implementations of lsp4j's {@link LanguageServer}, or proxies to server
39-
* processes (see {@link LanguageServerService#launch}).
37+
* A {@link SciJavaPlugin} that can create {@link LanguageServer}s.
4038
* <p>
4139
* Servers need not know SciJava scripts: given the plugin's
4240
* {@link #dialect() dialect}, sessions show them each script as plain code of
4341
* its language, its parameters declared (see
4442
* {@link ScriptDocument}).
4543
* </p>
46-
* <p>
47-
* Each language is served by the highest-priority plugin that supports it. A
48-
* server combining several sources (e.g. an instant in-process analysis and a
49-
* slower, smarter server process) combines them itself.
50-
* </p>
5144
*
5245
* @author Gabriel Selzer
5346
*/
5447
public interface LanguageServerPlugin extends SciJavaPlugin {
5548

56-
/** Whether this serves scripts of the given language. */
49+
/**
50+
* Declares whether {@link LanguageServer}s can be created to satisfy
51+
* scripts of
52+
* {@code groovy}), as {@link LanguageServerService#launch} asks. NB: Not
53+
* one whose servers this launches itself (e.g. a plugin building on
54+
* {@code python} servers serves a language of its own).
55+
*/
56+
boolean supports(String languageId);
57+
58+
/**
59+
* Whether this serves scripts of the given SciJava language, as sessions
60+
* ask (see {@link LanguageServerService#session}).
61+
*/
5762
boolean supports(ScriptLanguage language);
5863

5964
/**
60-
* Creates a server for a session (see {@link LanguageServerService#session}):
61-
* the session initializes it, and shuts it down when closed.
65+
* Creates a server for documents of an LSP language this serves: for a
66+
* session (see {@link LanguageServerService#session}), which initializes
67+
* it, and shuts it down when closed; or for whoever
68+
* {@link LanguageServerService#launch launches} it.
69+
* <p>
70+
* NB: A plugin may know better where its scripts run (e.g. as their text
71+
* says, which may change as they are edited), and analyze them there,
72+
* whatever {@code environment} says.
73+
* </p>
6274
*
63-
* @param environment Where the session's script runs, as far as known: for
75+
* @param languageId The LSP language asked for (one this
76+
* {@link #supports(String) serves}, e.g. {@code groovy}); or null,
77+
* for a session about a script of a language this
78+
* {@link #supports(ScriptLanguage) serves}.
79+
* @param environment Where the scripts run, as far as known: e.g., for
6480
* code typed into a running interpreter, its state (see
6581
* {@link Environment#live()}).
6682
*/
67-
LanguageServer create(Environment environment);
83+
LanguageServer create(String languageId, Environment environment);
6884

6985
/**
7086
* How this plugin's servers see scripts: as plain code of their language,

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

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@
2929

3030
package org.scijava.code.lsp;
3131

32-
import java.util.List;
3332
import java.util.Map;
34-
import java.util.function.Function;
3533

3634
import javax.script.ScriptContext;
3735

38-
import org.eclipse.lsp4j.Diagnostic;
39-
import org.eclipse.lsp4j.TextDocumentItem;
4036
import org.eclipse.lsp4j.services.LanguageServer;
4137
import org.scijava.script.ScriptLanguage;
4238
import org.scijava.service.SciJavaService;
@@ -70,51 +66,24 @@ public interface LanguageServerService extends SciJavaService {
7066
boolean supports(ScriptLanguage language);
7167

7268
/**
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.
69+
* Whether any plugin serves documents of the given LSP language (e.g.
70+
* {@code python}): i.e. whether {@link #launch} can give a server for it.
9471
*/
95-
default LanguageServer launch(final String languageId,
96-
final Environment environment)
97-
{
98-
return launch(languageId, document -> environment);
99-
}
72+
boolean supports(String languageId);
10073

10174
/**
102-
* Finds the problems in a script, waiting for them: for tools that ask
103-
* once, rather than editors. (For a script file, whose servers may depend
104-
* on where it is, use a {@link #session session} with its file set.)
75+
* Gets a new server for an LSP language (e.g. {@code groovy}, or
76+
* {@code python}), from the highest-priority {@link LanguageServerPlugin}
77+
* serving it: in-process, or a proxy to server processes (see
78+
* {@link ServerProcessPlugin}; shared, one per environment), whichever the
79+
* plugins offer. It sees documents as plain code of the language, and
80+
* analyzes them as running in the given environment. The caller
81+
* initializes it, and shuts it down.
10582
*
106-
* @param language The script's language.
107-
* @param text The script.
108-
* @param timeout How long to wait for all servers, in milliseconds; those
109-
* not done by then are left out.
83+
* @param environment Where the scripts run.
84+
* @throws IllegalArgumentException If no plugin serves the language.
11085
*/
111-
default List<Diagnostic> diagnose(final ScriptLanguage language,
112-
final String text, final long timeout)
113-
{
114-
try (ScriptSession session = session(language)) {
115-
return session.update(text).diagnostics(timeout);
116-
}
117-
}
86+
LanguageServer launch(String languageId, Environment environment);
11887

11988
/**
12089
* Gets the variables a script's parameters ({@code #@} lines, and old-style

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
* server via {@link LanguageServerService#launch}: started when first needed,
4141
* one per environment, restarted if it stops, and stopped when idle.
4242
* <p>
43-
* For example, scripting-appose-python offers jedi-language-server, which
44-
* scripting-jython's server uses too.
43+
* For example, code-lsp-jedi offers jedi-language-server, which
44+
* scripting-appose-python's and scripting-jython's servers use.
4545
* </p>
4646
*
4747
* @author Gabriel Selzer

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
import org.eclipse.lsp4j.CompletionItemKind;
4646
import org.eclipse.lsp4j.CompletionList;
4747
import org.scijava.code.lsp.Environment;
48+
import org.scijava.code.lsp.jvm.AbstractLanguageServer;
4849
import org.scijava.code.lsp.jvm.Members;
49-
import org.scijava.code.lsp.jvm.ScriptLanguageServer;
5050

5151
/**
5252
* A language server knowing only the variables of a running interpreter (see
@@ -57,7 +57,7 @@
5757
*
5858
* @author Gabriel Selzer
5959
*/
60-
public class BindingsLanguageServer extends ScriptLanguageServer {
60+
public class BindingsLanguageServer extends AbstractLanguageServer {
6161

6262
/** A trailing {@code variable.prefix} before the caret. */
6363
private static final Pattern MEMBER = Pattern.compile(

0 commit comments

Comments
 (0)