Skip to content

Commit c72e2f8

Browse files
gselzerclaude
andcommitted
code-lsp-jython: put imports after a last line on lines of their own
When a script's last import was its last line, without a line break, accepting a Java class appended its import onto that line (e.g. "from java.lang import Mathfrom java.util import ArrayList"). Now an import inserted where no line starts goes after a line break instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent debb717 commit c72e2f8

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎scijava-code-lsp-jython/src/main/java/org/scijava/code/lsp/jython/JythonImports.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ public static TextEdit autoImportEdit(final String text,
163163
}
164164
final int targetLine = imported.isEmpty() ? 0 : lastImportLine + 1;
165165
final int offset = offsetOfLine(text, targetLine);
166-
final Position at = Positions.position(text, Math.min(offset, text
167-
.length()));
168-
return new TextEdit(new Range(at, at), importStatement + "\n");
166+
final Position at = Positions.position(text, offset);
167+
// NB: After a last line without a line break (at the end), the import
168+
// goes on a line of its own.
169+
final boolean lineStart = offset == 0 || text.charAt(offset - 1) == '\n';
170+
final String newText = lineStart ? importStatement + "\n" : "\n" +
171+
importStatement;
172+
return new TextEdit(new Range(at, at), newText);
169173
}
170174

171175
/** Character offset of the start of the given (0-based) line in {@code text}. */

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.scijava.Context;
5353
import org.scijava.code.lsp.Environment;
5454
import org.scijava.code.lsp.LanguageServerService;
55+
import org.scijava.code.lsp.Positions;
5556
import org.scijava.code.lsp.ScriptDialect;
5657
import org.scijava.code.lsp.ScriptSession;
5758
import org.scijava.code.lsp.jvm.ClassIndex;
@@ -183,6 +184,28 @@ public void testClassImportAfterHeader() {
183184
}
184185
}
185186

187+
/**
188+
* Tests that accepting a Java class, when the script's last line is an
189+
* import without a line break, puts its import on a line of its own.
190+
*/
191+
@Test
192+
public void testClassImportAtEOF() {
193+
ClassIndex.ensureCache();
194+
final String additionalEdit = "from java.util import ArrayList";
195+
final String code = "x = ArrayLi\nfrom java.lang import Math";
196+
// Find the CompletionItem that is importing java.util.ArrayList.
197+
final List<CompletionItem> items = items(session(new Environment()), code,
198+
code.indexOf('\n'));
199+
final CompletionItem arrayList = items.stream() //
200+
.filter(c -> c.getKind() == CompletionItemKind.Class) //
201+
.filter(c -> additionalEdit.equals(c.getDetail())) //
202+
.findFirst().orElseThrow();
203+
// The insertion point should be at the end of the script.
204+
final Position end = Positions.position(code, code.length());
205+
assertEquals(Collections.singletonList(new TextEdit(new Range(end, end),
206+
"\n" + additionalEdit)), arrayList.getAdditionalTextEdits());
207+
}
208+
186209
@Test
187210
public void testInheritedMembers() {
188211
// NB: The stubs of the packages java.util refers to (e.g. java.lang, of
@@ -228,8 +251,7 @@ public void testCallablesAreSnippets() {
228251
@Test
229252
public void testDunders() {
230253
// Names like __name__: Jython's, not Python 3's.
231-
final List<String> dunders = labels(items(session(new Environment()),
232-
"x = __"));
254+
final List<String> dunders = labels(completions("x = __"));
233255
assertTrue(dunders.contains("__name__"), dunders.toString());
234256
assertFalse(dunders.contains("__build_class__"), dunders.toString());
235257
}
@@ -332,10 +354,16 @@ private static List<CompletionItem> completions(final String code) {
332354

333355
private static List<CompletionItem> items(final ScriptSession session,
334356
final String code)
357+
{
358+
return items(session, code, code.length());
359+
}
360+
361+
private static List<CompletionItem> items(final ScriptSession session,
362+
final String code, final int offset)
335363
{
336364
try (ScriptSession s = session) {
337365
final List<CompletionItem> items = new java.util.ArrayList<>(s.completion(
338-
code, code.length()).get(30, TimeUnit.SECONDS).getItems());
366+
code, offset).get(30, TimeUnit.SECONDS).getItems());
339367
items.sort(Comparator.comparing(CompletionItem::getSortText));
340368
return items;
341369
}

0 commit comments

Comments
 (0)