4646
4747import org .eclipse .lsp4j .CompletionItem ;
4848import org .eclipse .lsp4j .CompletionList ;
49- import org .eclipse .lsp4j .CompletionParams ;
5049import org .eclipse .lsp4j .Diagnostic ;
51- import org .eclipse .lsp4j .DidChangeTextDocumentParams ;
52- import org .eclipse .lsp4j .DidOpenTextDocumentParams ;
5350import org .eclipse .lsp4j .Hover ;
54- import org .eclipse .lsp4j .HoverParams ;
5551import org .eclipse .lsp4j .SignatureHelp ;
56- import org .eclipse .lsp4j .SignatureHelpParams ;
57- import org .eclipse .lsp4j .TextDocumentContentChangeEvent ;
58- import org .eclipse .lsp4j .TextDocumentIdentifier ;
59- import org .eclipse .lsp4j .TextDocumentItem ;
60- import org .eclipse .lsp4j .VersionedTextDocumentIdentifier ;
6152import org .junit .AfterClass ;
6253import org .junit .BeforeClass ;
6354import org .junit .Test ;
6455import org .scijava .Context ;
6556import org .scijava .code .lsp .LanguageServerService ;
6657import org .scijava .code .lsp .Positions ;
67- import org .scijava .code .lsp .MergedLanguageServer ;
58+ import org .scijava .code .lsp .ScriptSession ;
6859import org .scijava .code .lsp .UpdatingCompletionList ;
6960import org .scijava .module .ModuleService ;
7061import org .scijava .script .ScriptInfo ;
@@ -95,21 +86,22 @@ public class ApposePythonCompletionIntegrationTest {
9586
9687 private static Context context ;
9788 private static ScriptLanguage language ;
98- private static MergedLanguageServer server ;
89+ private static LanguageServerService servers ;
9990 private static File scriptDir ;
10091
10192 @ BeforeClass
10293 public static void setUp () throws URISyntaxException {
10394 context = new Context ();
10495 language = context .service (ScriptService .class ).getLanguageByName (
10596 "appose-python" );
106- server = context .service (LanguageServerService .class ). server ( language );
97+ servers = context .service (LanguageServerService .class );
10798 scriptDir = new File (ApposePythonCompletionIntegrationTest .class
10899 .getResource ("/appose-test/requirements.txt" ).toURI ()).getParentFile ();
109100 }
110101
111102 @ AfterClass
112103 public static void tearDown () {
104+ sessions .values ().forEach (ScriptSession ::close );
113105 if (context != null ) context .dispose ();
114106 }
115107
@@ -123,8 +115,8 @@ public void testStandardLibrary() throws Exception {
123115 .equals ("dumps" ) && c .getLabelDetails () != null ).findFirst ().get ();
124116 assertTrue (dumps .getLabelDetails ().getDetail (), dumps .getLabelDetails ()
125117 .getDetail ().startsWith ("(Any obj, bool skipkeys" ));
126- assertTrue (dumps .getDocumentation () != null || server
127- . resolveCompletionItem ( dumps ).get (1 , TimeUnit .MINUTES )
118+ assertTrue (dumps .getDocumentation () != null || session ( null ). resolve (
119+ dumps ).get (1 , TimeUnit .MINUTES )
128120 .getDocumentation () != null );
129121 report ("import json\n json.l" , null , "loads" , "o" );
130122 }
@@ -153,13 +145,12 @@ public void testScriptEnvironment() throws Exception {
153145 @ Test
154146 public void testSignatureHelp () throws Exception {
155147 final String code = "import json\n json.dumps(obj, " ;
156- final String uri = sync ( code , null );
148+ final ScriptSession session = session ( null );
157149 final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
158150 SignatureHelp help = null ;
159151 while (help == null && System .currentTimeMillis () < deadline ) {
160- help = server .signatureHelp (new SignatureHelpParams (
161- new TextDocumentIdentifier (uri ), Positions .position (code , code
162- .length ()))).get (1 , TimeUnit .MINUTES );
152+ help = session .signatureHelp (code , code .length ()).get (1 ,
153+ TimeUnit .MINUTES );
163154 }
164155 assertTrue (help .getSignatures ().get (0 ).getLabel ().contains ("dumps(" ));
165156 assertFalse (help .getSignatures ().get (0 ).getParameters ().isEmpty ());
@@ -169,13 +160,12 @@ public void testSignatureHelp() throws Exception {
169160 @ Test
170161 public void testHover () throws Exception {
171162 final String code = "import json\n json.dumps(1)" ;
172- final String uri = sync ( code , null );
163+ final ScriptSession session = session ( null );
173164 final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
174165 Hover hover = null ;
175166 while (hover == null && System .currentTimeMillis () < deadline ) {
176- hover = server .hover (new HoverParams (new TextDocumentIdentifier (uri ),
177- Positions .position (code , code .indexOf ("dumps" ) + 2 ))).get (1 ,
178- TimeUnit .MINUTES );
167+ hover = session .hover (code , code .indexOf ("dumps" ) + 2 ).get (1 ,
168+ TimeUnit .MINUTES );
179169 }
180170 assertTrue (String .valueOf (hover ), hover .getContents ().getRight ().getValue ()
181171 .contains ("JSON" ));
@@ -190,19 +180,17 @@ public void testHover() throws Exception {
190180 public void testDiagnostics () throws Exception {
191181 // A syntax error, on the script's third line.
192182 final String code = "#@ double sigma\n x = sigma\n y = (1 +\n " ;
193- final LanguageServerService servers = context .service (
194- LanguageServerService .class );
195183 final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
196184 List <Diagnostic > problems = Collections .emptyList ();
197185 while (problems .isEmpty () && System .currentTimeMillis () < deadline ) {
198- problems = servers .diagnose (language , "untitled:/diagnose.py" , code ,
186+ problems = servers .diagnose (language , code ,
199187 120_000 );
200188 }
201189 assertFalse (problems .isEmpty ());
202190 assertTrue (problems .toString (), problems .get (0 ).getRange ().getStart ()
203191 .getLine () >= 2 );
204192 // Fixed: no more problems.
205- assertTrue (servers .diagnose (language , "untitled:/diagnose.py" , code
193+ assertTrue (servers .diagnose (language , code
206194 .replace ("(1 +" , "(1 + 2)" ), 120_000 ).isEmpty ());
207195 }
208196
@@ -249,26 +237,18 @@ public void testNewDependency() throws Exception {
249237
250238 // -- Helper methods --
251239
252- /** The open documents' versions , by URI . */
253- private static final Map <String , Integer > versions = new HashMap <>();
240+ /** The scripts' sessions , by path ("" for an untitled one) . */
241+ private static final Map <String , ScriptSession > sessions = new HashMap <>();
254242
255- /** Opens a script on the server, or changes it; returns its URI. */
256- private static String sync (final String code , final String path ) {
257- final String uri = path == null ? "untitled:/integration.py" : new File (
258- path ).toURI ().toString ();
259- final Integer version = versions .get (uri );
260- if (version == null ) {
261- versions .put (uri , 1 );
262- server .didOpen (new DidOpenTextDocumentParams (new TextDocumentItem (uri ,
263- "appose-python" , 1 , code )));
264- }
265- else {
266- versions .put (uri , version + 1 );
267- server .didChange (new DidChangeTextDocumentParams (
268- new VersionedTextDocumentIdentifier (uri , version + 1 ), Collections
269- .singletonList (new TextDocumentContentChangeEvent (code ))));
270- }
271- return uri ;
243+ /** The session about a script (null path: untitled). */
244+ private static ScriptSession session (final String path ) {
245+ return sessions .computeIfAbsent (path == null ? "" : path , p -> servers
246+ .session (language ).setFile (path == null ? null : new File (path )));
247+ }
248+
249+ /** Tells the servers a script's text. */
250+ private static ScriptSession sync (final String code , final String path ) {
251+ return session (path ).update (code );
272252 }
273253
274254 /** Completes until the expected completion appears (environments build). */
@@ -295,10 +275,8 @@ private static List<CompletionItem> completeUntil(final String code,
295275 private static List <CompletionItem > complete (final String code ,
296276 final String path , final boolean settled ) throws Exception
297277 {
298- final String uri = sync (code , path );
299- CompletionList list = server .completion (new CompletionParams (
300- new TextDocumentIdentifier (uri ), Positions .position (code , code .length ())))
301- .get (1 , TimeUnit .MINUTES ).getRight ();
278+ CompletionList list = session (path ).completion (code , code .length ())
279+ .get (1 , TimeUnit .MINUTES );
302280 if (settled && list instanceof UpdatingCompletionList ) {
303281 final CompletionList better = ((UpdatingCompletionList ) list ).update ()
304282 .get (1 , TimeUnit .MINUTES );
@@ -321,11 +299,10 @@ private static void report(final String code, final String path,
321299 final int n = complete (code + more , path , true ).size ();
322300 final long cached = System .nanoTime () - t0 ;
323301 final String other = code .substring (0 , code .lastIndexOf ('.' ) + 1 ) + "s" ;
324- final String uri = sync ( other , path );
302+ final ScriptSession session = session ( path );
325303 t0 = System .nanoTime ();
326- final CompletionList first = server .completion (new CompletionParams (
327- new TextDocumentIdentifier (uri ), Positions .position (other , other
328- .length ()))).get (1 , TimeUnit .MINUTES ).getRight ();
304+ final CompletionList first = session .completion (other , other .length ())
305+ .get (1 , TimeUnit .MINUTES );
329306 final long wait = System .nanoTime () - t0 ;
330307 final CompletionList update = first instanceof UpdatingCompletionList
331308 ? ((UpdatingCompletionList ) first ).update ().get (1 , TimeUnit .MINUTES )
0 commit comments