3636import java .util .concurrent .TimeUnit ;
3737import java .util .concurrent .atomic .AtomicInteger ;
3838import java .util .function .Consumer ;
39- import java .util .function .Function ;
4039import java .util .function .Supplier ;
4140
4241import org .eclipse .lsp4j .CompletionItem ;
4847import org .eclipse .lsp4j .DidOpenTextDocumentParams ;
4948import org .eclipse .lsp4j .Hover ;
5049import org .eclipse .lsp4j .HoverParams ;
50+ import org .eclipse .lsp4j .InitializeParams ;
51+ import org .eclipse .lsp4j .InitializeResult ;
52+ import org .eclipse .lsp4j .InitializedParams ;
5153import org .eclipse .lsp4j .MessageActionItem ;
5254import org .eclipse .lsp4j .MessageParams ;
5355import org .eclipse .lsp4j .PublishDiagnosticsParams ;
7880 * {@link Positions}). Answers are lsp4j's own types.
7981 * </p>
8082 * <p>
81- * A session owns its server: it is the server's client. Get one from the
83+ * A session owns its server: it is the server's client, initializes it, and
84+ * shuts it down when closed. Get one from the
8285 * {@link LanguageServerService#session service}, which makes it a server of
8386 * its own.
8487 * </p>
@@ -89,9 +92,11 @@ public class ScriptSession implements AutoCloseable {
8992
9093 private static final AtomicInteger UNTITLED = new AtomicInteger ();
9194
95+ /** How long to wait for a server to initialize, in milliseconds. */
96+ private static final long INITIALIZE_TIMEOUT = 10_000 ;
97+
9298 private final LanguageServer server ;
9399 private final ScriptLanguage language ;
94- private final Function <String , CompletableFuture <List <Diagnostic >>> allReported ;
95100 private final String untitled ;
96101 private volatile Supplier <File > file = () -> null ;
97102 private volatile Logger log ;
@@ -107,15 +112,12 @@ public class ScriptSession implements AutoCloseable {
107112 /**
108113 * @param server The server, which the session owns.
109114 * @param language The script's language, or null if unknown.
110- * @param allReported Gets the problems in a document once all its servers
111- * reported them; or null if the server reports them at once.
112115 */
113- ScriptSession (final LanguageServer server , final ScriptLanguage language ,
114- final Function < String , CompletableFuture < List < Diagnostic >>> allReported )
116+ private ScriptSession (final LanguageServer server ,
117+ final ScriptLanguage language )
115118 {
116119 this .server = server ;
117120 this .language = language ;
118- this .allReported = allReported ;
119121 final List <String > extensions = language == null ? Collections
120122 .emptyList () : language .getExtensions ();
121123 untitled = "untitled:/script-" + UNTITLED .incrementAndGet () + (extensions
@@ -125,40 +127,36 @@ public class ScriptSession implements AutoCloseable {
125127 /**
126128 * Makes a session about a script, with the given server of its own (e.g. a
127129 * fake one, in tests): the session becomes its client, so the server must
128- * not be shared. The server is taken to report each version's problems at
129- * once .
130+ * not be shared. The server is initialized now, and shut down when the
131+ * session is closed .
130132 *
131133 * @param server The server, which the session owns.
132134 * @param language The script's language, or null if unknown.
133135 */
134136 public static ScriptSession owning (final LanguageServer server ,
135137 final ScriptLanguage language )
136138 {
137- return owning (server , language , null );
139+ return new ScriptSession (server , language ). connect ( );
138140 }
139141
140142 /**
141- * As {@link #owning(LanguageServer, ScriptLanguage)}, for a server that
142- * stands for several (e.g. one per plugin), which report each version's
143- * problems separately.
144- *
145- * @param server The server, which the session owns.
146- * @param language The script's language, or null if unknown.
147- * @param allReported Gets the problems in a document once all its servers
148- * reported them; or null if the server reports them at once.
143+ * Becomes the client of its server, and initializes it: as LSP says, it
144+ * awaits the server's answer before telling it anything else. (Servers in
145+ * process answer at once.)
149146 */
150- public static ScriptSession owning (final LanguageServer server ,
151- final ScriptLanguage language ,
152- final Function <String , CompletableFuture <List <Diagnostic >>> allReported )
153- {
154- return new ScriptSession (server , language , allReported ).connect ();
155- }
156-
157- /** Becomes the client of its server. */
158- ScriptSession connect () {
147+ private ScriptSession connect () {
159148 if (server instanceof LanguageClientAware ) {
160149 ((LanguageClientAware ) server ).connect (new Client ());
161150 }
151+ try {
152+ final CompletableFuture <InitializeResult > init = server .initialize (
153+ new InitializeParams ());
154+ if (init != null ) init .get (INITIALIZE_TIMEOUT , TimeUnit .MILLISECONDS );
155+ }
156+ catch (final Exception exc ) {
157+ // NB: Not initialized (in time): asked anyway, answering what it can.
158+ }
159+ server .initialized (new InitializedParams ());
162160 return this ;
163161 }
164162
@@ -194,7 +192,7 @@ public ScriptSession setDiagnosticsListener(
194192 */
195193 public synchronized ScriptSession update (final String newText ) {
196194 final String current = currentUri ();
197- if (uri != null && !uri .equals (current )) close ();
195+ if (uri != null && !uri .equals (current )) closeDocument ();
198196 if (uri == null ) {
199197 uri = current ;
200198 version = 1 ;
@@ -262,8 +260,8 @@ public synchronized List<Diagnostic> diagnostics() {
262260 * Waits for the problems in the text last sent: for tools that ask once,
263261 * rather than editors.
264262 *
265- * @param timeout How long to wait, in milliseconds; servers not done by
266- * then are left out .
263+ * @param timeout How long to wait, in milliseconds; if the server is not
264+ * done by then, the problems reported so far .
267265 */
268266 public List <Diagnostic > diagnostics (final long timeout ) {
269267 final String u ;
@@ -274,17 +272,30 @@ public List<Diagnostic> diagnostics(final long timeout) {
274272 }
275273 if (u == null ) return Collections .emptyList ();
276274 try {
277- return (allReported == null ? next : allReported .apply (u )).get (timeout ,
278- TimeUnit .MILLISECONDS );
275+ return next .get (timeout , TimeUnit .MILLISECONDS );
279276 }
280277 catch (final Exception exc ) {
281- return diagnostics (); // NB: Not all in time: those that were .
278+ return diagnostics (); // NB: Not in time: those reported so far .
282279 }
283280 }
284281
285- /** Closes the server's copy of the script, if open . */
282+ /** Closes the server's copy of the script, and shuts the server down . */
286283 @ Override
287284 public synchronized void close () {
285+ closeDocument ();
286+ try {
287+ final CompletableFuture <Object > done = server .shutdown ();
288+ if (done != null ) done .thenRun (server ::exit );
289+ }
290+ catch (final RuntimeException | LinkageError exc ) {
291+ if (log != null ) log .debug ("Language server shutdown failed" , exc );
292+ }
293+ }
294+
295+ // -- Helper methods --
296+
297+ /** Closes the server's copy of the script, if open. */
298+ private void closeDocument () {
288299 if (uri == null ) return ;
289300 server .getTextDocumentService ().didClose (new DidCloseTextDocumentParams (
290301 new TextDocumentIdentifier (uri )));
@@ -293,8 +304,6 @@ public synchronized void close() {
293304 diagnostics = Collections .emptyList ();
294305 }
295306
296- // -- Helper methods --
297-
298307 private TextDocumentIdentifier identifier () {
299308 return new TextDocumentIdentifier (uri );
300309 }
0 commit comments