Skip to content

Commit 142274f

Browse files
gselzerclaude
andcommitted
code-lsp: test Positions in a PositionsTest
testOffset and testPosition move out of ServerProcessesTest, joined by tests of clamping, CRLF text, and callStart. ServerProcessesTest's fake server drops what no test uses any more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8fc6f86 commit 142274f

2 files changed

Lines changed: 89 additions & 87 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
34+
import org.eclipse.lsp4j.Position;
35+
import org.junit.jupiter.api.Test;
36+
37+
/**
38+
* Tests {@link Positions}.
39+
*
40+
* @author Gabriel Selzer
41+
*/
42+
public class PositionsTest {
43+
44+
@Test
45+
public void testOffset() {
46+
assertEquals(4, Positions.offset("ab\ncd", new Position(1, 1)));
47+
// Clamped to the line, and the text.
48+
assertEquals(2, Positions.offset("ab\ncd", new Position(0, 9)));
49+
assertEquals(5, Positions.offset("ab\ncd", new Position(7, 0)));
50+
}
51+
52+
@Test
53+
public void testPosition() {
54+
assertEquals(new Position(0, 0), Positions.position("ab\ncd", 0));
55+
assertEquals(new Position(1, 1), Positions.position("ab\ncd", 4));
56+
// UTF-16 code units, as Java counts.
57+
assertEquals(new Position(1, 3), Positions.position("a\n😀b", 5));
58+
// Clamped to the text.
59+
assertEquals(new Position(1, 2), Positions.position("ab\ncd", 99));
60+
assertEquals(new Position(0, 0), Positions.position("ab\ncd", -1));
61+
}
62+
63+
@Test
64+
public void testCrlf() {
65+
// Lines after a \r\n start where they should, both ways.
66+
final String text = "ab\r\ncd";
67+
assertEquals(5, Positions.offset(text, new Position(1, 1)));
68+
assertEquals(new Position(1, 1), Positions.position(text, 5));
69+
}
70+
71+
@Test
72+
public void testCallStart() {
73+
// The call around the caret: its opening parenthesis.
74+
assertEquals(1, Positions.callStart("f(a, ", 5));
75+
// Closed calls within it are skipped.
76+
assertEquals(1, Positions.callStart("f(g(x), ", 8));
77+
// Within a list or block, not a call.
78+
assertEquals(-1, Positions.callStart("[1, ", 4));
79+
assertEquals(-1, Positions.callStart("{", 1));
80+
// Only on the caret's line.
81+
assertEquals(-1, Positions.callStart("f(\n", 3));
82+
// Not once the call is closed.
83+
assertEquals(-1, Positions.callStart("f(x)", 4));
84+
}
85+
}

‎scijava-code-lsp/src/test/java/org/scijava/code/lsp/server/ServerProcessesTest.java‎

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static org.junit.jupiter.api.Assertions.assertEquals;
3333
import static org.junit.jupiter.api.Assertions.assertFalse;
3434
import static org.junit.jupiter.api.Assertions.assertNotSame;
35-
import static org.junit.jupiter.api.Assertions.assertNull;
3635
import static org.junit.jupiter.api.Assertions.assertSame;
3736
import static org.junit.jupiter.api.Assertions.assertThrows;
3837
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -66,12 +65,8 @@
6665
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
6766
import org.eclipse.lsp4j.InitializeParams;
6867
import org.eclipse.lsp4j.InitializeResult;
69-
import org.eclipse.lsp4j.ParameterInformation;
7068
import org.eclipse.lsp4j.Position;
7169
import org.eclipse.lsp4j.ServerCapabilities;
72-
import org.eclipse.lsp4j.SignatureHelp;
73-
import org.eclipse.lsp4j.SignatureHelpParams;
74-
import org.eclipse.lsp4j.SignatureInformation;
7570
import org.eclipse.lsp4j.jsonrpc.messages.Either;
7671
import org.eclipse.lsp4j.launch.LSPLauncher;
7772
import org.eclipse.lsp4j.services.LanguageServer;
@@ -81,7 +76,6 @@
8176
import org.junit.jupiter.api.Test;
8277
import org.scijava.Context;
8378
import org.scijava.code.lsp.Environment;
84-
import org.scijava.code.lsp.Positions;
8579
import org.scijava.log.LogService;
8680
import org.scijava.log.StderrLogService;
8781

@@ -111,14 +105,6 @@ public void tearDown() {
111105
if (servers != null) servers.shutdown();
112106
}
113107

