Skip to content

Commit 24f723b

Browse files
gselzerclaude
andcommitted
code-lsp: a language server for SciJava scripts
SciJavaLanguageServer serves the completions, signature help, hover and diagnostics of the context's code completers over the Language Server Protocol, for editors outside SciJava (e.g. VS Code); Main runs it over standard input and output, keeping SciJava's output off the protocol's stream. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 88e4c37 commit 24f723b

5 files changed

Lines changed: 933 additions & 2 deletions

File tree

‎code-lsp/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SciJava Code LSP
2+
3+
A bridge between SciJava code intelligence (`scijava-code-api`) and the
4+
[Language Server Protocol](https://microsoft.github.io/language-server-protocol/),
5+
in both directions.
6+
7+
## Using language servers
8+
9+
`LspClient` asks a language server about SciJava scripts, in the terms of
10+
`scijava-code-api`: completions, signature help, hover and diagnostics, at
11+
positions of the script as written. The server sees the script as plain code
12+
of its language (a `ScriptDocument`: its `#@` parameters declared with their
13+
run-time types), opened, changed and closed as the script is edited.
14+
Answers are reused while the same question is asked again, and pending
15+
questions are cancelled (`$/cancelRequest`) once superseded.
16+
17+
`LspServers` starts servers in the background (one per environment scripts
18+
run in), restarts them if they stop, and stops them when idle. `LspServerSpec`
19+
says how to start one; `LspServerPlugin`s offer them per language, and
20+
`LspService` shares them between the completers of a context. For example,
21+
`scripting-appose-python` offers jedi-language-server, which both its own
22+
completer and Jython's use.
23+
24+
## Being a language server
25+
26+
`org.scijava.code.lsp.server.Main` runs a language server for SciJava
27+
scripts over standard input and output: completions, signature help, hover
28+
and diagnostics from the code completers of the SciJava context, for every
29+
script language they support (by file extension), with SciJava's own
30+
understanding of scripts. Editors outside SciJava can use it, e.g. VS Code
31+
via a generic language client extension, configured to run:
32+
33+
```
34+
java -cp "<Fiji>/jars/*;<Fiji>/plugins/*" org.scijava.code.lsp.server.Main
35+
```
36+
37+
(with `:` instead of `;` on Linux and macOS) for the file types of interest
38+
(e.g. `*.py` for Jython, `*.groovy`).

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private static List<CompletionItem> items(
554554
* Converts an offset to an LSP position: a line, and a character in that
555555
* line, counted in UTF-16 code units (as Java does).
556556
*/
557-
static Position position(final String text, final int offset) {
557+
public static Position position(final String text, final int offset) {
558558
int line = 0, start = 0;
559559
for (int i = 0; i < offset; i++) {
560560
if (text.charAt(i) == '\n') {
@@ -566,7 +566,7 @@ static Position position(final String text, final int offset) {
566566
}
567567

568568
/** Converts an LSP position to an offset (clamped to the text). */
569-
static int offset(final String text, final Position position) {
569+
public static int offset(final String text, final Position position) {
570570
int start = 0;
571571
for (int line = 0; line < position.getLine(); line++) {
572572
final int nl = text.indexOf('\n', start);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*-
2+
* #%L
3+
* Language Server Protocol bridge for SciJava code intelligence.
4+
* %%
5+
* Copyright (C) 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.code.lsp.server;
31+
32+
import java.io.InputStream;
33+
import java.io.PrintStream;
34+
35+
import org.eclipse.lsp4j.jsonrpc.Launcher;
36+
import org.eclipse.lsp4j.launch.LSPLauncher;
37+
import org.eclipse.lsp4j.services.LanguageClient;
38+
import org.scijava.Context;
39+
40+
/**
41+
* Runs the {@link SciJavaLanguageServer} over standard input and output, for
42+
* an editor to start, e.g. with Fiji's classpath:
43+
*
44+
* <pre>
45+
* java -cp "Fiji/jars/*;Fiji/plugins/*" org.scijava.code.lsp.server.Main
46+
* </pre>
47+
*
48+
* @author Gabriel Selzer
49+
*/
50+
public final class Main {
51+
52+
private Main() {
53+
// NB: Prevent instantiation.
54+
}
55+
56+
public static void main(final String[] args) throws Exception {
57+
// NB: Standard output is the protocol's: whatever else would be printed
58+
// there (e.g. SciJava's log) goes to standard error.
59+
final InputStream in = System.in;
60+
final PrintStream out = System.out;
61+
System.setOut(System.err);
62+
63+
final Context context = new Context();
64+
final SciJavaLanguageServer server = new SciJavaLanguageServer(context);
65+
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
66+
server, in, out);
67+
server.connect(launcher.getRemoteProxy());
68+
server.setOnExit(() -> {
69+
context.dispose();
70+
System.exit(0);
71+
});
72+
// Serve until the editor closes the connection.
73+
launcher.startListening().get();
74+
context.dispose();
75+
System.exit(0);
76+
}
77+
}

0 commit comments

Comments
 (0)