3232import static org .junit .jupiter .api .Assertions .assertEquals ;
3333import static org .junit .jupiter .api .Assertions .assertFalse ;
3434import static org .junit .jupiter .api .Assertions .assertNotSame ;
35- import static org .junit .jupiter .api .Assertions .assertNull ;
3635import static org .junit .jupiter .api .Assertions .assertSame ;
3736import static org .junit .jupiter .api .Assertions .assertThrows ;
3837import static org .junit .jupiter .api .Assertions .assertTrue ;
6665import org .eclipse .lsp4j .DidSaveTextDocumentParams ;
6766import org .eclipse .lsp4j .InitializeParams ;
6867import org .eclipse .lsp4j .InitializeResult ;
69- import org .eclipse .lsp4j .ParameterInformation ;
7068import org .eclipse .lsp4j .Position ;
7169import org .eclipse .lsp4j .ServerCapabilities ;
72- import org .eclipse .lsp4j .SignatureHelp ;
73- import org .eclipse .lsp4j .SignatureHelpParams ;
74- import org .eclipse .lsp4j .SignatureInformation ;
7570import org .eclipse .lsp4j .jsonrpc .messages .Either ;
7671import org .eclipse .lsp4j .launch .LSPLauncher ;
7772import org .eclipse .lsp4j .services .LanguageServer ;
8176import org .junit .jupiter .api .Test ;
8277import org .scijava .Context ;
8378import org .scijava .code .lsp .Environment ;
84- import org .scijava .code .lsp .Positions ;
8579import org .scijava .log .LogService ;
8680import 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\n cd" , new Position (1 , 1 )));
117- // Clamped to the line, and the text.
118- assertEquals (2 , Positions .offset ("ab\n cd" , new Position (0 , 9 )));
119- assertEquals (5 , Positions .offset ("ab\n cd" , 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\n cd" , 0 ));
324- assertEquals (new Position (1 , 1 ), Positions .position ("ab\n cd" , 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