114-
@Test
115-
public void testOffset() {
116-
assertEquals(4, Positions.offset("ab\ncd", new Position(1, 1)));
117-
// Clamped to the line, and the text.
118-
assertEquals(2, Positions.offset("ab\ncd", new Position(0, 9)));
119-
assertEquals(5, Positions.offset("ab\ncd", new Position(7, 0)));
120-
}
121-
122108
@Test
123109
public void testLaunched() throws Exception {
124110
// A server process, seen as a LanguageServer (for LanguageServerPlugins).
@@ -318,15 +304,6 @@ public void testCloseOwnOnly() throws Exception {
318304
assertFalse(started.get(1).events.contains("close " + uri));
319305
}
320306

321-
@Test
322-
public void testPosition() {
323-
assertEquals(new Position(0, 0), Positions.position("ab\ncd", 0));
324-
assertEquals(new Position(1, 1), Positions.position("ab\ncd", 4));
325-
// UTF-16 code units, as Java counts.
326-
assertEquals(new Position(1, 3), Positions.position("a\n😀b",
327-
5));
328-
}
329-
330307
// -- Helper methods --
331308

332309
private static List<CompletionItem> complete(final ProcessLanguageServer s,
@@ -432,27 +409,20 @@ public String rootUri(final Environment env) {
432409
}
433410
}
434411

435-
/** A language server knowing a few things about json, and a function f. */
412+
/** A language server knowing a few things about json. */
436413
private static final class FakeServer implements LanguageServer,
437414
TextDocumentService, WorkspaceService
438415
{
439416

440-
private volatile boolean alive = true, shutdown, noActive, mute;
417+
private volatile boolean alive = true, shutdown;
441418
private volatile org.eclipse.lsp4j.services.LanguageClient client;
442-
private volatile Object options;
443419
private final Map<String, String> texts = new LinkedHashMap<>();
444420
private final List<String> events = new CopyOnWriteArrayList<>();
445-
private final List<Position> positions = new CopyOnWriteArrayList<>();
446-
/** If set, completion requests wait for this (and are kept as held). */
447-
private volatile CompletableFuture<Void> hold;
448-
private final List<CompletableFuture<Either<List<CompletionItem>, CompletionList>>> held =
449-
new CopyOnWriteArrayList<>();
450421

451422
@Override
452423
public CompletableFuture<InitializeResult> initialize(
453424
final InitializeParams params)
454425
{
455-
options = params.getInitializationOptions();
456426
final ServerCapabilities capabilities = new ServerCapabilities();
457427
capabilities.setCompletionProvider(new CompletionOptions(true, null));
458428
return CompletableFuture.completedFuture(new InitializeResult(
@@ -484,45 +454,12 @@ public WorkspaceService getWorkspaceService() {
484454
public CompletableFuture<Either<List<CompletionItem>, CompletionList>>
485455
completion(final CompletionParams params)
486456
{
487-
positions.add(params.getPosition());
488457
final List<CompletionItem> items = new ArrayList<>();
489458
items.add(item("decoder", CompletionItemKind.Module, "c"));
490459
items.add(item("dump", CompletionItemKind.Function, "b"));
491460
items.add(item("dumps", CompletionItemKind.Function, "a"));
492-
final Either<List<CompletionItem>, CompletionList> answer = Either
493-
.forRight(new CompletionList(items));
494-
if (hold == null) return CompletableFuture.completedFuture(answer);
495-
final CompletableFuture<Either<List<CompletionItem>, CompletionList>> f =
496-
new CompletableFuture<>();
497-
held.add(f);
498-
hold.thenRun(() -> f.complete(answer));
499-
return f;
500-
}
501-
502-
@Override
503-
public CompletableFuture<CompletionItem> resolveCompletionItem(
504-
final CompletionItem item)
505-
{
506-
events.add("resolve " + item.getLabel());
507-
item.setDetail("def " + item.getLabel() + "(obj, indent) -> str");
508-
item.setDocumentation("Docs of " + item.getLabel() + " & co.");
509-
return CompletableFuture.completedFuture(item);
510-
}
511-
512-
@Override
513-
public CompletableFuture<SignatureHelp> signatureHelp(
514-
final SignatureHelpParams params)
515-
{
516-
if (params.getPosition().getCharacter() < 2) {
517-
return CompletableFuture.completedFuture(null);
518-
}
519-
final SignatureInformation f = new SignatureInformation("def f(x, y=1)");
520-
f.setDocumentation("Docs of f");
521-
f.setParameters(Arrays.asList(new ParameterInformation("x"),
522-
new ParameterInformation("y=1")));
523-
final SignatureInformation g = new SignatureInformation("def f(z)");
524-
return CompletableFuture.completedFuture(new SignatureHelp(Arrays.asList(
525-
f, g), 0, noActive ? null : 1));
461+
return CompletableFuture.completedFuture(Either.forRight(
462+
new CompletionList(items)));
526463
}
527464

528465
@Override
@@ -547,7 +484,6 @@ public void didChange(final DidChangeTextDocumentParams params) {
547484

548485
/** Complains about each "!!", and about the first line, always. */
549486
private void publish(final String uri, final int version) {
550-
if (mute) return;
551487
final List<org.eclipse.lsp4j.Diagnostic> list = new ArrayList<>();
552488
list.add(diagnostic(0, 0, 1, "First line"));
553489
final String[] lines = texts.get(uri).split("\n", -1);
@@ -570,25 +506,6 @@ private static org.eclipse.lsp4j.Diagnostic diagnostic(final int line,
570506
return d;
571507
}
572508

573-
@Override
574-
public CompletableFuture<org.eclipse.lsp4j.Hover> hover(
575-
final org.eclipse.lsp4j.HoverParams params)
576-
{
577-
final String text = texts.get(params.getTextDocument().getUri());
578-
final String line = text.split("\n", -1)[params.getPosition()
579-
.getLine()];
580-
int start = params.getPosition().getCharacter(), end = start;
581-
while (start > 0 && Character.isLetter(line.charAt(start - 1))) start--;
582-
while (end < line.length() && Character.isLetter(line.charAt(end))) end++;
583-
if (start == end) return CompletableFuture.completedFuture(null);
584-
final org.eclipse.lsp4j.Hover h = new org.eclipse.lsp4j.Hover(
585-
new org.eclipse.lsp4j.MarkupContent("plaintext", "Docs of " + line
586-
.substring(start, end)), new org.eclipse.lsp4j.Range(new Position(
587-
params.getPosition().getLine(), start), new Position(params
588-
.getPosition().getLine(), end)));
589-
return CompletableFuture.completedFuture(h);
590-
}
591-
592509
@Override
593510
public void didClose(final DidCloseTextDocumentParams params) {
594511
events.add("close " + params.getTextDocument().getUri());

0 commit comments

Comments
 (0)