diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/CancellationSupport.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/CancellationSupport.java index 3d8c28073..e97f81ce2 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/CancellationSupport.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/CancellationSupport.java @@ -10,11 +10,12 @@ ******************************************************************************/ package com.redhat.devtools.intellij.lsp4ij.internal; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArrayList; +import com.intellij.openapi.progress.ProcessCanceledException; import org.eclipse.lsp4j.jsonrpc.CancelChecker; /** @@ -32,12 +33,17 @@ public class CancellationSupport implements CancelChecker { private boolean cancelled; public CancellationSupport() { - this.futuresToCancel = new ArrayList>(); + this.futuresToCancel = new CopyOnWriteArrayList<>(); this.cancelled = false; } public CompletableFuture execute(CompletableFuture future) { - this.futuresToCancel.add(future); + if (cancelled) { + future.cancel(true); + throw new ProcessCanceledException(); + } else { + this.futuresToCancel.add(future); + } return future; } @@ -45,7 +51,7 @@ public CompletableFuture execute(CompletableFuture future) { * Cancel all LSP requests. */ public void cancel() { - if (cancelled == true) { + if (cancelled) { return; } this.cancelled = true; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java index be2d6f00d..db28edcb8 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java @@ -73,7 +73,7 @@ private List getTargets(Editor editor, PsiFile file) { .map(languageServer -> cancellationSupport.execute(languageServer.getServer().getTextDocumentService().documentHighlight(params))) .map(request -> request.thenAcceptAsync(result -> { if (result != null) { - result.forEach(hightlight -> highlights.add(hightlight)); + highlights.addAll(result); } })).toArray(CompletableFuture[]::new)))); while (!future.isDone() || !highlights.isEmpty()) {