Skip to content

Commit f1dd82f

Browse files
gselzerclaude
andcommitted
code-lsp: show servers scripts as plain code, from the session
A ScriptSession now transforms each script into plain code of its language, in the dialect of the plugin offering its server (LanguageServerPlugin#dialect), and maps positions in the server's answers back. Servers need know nothing of SciJava scripts. TransformingLanguageServer, which did this inside some servers, is gone, and so is ScriptDocument's unused environment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 28d6494 commit f1dd82f

10 files changed

Lines changed: 214 additions & 470 deletions

File tree

‎code-lsp/README.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ scripts.
1313

1414
```
1515
client (e.g. Script Editor)
16-
│ ScriptSession: a script's text, and offsets into it
16+
│ ScriptSession: a script's text, and offsets into it;
17+
│ shows its server the script as plain code (ScriptDocument)
1718
▼
1819
LanguageServerService ──► the LanguageServerPlugin serving the language
19-
│ a server of the session's own
20+
│ a server of the session's own, and its ScriptDialect
2021
├──► in-process servers (e.g. Groovy, Jython), built on ScriptLanguageServer
2122
└──► server processes (e.g. jedi-language-server), via LspServerSpec
22-
seeing the script as plain code (TransformingLanguageServer)
2323
```
2424

2525
It does four things, each in a package of its own.
@@ -34,7 +34,8 @@ jedi's) combines them itself.
3434
Clients ask about a script through a `ScriptSession` from the service, with
3535
a server of its own: it keeps the server's copy of the script up to date,
3636
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
37+
(see `Positions`), and reports the problems found in it. Servers need know
38+
nothing of SciJava scripts: the session shows them plain code (see 3). For code typed into
3839
a running interpreter, the server learns the interpreter's state through its
3940
`Environment`. Tools that ask once (e.g. to check a
4041
script) can call `LanguageServerService.diagnose`. For code typed into an
@@ -55,10 +56,10 @@ environment, restarted if it stops, and stopped when idle.
5556
A SciJava script is not quite plain code of its language: its `#@`
5657
parameters define variables before it runs. `LanguageServerService.scriptParameters`
5758
parses them; a `ScriptDocument` is the script as plain code, with them
58-
declared with their run-time types, as its language's `ScriptDialect` says.
59-
`TransformingLanguageServer` shows servers that know the language but not
60-
SciJava (e.g. jedi) this text, and maps positions in their answers back.
61-
In-process servers use `ScriptDocument` too, to know the parameters.
59+
declared with their run-time types, as a `ScriptDialect` says. Each
60+
`LanguageServerPlugin` names the dialect its servers read; a session shows its
61+
server the script in that dialect, and maps positions in the server's answers
62+
back.
6263

6364
## 4. A toolkit for in-process servers (`org.scijava.code.lsp.jvm`)
6465

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package org.scijava.code.lsp;
3131

3232
import org.eclipse.lsp4j.services.LanguageServer;
33+
import org.scijava.code.lsp.script.ScriptDialect;
3334
import org.scijava.plugin.SciJavaPlugin;
3435
import org.scijava.script.ScriptLanguage;
3536

@@ -38,6 +39,12 @@
3839
* implementations of lsp4j's {@link LanguageServer}, or proxies to server
3940
* processes (see {@link LanguageServerService#launch}).
4041
* <p>
42+
* Servers need not know SciJava scripts: given the plugin's
43+
* {@link #dialect() dialect}, sessions show them each script as plain code of
44+
* its language, its parameters declared (see
45+
* {@link org.scijava.code.lsp.script.ScriptDocument}).
46+
* </p>
47+
* <p>
4148
* Each language is served by the highest-priority plugin that supports it. A
4249
* server combining several sources (e.g. an instant in-process analysis and a
4350
* slower, smarter server process) combines them itself.
@@ -59,4 +66,12 @@ public interface LanguageServerPlugin extends SciJavaPlugin {
5966
* {@link Environment#live()}).
6067
*/
6168
LanguageServer create(Environment environment);
69+
70+
/**
71+
* How this plugin's servers see scripts: as plain code of their language,
72+
* written in this dialect; or, if null (the default), as written.
73+
*/
74+
default ScriptDialect dialect() {
75+
return null;
76+
}
6277
}

‎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
@@ -99,7 +99,7 @@ default List<Diagnostic> diagnose(final ScriptLanguage language,
9999
* Gets the variables a script's parameters ({@code #@} lines, and old-style
100100
* {@code // @} lines in its header) define before it runs, by name, with
101101
* their Java types ({@code Object} if unresolvable); in declaration order.
102-
* For servers to know them (see
102+
* Sessions declare them for their servers (see
103103
* {@link org.scijava.code.lsp.script.ScriptDocument}). Half-typed declarations
104104
* are expected, and parsed quietly.
105105
*/

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

Lines changed: 116 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
package org.scijava.code.lsp;
3131

3232
import java.io.File;
33+
import java.util.ArrayList;
3334
import java.util.Collections;
3435
import java.util.List;
3536
import java.util.concurrent.CompletableFuture;
3637
import java.util.concurrent.TimeUnit;
3738
import java.util.concurrent.atomic.AtomicInteger;
3839
import java.util.function.Consumer;
40+
import java.util.function.Function;
3941
import java.util.function.Supplier;
4042

4143
import org.eclipse.lsp4j.CompletionItem;
@@ -50,19 +52,25 @@
5052
import org.eclipse.lsp4j.InitializeParams;
5153
import org.eclipse.lsp4j.InitializeResult;
5254
import org.eclipse.lsp4j.InitializedParams;
55+
import org.eclipse.lsp4j.InsertReplaceEdit;
5356
import org.eclipse.lsp4j.MessageActionItem;
5457
import org.eclipse.lsp4j.MessageParams;
58+
import org.eclipse.lsp4j.Position;
5559
import org.eclipse.lsp4j.PublishDiagnosticsParams;
60+
import org.eclipse.lsp4j.Range;
5661
import org.eclipse.lsp4j.ShowMessageRequestParams;
5762
import org.eclipse.lsp4j.SignatureHelp;
5863
import org.eclipse.lsp4j.SignatureHelpParams;
5964
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
6065
import org.eclipse.lsp4j.TextDocumentIdentifier;
6166
import org.eclipse.lsp4j.TextDocumentItem;
67+
import org.eclipse.lsp4j.TextEdit;
6268
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
69+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
6370
import org.eclipse.lsp4j.services.LanguageClient;
6471
import org.eclipse.lsp4j.services.LanguageClientAware;
6572
import org.eclipse.lsp4j.services.LanguageServer;
73+
import org.scijava.code.lsp.script.ScriptDocument;
6674
import org.scijava.log.Logger;
6775
import org.scijava.script.ScriptLanguage;
6876

@@ -80,6 +88,14 @@
8088
* {@link Positions}). Answers are lsp4j's own types.
8189
* </p>
8290
* <p>
91+
* The server need not know SciJava scripts: given a transformation (e.g. by
92+
* the {@link LanguageServerPlugin#dialect() dialect} of the plugin offering
93+
* the server), the session shows the server each script as plain code of its
94+
* language (see {@link ScriptDocument}), and maps positions both ways.
95+
* Whatever the server says about the lines the transformation added is left
96+
* out.
97+
* </p>
98+
* <p>
8399
* A session owns its server: it is the server's client, initializes it, and
84100
* shuts it down when closed. Get one from the
85101
* {@link LanguageServerService#session service}, which makes it a server of
@@ -97,13 +113,16 @@ public class ScriptSession implements AutoCloseable {
97113

98114
private final LanguageServer server;
99115
private final ScriptLanguage language;
116+
private final Function<String, ScriptDocument> transform;
100117
private final String untitled;
101118
private volatile Supplier<File> file = () -> null;
102119
private volatile Logger log;
103120
private volatile Consumer<List<Diagnostic>> diagnosticsListener;
104121

105122
private String uri;
106123
private String text;
124+
/** The text, as the server sees it. */
125+
private ScriptDocument doc;
107126
private int version;
108127
private List<Diagnostic> diagnostics = Collections.emptyList();
109128
private CompletableFuture<List<Diagnostic>> nextDiagnostics =
@@ -112,12 +131,16 @@ public class ScriptSession implements AutoCloseable {
112131
/**
113132
* @param server The server, which the session owns.
114133
* @param language The script's language, or null if unknown.
134+
* @param transform Makes scripts what the server sees; or null, to show
135+
* them as they are.
115136
*/
116137
private ScriptSession(final LanguageServer server,
117-
final ScriptLanguage language)
138+
final ScriptLanguage language,
139+
final Function<String, ScriptDocument> transform)
118140
{
119141
this.server = server;
120142
this.language = language;
143+
this.transform = transform == null ? ScriptDocument::of : transform;
121144
final List<String> extensions = language == null ? Collections
122145
.emptyList() : language.getExtensions();
123146
untitled = "untitled:/script-" + UNTITLED.incrementAndGet() + (extensions
@@ -136,7 +159,23 @@ private ScriptSession(final LanguageServer server,
136159
public static ScriptSession owning(final LanguageServer server,
137160
final ScriptLanguage language)
138161
{
139-
return new ScriptSession(server, language).connect();
162+
return owning(server, language, null);
163+
}
164+
165+
/**
166+
* As {@link #owning(LanguageServer, ScriptLanguage)}, showing the server
167+
* each script transformed (e.g. into plain code of its language).
168+
*
169+
* @param server The server, which the session owns.
170+
* @param language The script's language, or null if unknown.
171+
* @param transform Makes scripts what the server sees; or null, to show
172+
* them as they are.
173+
*/
174+
public static ScriptSession owning(final LanguageServer server,
175+
final ScriptLanguage language,
176+
final Function<String, ScriptDocument> transform)
177+
{
178+
return new ScriptSession(server, language, transform).connect();
140179
}
141180

142181
/**
@@ -197,17 +236,19 @@ public synchronized ScriptSession update(final String newText) {
197236
uri = current;
198237
version = 1;
199238
text = newText;
239+
doc = transform.apply(text);
200240
newVersion();
201241
server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(
202-
new TextDocumentItem(uri, languageId(), version, text)));
242+
new TextDocumentItem(uri, languageId(), version, doc.text())));
203243
}
204244
else if (!newText.equals(text)) {
205245
text = newText;
246+
doc = transform.apply(text);
206247
version++;
207248
newVersion();
208249
server.getTextDocumentService().didChange(new DidChangeTextDocumentParams(
209250
new VersionedTextDocumentIdentifier(uri, version), Collections
210-
.singletonList(new TextDocumentContentChangeEvent(text))));
251+
.singletonList(new TextDocumentContentChangeEvent(doc.text()))));
211252
}
212253
return this;
213254
}
@@ -219,10 +260,11 @@ public synchronized CompletableFuture<CompletionList> completion(
219260
final String script, final int offset)
220261
{
221262
update(script);
263+
final ScriptDocument d = doc;
222264
return server.getTextDocumentService().completion(new CompletionParams(
223-
identifier(), Positions.position(script, offset))).thenApply(
224-
answer -> answer == null ? new CompletionList() : answer.isRight()
225-
? answer.getRight() : new CompletionList(false, answer.getLeft()));
265+
identifier(), position(offset))).thenApply(answer -> answer == null
266+
? new CompletionList() : back(d, answer.isRight() ? answer.getRight()
267+
: new CompletionList(false, answer.getLeft())));
226268
}
227269

228270
/** A completion, with what it takes long to know (e.g. documentation). */
@@ -236,17 +278,23 @@ public synchronized CompletableFuture<SignatureHelp> signatureHelp(
236278
{
237279
update(script);
238280
return server.getTextDocumentService().signatureHelp(
239-
new SignatureHelpParams(identifier(), Positions.position(script,
240-
offset)));
281+
new SignatureHelpParams(identifier(), position(offset)));
241282
}
242283

243284
/** What is known about the code at an offset (null if nothing). */
244285
public synchronized CompletableFuture<Hover> hover(final String script,
245286
final int offset)
246287
{
247288
update(script);
289+
final ScriptDocument d = doc;
248290
return server.getTextDocumentService().hover(new HoverParams(identifier(),
249-
Positions.position(script, offset)));
291+
position(offset))).thenApply(h -> {
292+
if (h == null || h.getRange() == null) return h;
293+
final Hover out = new Hover();
294+
out.setContents(h.getContents());
295+
out.setRange(d.toOriginal(h.getRange()));
296+
return out;
297+
});
250298
}
251299

252300
// -- Problems --
@@ -301,9 +349,15 @@ private void closeDocument() {
301349
new TextDocumentIdentifier(uri)));
302350
uri = null;
303351
text = null;
352+
doc = null;
304353
diagnostics = Collections.emptyList();
305354
}
306355

356+
/** Where an offset into the script is, in what the server sees. */
357+
private Position position(final int offset) {
358+
return Positions.position(doc.text(), doc.toTransformed(offset));
359+
}
360+
307361
private TextDocumentIdentifier identifier() {
308362
return new TextDocumentIdentifier(uri);
309363
}
@@ -322,18 +376,68 @@ private void newVersion() {
322376
if (nextDiagnostics.isDone()) nextDiagnostics = new CompletableFuture<>();
323377
}
324378

379+
/** Maps completions' edits back, leaving out those in the added lines. */
380+
private static CompletionList back(final ScriptDocument doc,
381+
final CompletionList list)
382+
{
383+
final List<CompletionItem> items = new ArrayList<>();
384+
for (final CompletionItem item : list.getItems()) {
385+
if (item.getTextEdit() != null) {
386+
if (item.getTextEdit().isLeft()) {
387+
final TextEdit e = item.getTextEdit().getLeft();
388+
final Range r = doc.toOriginal(e.getRange());
389+
if (r == null) continue;
390+
item.setTextEdit(Either.forLeft(new TextEdit(r, e.getNewText())));
391+
}
392+
else {
393+
final InsertReplaceEdit e = item.getTextEdit().getRight();
394+
final Range insert = doc.toOriginal(e.getInsert());
395+
final Range replace = doc.toOriginal(e.getReplace());
396+
if (insert == null || replace == null) continue;
397+
item.setTextEdit(Either.forRight(new InsertReplaceEdit(e
398+
.getNewText(), insert, replace)));
399+
}
400+
}
401+
if (item.getAdditionalTextEdits() != null) {
402+
final List<TextEdit> edits = new ArrayList<>();
403+
for (final TextEdit e : item.getAdditionalTextEdits()) {
404+
final Range r = doc.toOriginal(e.getRange());
405+
if (r != null) edits.add(new TextEdit(r, e.getNewText()));
406+
}
407+
item.setAdditionalTextEdits(edits);
408+
}
409+
items.add(item);
410+
}
411+
return new CompletionList(list.isIncomplete(), items);
412+
}
413+
414+
/** Maps problems back, leaving out those in the added lines. */
415+
private static List<Diagnostic> back(final ScriptDocument doc,
416+
final List<Diagnostic> diagnostics)
417+
{
418+
final List<Diagnostic> out = new ArrayList<>();
419+
for (final Diagnostic d : diagnostics) {
420+
final Range r = doc.toOriginal(d.getRange());
421+
if (r == null) continue;
422+
d.setRange(r);
423+
out.add(d);
424+
}
425+
return out;
426+
}
427+
325428
/** Receives the server's messages about the script. */
326429
private final class Client implements LanguageClient {
327430

328431
@Override
329432
public void publishDiagnostics(final PublishDiagnosticsParams p) {
330-
final List<Diagnostic> found = p.getDiagnostics() == null ? Collections
331-
.emptyList() : p.getDiagnostics();
433+
final List<Diagnostic> found;
332434
final CompletableFuture<List<Diagnostic>> next;
333435
synchronized (ScriptSession.this) {
334436
if (!p.getUri().equals(uri)) return;
335437
// NB: Those of an earlier version are out of date.
336438
if (p.getVersion() != null && p.getVersion() < version) return;
439+
found = p.getDiagnostics() == null ? Collections.emptyList() : back(
440+
doc, p.getDiagnostics());
337441
diagnostics = found;
338442
next = nextDiagnostics;
339443
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.scijava.code.lsp.LanguageServerPlugin;
4040
import org.scijava.code.lsp.LanguageServerService;
4141
import org.scijava.code.lsp.ScriptSession;
42+
import org.scijava.code.lsp.script.ScriptDialect;
43+
import org.scijava.code.lsp.script.ScriptDocument;
4244
import org.scijava.log.LogService;
4345
import org.scijava.plugin.Parameter;
4446
import org.scijava.plugin.Plugin;
@@ -51,8 +53,8 @@
5153
* Default implementation of {@link LanguageServerService}.
5254
* <p>
5355
* Each session gets a server of its own, from the highest-priority plugin
54-
* serving its language. (Server processes behind them are shared; see
55-
* {@link #launch}.)
56+
* 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}.)
5658
* </p>
5759
*
5860
* @author Gabriel Selzer
@@ -93,9 +95,11 @@ public ScriptSession session(final ScriptLanguage language,
9395
// ApposePythonLanguageServer).
9496
final LanguageServerPlugin plugin = plugin(language);
9597
LanguageServer server = null;
98+
ScriptDialect dialect = null;
9699
if (plugin != null) {
97100
try {
98101
server = plugin.create(Environment.NONE.withLive(live));
102+
dialect = plugin.dialect();
99103
}
100104
catch (final RuntimeException | LinkageError exc) {
101105
if (log != null) log.warn("Could not create a language server: " +
@@ -105,7 +109,10 @@ public ScriptSession session(final ScriptLanguage language,
105109
// NB: Without a server, the interpreter's variables (if any) are
106110
// completed.
107111
if (server == null) server = new BindingsLanguageServer(live);
108-
return ScriptSession.owning(server, language).setLogger(log);
112+
final ScriptDialect d = dialect;
113+
return ScriptSession.owning(server, language, d == null ? null
114+
: text -> ScriptDocument.of(text, scriptParameters(text), d)).setLogger(
115+
log);
109116
}
110117

111118
@Override

0 commit comments

Comments
 (0)