Skip to content

Commit 160ea7e

Browse files
gselzerclaude
andcommitted
code-lsp: keep import edits inserted before a header
A completion's additional edits are mapped back to the script, and those in the dialect's header dropped. But when a script has no imports yet, Jython's server inserts a class's import at the very top, in the header, so accepting the class lost its import. As a HACK, an insertion at the very start of the text now goes at the start of the script. NB: That places imports above a script's parameter block, where SciJava scripts conventionally put them after it. Ideally new imports would go right after the parameters at the top. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f87b452 commit 160ea7e

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎scijava-code-lsp-jython/src/test/java/org/scijava/code/lsp/jython/JythonLanguageServerTest.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@
4040
import javax.script.ScriptEngine;
4141

4242
import org.eclipse.lsp4j.CompletionItem;
43+
import org.eclipse.lsp4j.CompletionItemKind;
4344
import org.eclipse.lsp4j.CompletionList;
45+
import org.eclipse.lsp4j.Position;
46+
import org.eclipse.lsp4j.Range;
4447
import org.eclipse.lsp4j.SignatureHelp;
48+
import org.eclipse.lsp4j.TextEdit;
4549
import org.junit.jupiter.api.AfterAll;
4650
import org.junit.jupiter.api.BeforeAll;
4751
import org.junit.jupiter.api.Test;
@@ -72,6 +76,17 @@ public ScriptEngine getScriptEngine() {
7276
}
7377
};
7478

79+
/** A language whose dialect prepends a header (see {@link HeaderDialect}). */
80+
private static final ScriptLanguage HEADER_LANGUAGE =
81+
new AbstractScriptLanguage()
82+
{
83+
84+
@Override
85+
public ScriptEngine getScriptEngine() {
86+
return null;
87+
}
88+
};
89+
7590
private static Context context;
7691

7792
@BeforeAll
@@ -142,6 +157,32 @@ public void testJdkClassImport() {
142157
"from java.util import ArrayList"), "expected ArrayList import among " + texts);
143158
}
144159

160+
/**
161+
* Tests that accepting a Java class imports it at the top of the script,
162+
* when the script imports nothing yet, even if its dialect prepends a
163+
* header (which is not part of the script, so cannot take the import).
164+
*/
165+
@Test
166+
public void testClassImportAfterHeader() {
167+
ClassIndex.ensureCache();
168+
final LanguageServerService servers = context.service(
169+
LanguageServerService.class);
170+
final String importList = "from java.util import ArrayList";
171+
172+
try (ScriptSession session = new ScriptSession(servers, HEADER_LANGUAGE)) {
173+
// Find the CompletionItem that is importing java.util.ArrayList
174+
List<CompletionItem> items = items(session, "#@String s\n#@Double d\n\nArrayLi");
175+
CompletionItem arrayList = items.stream()
176+
.filter(c -> c.getKind() == CompletionItemKind.Class)
177+
.filter(c -> importList.equals(c.getDetail()))
178+
.findFirst().orElseThrow();
179+
// NB: At the top of the script itself, as the header is not part of it.
180+
final Position top = new Position(0, 0);
181+
assertEquals(Collections.singletonList(new TextEdit(new Range(top, top),
182+
importList + "\n")), arrayList.getAdditionalTextEdits());
183+
}
184+
}
185+
145186
@Test
146187
public void testInheritedMembers() {
147188
// NB: The stubs of the packages java.util refers to (e.g. java.lang, of
@@ -243,6 +284,29 @@ public Header header(final Map<String, Variable> variables) {
243284
}
244285
}
245286

287+
/**
288+
* Makes {@code HEADER_LANGUAGE} Jython, with a header that imports nothing
289+
* (as one declaring a parameter might).
290+
*/
291+
@Plugin(type = ScriptDialect.class)
292+
public static class HeaderDialect implements ScriptDialect {
293+
294+
@Override
295+
public boolean supports(final ScriptLanguage language) {
296+
return language == HEADER_LANGUAGE;
297+
}
298+
299+
@Override
300+
public String outputLanguage() {
301+
return "jython";
302+
}
303+
304+
@Override
305+
public Header header(final Map<String, Variable> variables) {
306+
return new Header("name = None", Collections.emptySet());
307+
}
308+
}
309+
246310
private static SignatureHelp help(final String code) {
247311
try (ScriptSession session = session(new Environment())) {
248312
return session.signatureHelp(code, code.length()).get(30,

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,13 @@ private static CompletionList back(final ScriptDocument doc,
623623
if (item.getAdditionalTextEdits() != null) {
624624
final List<TextEdit> edits = new ArrayList<>();
625625
for (final TextEdit e : item.getAdditionalTextEdits()) {
626-
final Range r = doc.toOriginal(e.getRange());
626+
Range r = doc.toOriginal(e.getRange());
627+
// HACK: An insertion at the very start, before the header, goes
628+
// at the start of the script, as it would without a header (e.g.
629+
// an import, where the script has none, as Jython's server puts
630+
// it). Only there: nowhere else in the header maps to the script.
631+
if (r == null && atStart(e.getRange())) r = new Range(new Position(
632+
0, 0), new Position(0, 0));
627633
if (r != null) edits.add(new TextEdit(r, e.getNewText()));
628634
}
629635
item.setAdditionalTextEdits(edits);
@@ -633,6 +639,12 @@ private static CompletionList back(final ScriptDocument doc,
633639
return new CompletionList(list.isIncomplete(), items);
634640
}
635641

642+
/** Whether the range is empty, at the very start of the text. */
643+
private static boolean atStart(final Range r) {
644+
final Position start = new Position(0, 0);
645+
return start.equals(r.getStart()) && start.equals(r.getEnd());
646+
}
647+
636648
/**
637649
* The name a completion's label starts with, e.g. {@code java} for
638650
* {@code java.lang} and {@code max} for {@code max(a, b)}.

0 commit comments

Comments
 (0)