Skip to content

Commit 199b19c

Browse files
gselzerclaude
andcommitted
code-lsp: serve plaintext with the interpreter's variables, as a plugin
BindingsLanguageServer is offered for plaintext by BindingsServerPlugin, both back in impl. So a session always asks the service for its servers: of its dialect's LSP language if served, and otherwise (or if that server cannot be created) of plaintext, as its script is then shown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1860a28 commit 199b19c

4 files changed

Lines changed: 101 additions & 22 deletions

File tree

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public final class ScriptSession implements AutoCloseable {
102102

103103
private static final AtomicInteger UNTITLED = new AtomicInteger();
104104

105+
/**
106+
* The LSP language of scripts as they are (see
107+
* {@link ScriptDocument#of(String)}).
108+
*/
109+
private static final String PLAINTEXT = "plaintext";
110+
105111
/** How long to wait for a server to initialize, in milliseconds. */
106112
private static final long INITIALIZE_TIMEOUT = 10_000;
107113

@@ -326,33 +332,45 @@ public void close() {
326332

327333
/**
328334
* Gets servers for scripts of a language: of the LSP language its dialect
329-
* says; or, without one (or if one cannot be created), servers completing
330-
* the interpreter's variables (if any).
335+
* says, if served; otherwise (or if one cannot be created), of
336+
* {@code plaintext}, as the script is then shown (see
337+
* {@link ScriptDocument#of(String)}).
331338
*/
332339
private static Function<Environment, LanguageServer> launcher(
333340
final LanguageServerService servers, final ScriptLanguage language,
334341
final Optional<ScriptContext> live)
335342
{
336343
final LogService log = servers.context().getService(LogService.class);
337-
final Function<Environment, Environment> typedInto = env -> live.map(
338-
env::withLive).orElse(env);
344+
final String languageId = servedLanguage(servers, language);
345+
return env -> {
346+
final Environment typedInto = live.map(env::withLive).orElse(env);
347+
try {
348+
return servers.launch(languageId, typedInto);
349+
}
350+
catch (final RuntimeException | LinkageError exc) {
351+
if (PLAINTEXT.equals(languageId)) throw exc;
352+
if (log != null) log.warn("Could not create a language server for " +
353+
languageId, exc);
354+
return servers.launch(PLAINTEXT, typedInto);
355+
}
356+
};
357+
}
358+
359+
/**
360+
* The LSP language of a script language's dialect, if a plugin serves it;
361+
* or {@code plaintext}.
362+
*/
363+
private static String servedLanguage(final LanguageServerService servers,
364+
final ScriptLanguage language)
365+
{
339366
try {
340367
final String languageId = servers.dialect(language).languageId();
341-
if (servers.supports(languageId)) return env -> {
342-
try {
343-
return servers.launch(languageId, typedInto.apply(env));
344-
}
345-
catch (final RuntimeException | LinkageError exc) {
346-
if (log != null) log.warn("Could not create a language server for " +
347-
languageId, exc);
348-
return new BindingsLanguageServer(typedInto.apply(env));
349-
}
350-
};
368+
if (servers.supports(languageId)) return languageId;
351369
}
352370
catch (final IllegalArgumentException exc) {
353371
// NB: The language has no dialect.
354372
}
355-
return env -> new BindingsLanguageServer(typedInto.apply(env));
373+
return PLAINTEXT;
356374
}
357375

358376
/**

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

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

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

3232
import java.util.ArrayList;
3333
import java.util.Comparator;
@@ -45,15 +45,15 @@
4545
import org.eclipse.lsp4j.CompletionItem;
4646
import org.eclipse.lsp4j.CompletionItemKind;
4747
import org.eclipse.lsp4j.CompletionList;
48+
import org.scijava.code.lsp.Environment;
4849
import org.scijava.code.lsp.jvm.AbstractLanguageServer;
4950
import org.scijava.code.lsp.jvm.Members;
5051

5152
/**
5253
* A language server knowing only the variables of a running interpreter (see
5354
* {@link Environment#live()}): their names, and the members of their values'
54-
* classes. For code typed into an interpreter whose language has no servers
55-
* of its own (see
56-
* {@link org.scijava.code.lsp.LanguageServerService#supports}).
55+
* classes. It serves {@code plaintext} (see {@link BindingsServerPlugin}), as
56+
* scripts become whose languages have no servers of their own.
5757
*
5858
* @author Gabriel Selzer
5959
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.impl;
31+
32+
import org.eclipse.lsp4j.services.LanguageServer;
33+
import org.scijava.code.lsp.Environment;
34+
import org.scijava.code.lsp.LanguageServerPlugin;
35+
import org.scijava.plugin.Plugin;
36+
37+
/**
38+
* Offers {@link BindingsLanguageServer}s for {@code plaintext}: scripts whose
39+
* languages have no servers of their own, of which they then complete the
40+
* interpreter's variables (if any).
41+
*
42+
* @author Gabriel Selzer
43+
*/
44+
@Plugin(type = LanguageServerPlugin.class, name = "Bindings")
45+
public class BindingsServerPlugin implements LanguageServerPlugin {
46+
47+
@Override
48+
public boolean supports(final String languageId) {
49+
return "plaintext".equals(languageId);
50+
}
51+
52+
@Override
53+
public LanguageServer create(final String languageId,
54+
final Environment environment)
55+
{
56+
return new BindingsLanguageServer(environment);
57+
}
58+
}

‎code-lsp/src/test/java/org/scijava/code/lsp/LanguageServersTest.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101

102102
/**
103103
* Tests {@link LanguageServerService} and {@link ScriptSession}, with fake
104-
* in-process servers; and {@link BindingsLanguageServer}.
104+
* in-process servers; and the {@code plaintext} server, of an interpreter's
105+
* variables.
105106
*
106107
* @author Gabriel Selzer
107108
*/
@@ -341,7 +342,7 @@ public void testBindings() throws Exception {
341342
final SimpleScriptContext live = new SimpleScriptContext();
342343
live.setAttribute("text", "hello", ScriptContext.ENGINE_SCOPE);
343344
live.setAttribute("count", 3, ScriptContext.ENGINE_SCOPE);
344-
final BindingsLanguageServer server = new BindingsLanguageServer(
345+
final LanguageServer server = service.launch("plaintext",
345346
new Environment().withLive(live));
346347
assertEquals(Collections.singletonList("count"), labels(complete(server,
347348
"x = co")));
@@ -369,7 +370,7 @@ public ScriptEngine getScriptEngine() {
369370
assertEquals(Collections.singletonList("count"), labels(session
370371
.completion("x = co", 6).get(10, TimeUnit.SECONDS)));
371372
}
372-
// Without an interpreter: nothing, and no server created.
373+
// Without an interpreter: nothing, and none of the fake servers.
373374
try (ScriptSession session = new ScriptSession(service, unserved)) {
374375
assertTrue(session.completion("x = co", 6).get(10, TimeUnit.SECONDS)
375376
.getItems().isEmpty());
@@ -384,6 +385,8 @@ public void testSupports() {
384385
// By LSP language.
385386
assertTrue(service.supports("fake"));
386387
assertFalse(service.supports("cobol"));
388+
// Scripts as they are: their interpreters' variables.
389+
assertTrue(service.supports("plaintext"));
387390
}
388391

389392
@Test

0 commit comments

Comments
 (0)