Skip to content

Commit 6e7f65f

Browse files
gselzerclaude
andcommitted
code-lsp: serve script languages by their names, in lower case
A LanguageServerPlugin now only declares the LSP languages it serves. Sessions ask for the one named as their script's language, in lower case (e.g. groovy for Groovy), so supports(ScriptLanguage) is gone, and create's languageId is never null. ScriptSession now opens documents under that lower-case id too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bea114f commit 6e7f65f

6 files changed

Lines changed: 33 additions & 44 deletions

File tree

‎code-lsp/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ It does three things.
2828

2929
The core. `LanguageServerPlugin`s offer lsp4j `LanguageServer`s for
3030
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
33-
jedi's) combines them itself.
31+
languages of those names, in lower case (e.g. `Groovy`); each language is
32+
served by the highest-priority plugin supporting it. A server combining
33+
several sources (e.g. Jython's own analysis and jedi's) combines them itself.
3434

3535
Clients ask about a script through a `ScriptSession` from the service, with
3636
a server of its own: it keeps the server's copy of the script up to date,

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import org.eclipse.lsp4j.services.LanguageServer;
3333
import org.scijava.plugin.SciJavaPlugin;
34-
import org.scijava.script.ScriptLanguage;
3534

3635
/**
3736
* A {@link SciJavaPlugin} that can create {@link LanguageServer}s.
@@ -48,19 +47,15 @@ public interface LanguageServerPlugin extends SciJavaPlugin {
4847

4948
/**
5049
* 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).
50+
* scripts of the given LSP language (e.g. {@code groovy}), as
51+
* {@link LanguageServerService#launch} asks; and so scripts of the SciJava
52+
* language of that name, in lower case (e.g. {@code Groovy}), as sessions
53+
* ask (see {@link LanguageServerService#session}). NB: Not one whose
54+
* servers this launches itself (e.g. a plugin building on {@code python}
55+
* servers serves a language of its own).
5556
*/
5657
boolean supports(String languageId);
5758

58-
/**
59-
* Whether this serves scripts of the given SciJava language, as sessions
60-
* ask (see {@link LanguageServerService#session}).
61-
*/
62-
boolean supports(ScriptLanguage language);
63-
6459
/**
6560
* Creates a server for documents of an LSP language this serves: for a
6661
* session (see {@link LanguageServerService#session}), which initializes
@@ -72,10 +67,8 @@ public interface LanguageServerPlugin extends SciJavaPlugin {
7267
* whatever {@code environment} says.
7368
* </p>
7469
*
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}.
70+
* @param languageId The LSP language asked for, one this
71+
* {@link #supports(String) serves} (e.g. {@code groovy}).
7972
* @param environment Where the scripts run, as far as known: e.g., for
8073
* code typed into a running interpreter, its state (see
8174
* {@link Environment#live()}).

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Collections;
3535
import java.util.List;
36+
import java.util.Locale;
3637
import java.util.concurrent.CompletableFuture;
3738
import java.util.concurrent.TimeUnit;
3839
import java.util.concurrent.atomic.AtomicInteger;
@@ -367,8 +368,13 @@ private String currentUri() {
367368
return f == null ? untitled : f.getAbsoluteFile().toURI().toString();
368369
}
369370

371+
/**
372+
* The LSP language of the script: its language's name, in lower case (see
373+
* {@link LanguageServerPlugin#supports(String)}).
374+
*/
370375
private String languageId() {
371-
return language == null ? "plaintext" : language.getLanguageName();
376+
return language == null ? "plaintext" : language.getLanguageName()
377+
.toLowerCase(Locale.ROOT);
372378
}
373379

374380
/** Problems reported from now on are those of a new version. */

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

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

3232
import java.util.List;
33+
import java.util.Locale;
3334
import java.util.Map;
3435
import java.util.function.Predicate;
3536

@@ -98,8 +99,8 @@ public ScriptSession session(final ScriptLanguage language,
9899
ScriptDialect dialect = null;
99100
if (plugin != null) {
100101
try {
101-
// NB: Asked for a script language, not an LSP one.
102-
server = plugin.create(null, Environment.NONE.withLive(live));
102+
server = plugin.create(languageId(language), Environment.NONE
103+
.withLive(live));
103104
dialect = plugin.dialect();
104105
}
105106
catch (final RuntimeException | LinkageError exc) {
@@ -166,7 +167,15 @@ public synchronized void dispose() {
166167
*/
167168
private LanguageServerPlugin plugin(final ScriptLanguage language) {
168169
if (language == null) return null;
169-
return plugin(p -> p.supports(language));
170+
return plugin(languageId(language));
171+
}
172+
173+
/**
174+
* The LSP language of a SciJava language's scripts: its name, in lower case
175+
* (e.g. {@code groovy}).
176+
*/
177+
private static String languageId(final ScriptLanguage language) {
178+
return language.getLanguageName().toLowerCase(Locale.ROOT);
170179
}
171180

172181
/**

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.scijava.plugin.Parameter;
4545
import org.scijava.plugin.Plugin;
4646
import org.scijava.plugin.PluginService;
47-
import org.scijava.script.ScriptLanguage;
4847

4948
/**
5049
* Serves each LSP language a {@link ServerProcessPlugin} offers servers for
@@ -86,16 +85,6 @@ public boolean supports(final String languageId) {
8685
return plugin(languageId) != null;
8786
}
8887

89-
/**
90-
* Serves no SciJava language: server processes see scripts as written, so
91-
* they serve sessions only via plugins dedicated to a language (e.g.
92-
* declaring its scripts' parameters).
93-
*/
94-
@Override
95-
public boolean supports(final ScriptLanguage language) {
96-
return false;
97-
}
98-
9988
@Override
10089
public LanguageServer create(final String languageId,
10190
final Environment environment)

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public void testHighestPriorityServes() throws Exception {
148148
assertEquals(Arrays.asList("max", "mass"), labels(list));
149149
assertEquals(1, SERVERS.size());
150150
assertEquals("high", SERVERS.get(0).name);
151-
// For the script language: no LSP language asked for.
152-
assertNull(SERVERS.get(0).languageId);
151+
// For the script language's LSP language: its name, in lower case.
152+
assertEquals("fake", SERVERS.get(0).languageId);
153153
}
154154
// Each session gets a server of its own.
155155
try (ScriptSession other = service.session(fake)) {
@@ -430,7 +430,7 @@ public void logMessage(final MessageParams m) {}
430430

431431
// -- A fake language, and its servers --
432432

433-
@Plugin(type = ScriptLanguage.class)
433+
@Plugin(type = ScriptLanguage.class, name = "Fake")
434434
public static class FakeLanguage extends AbstractScriptLanguage {
435435

436436
@Override
@@ -473,10 +473,6 @@ public boolean supports(final String languageId) {
473473
return "fake".equals(languageId);
474474
}
475475

476-
@Override
477-
public boolean supports(final ScriptLanguage language) {
478-
return language instanceof FakeLanguage;
479-
}
480476

481477
@Override
482478
public LanguageServer create(final String languageId,
@@ -522,10 +518,6 @@ public boolean supports(final String languageId) {
522518
return "fake".equals(languageId);
523519
}
524520

525-
@Override
526-
public boolean supports(final ScriptLanguage language) {
527-
return language instanceof FakeLanguage;
528-
}
529521

530522
@Override
531523
public LanguageServer create(final String languageId,

0 commit comments

Comments
 (0)