24
24
import com .redhat .devtools .intellij .lsp4ij .internal .SupportedFeatures ;
25
25
import com .redhat .devtools .intellij .lsp4ij .server .ProcessStreamConnectionProvider ;
26
26
import com .redhat .devtools .intellij .lsp4ij .server .StreamConnectionProvider ;
27
- import com .redhat .devtools .intellij .lsp4ij .settings .ServerTrace ;
28
- import com .redhat .devtools .intellij .lsp4ij .settings .UserDefinedLanguageServerSettings ;
29
27
import com .redhat .devtools .intellij .lsp4ij .lifecycle .LanguageServerLifecycleManager ;
30
28
import com .redhat .devtools .intellij .lsp4ij .lifecycle .NullLanguageServerLifecycleManager ;
31
29
import org .eclipse .lsp4j .*;
@@ -114,7 +112,11 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
114
112
private LanguageServer languageServer ;
115
113
private LanguageClientImpl languageClient ;
116
114
private ServerCapabilities serverCapabilities ;
117
- private Timer timer ;
115
+
116
+ private final Timer timer = new Timer ("Stop Language Server Task Processor" ); //$NON-NLS-1$
117
+
118
+ private TimerTask stopTimerTask ;
119
+
118
120
private final AtomicBoolean stopping = new AtomicBoolean (false );
119
121
120
122
private final ExecutorService dispatcher ;
@@ -295,7 +297,7 @@ public synchronized void start() throws IOException {
295
297
messageBusConnection = ApplicationManager .getApplication ().getMessageBus ().connect ();
296
298
messageBusConnection .subscribe (AppTopics .FILE_DOCUMENT_SYNC , fileBufferListener );
297
299
messageBusConnection .subscribe (FileEditorManagerListener .FILE_EDITOR_MANAGER , fileBufferListener );
298
- getLanguageServerLifecycleManager ().onStartedLanguageServer (this , null );
300
+ getLanguageServerLifecycleManager ().onStartedLanguageServer (this , null );
299
301
}).exceptionally (e -> {
300
302
LOGGER .error ("Error while starting language server '" + serverDefinition .id + "'" , e );
301
303
initializeFuture .completeExceptionally (e );
@@ -372,23 +374,30 @@ private void logMessage(Message message, MessageConsumer consumer) {
372
374
getLanguageServerLifecycleManager ().logLSPMessage (message , consumer , this );
373
375
}
374
376
375
- private void removeStopTimer () {
376
- if (timer != null ) {
377
- timer .cancel ();
378
- timer = null ;
379
- getLanguageServerLifecycleManager ().onStartedLanguageServer (this , null );
377
+ private void removeStopTimerTask () {
378
+ synchronized (timer ) {
379
+ if (stopTimerTask != null ) {
380
+ stopTimerTask .cancel ();
381
+ stopTimerTask = null ;
382
+ getLanguageServerLifecycleManager ().onStartedLanguageServer (this , null );
383
+ }
380
384
}
381
385
}
382
386
383
- private void startStopTimer () {
384
- timer = new Timer ("Stop Language Server Timer" ); //$NON-NLS-1$
385
- getLanguageServerLifecycleManager ().onStoppingLanguageServer (this );
386
- timer .schedule (new TimerTask () {
387
- @ Override
388
- public void run () {
389
- stop ();
387
+ private void startStopTimerTask () {
388
+ synchronized (timer ) {
389
+ if (stopTimerTask != null ) {
390
+ stopTimerTask .cancel ();
390
391
}
391
- }, TimeUnit .SECONDS .toMillis (this .serverDefinition .lastDocumentDisconnectedTimeout ));
392
+ getLanguageServerLifecycleManager ().onStoppingLanguageServer (this );
393
+ stopTimerTask = new TimerTask () {
394
+ @ Override
395
+ public void run () {
396
+ stop ();
397
+ }
398
+ };
399
+ timer .schedule (stopTimerTask , TimeUnit .SECONDS .toMillis (this .serverDefinition .lastDocumentDisconnectedTimeout ));
400
+ }
392
401
}
393
402
394
403
/**
@@ -413,7 +422,7 @@ public synchronized void stop() {
413
422
return ;
414
423
}
415
424
getLanguageServerLifecycleManager ().onStoppingLanguageServer (this );
416
- removeStopTimer ();
425
+ removeStopTimerTask ();
417
426
if (this .languageClient != null ) {
418
427
this .languageClient .dispose ();
419
428
}
@@ -619,7 +628,7 @@ private boolean supportsWorkspaceFolderCapability() {
619
628
* @noreference internal so far
620
629
*/
621
630
private CompletableFuture <LanguageServer > connect (@ Nonnull URI absolutePath , Document document ) throws IOException {
622
- removeStopTimer ();
631
+ removeStopTimerTask ();
623
632
final URI thePath = absolutePath ; // should be useless
624
633
625
634
VirtualFile file = FileDocumentManager .getInstance ().getFile (document );
@@ -677,8 +686,7 @@ private void disconnect(URI path, boolean stopping) {
677
686
}
678
687
if (!stopping && this .connectedDocuments .isEmpty ()) {
679
688
if (this .serverDefinition .lastDocumentDisconnectedTimeout != 0 && !ApplicationManager .getApplication ().isUnitTestMode ()) {
680
- removeStopTimer ();
681
- startStopTimer ();
689
+ startStopTimerTask ();
682
690
} else {
683
691
stop ();
684
692
}
0 commit comments