|
52 | 52 | import org.scijava.Context; |
53 | 53 | import org.scijava.code.lsp.Environment; |
54 | 54 | import org.scijava.code.lsp.LanguageServerService; |
| 55 | +import org.scijava.code.lsp.Positions; |
55 | 56 | import org.scijava.code.lsp.ScriptDialect; |
56 | 57 | import org.scijava.code.lsp.ScriptSession; |
57 | 58 | import org.scijava.code.lsp.jvm.ClassIndex; |
@@ -183,6 +184,28 @@ public void testClassImportAfterHeader() { |
183 | 184 | } |
184 | 185 | } |
185 | 186 |
|
| 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 | + |
186 | 209 | @Test |
187 | 210 | public void testInheritedMembers() { |
188 | 211 | // NB: The stubs of the packages java.util refers to (e.g. java.lang, of |
@@ -228,8 +251,7 @@ public void testCallablesAreSnippets() { |
228 | 251 | @Test |
229 | 252 | public void testDunders() { |
230 | 253 | // 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 = __")); |
233 | 255 | assertTrue(dunders.contains("__name__"), dunders.toString()); |
234 | 256 | assertFalse(dunders.contains("__build_class__"), dunders.toString()); |
235 | 257 | } |
@@ -332,10 +354,16 @@ private static List<CompletionItem> completions(final String code) { |
332 | 354 |
|
333 | 355 | private static List<CompletionItem> items(final ScriptSession session, |
334 | 356 | 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) |
335 | 363 | { |
336 | 364 | try (ScriptSession s = session) { |
337 | 365 | 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()); |
339 | 367 | items.sort(Comparator.comparing(CompletionItem::getSortText)); |
340 | 368 | return items; |
341 | 369 | } |
|
0 commit comments