Skip to content

Commit

Permalink
fix: ignore cancel exception while formatting
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Feb 10, 2025
1 parent 3878f66 commit fff525c
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ public void format(@NotNull Document document,
CompletableFuture<List<? extends TextEdit>> formatFuture = this.getFeatureData(params);
try {
waitUntilDone(formatFuture, getFile());
} catch (
ProcessCanceledException e) {//Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility
//TODO delete block when minimum required version is 2024.2
handleError(formattingRequest, e);
return;
} catch (CancellationException e) {
// cancel the LSP requests textDocument/formatting / textDocument/rangeFormatting
handleError(formattingRequest, e);
return;
} catch (ExecutionException e) {
Throwable cause = e.getCause();
handleError(formattingRequest, cause);
handleError(formattingRequest, cause != null ? cause : e);
return;
}
try {
Expand All @@ -71,12 +80,13 @@ public void format(@NotNull Document document,
}
}

private static void handleError(@NotNull AsyncFormattingRequest formattingRequest, Throwable error) {
private static void handleError(@NotNull AsyncFormattingRequest formattingRequest,
@NotNull Throwable error) {
if (error instanceof ProcessCanceledException || error instanceof CancellationException) {
// Ignore the error
formattingRequest.onTextReady(formattingRequest.getDocumentText());
} else {
formattingRequest.onError("LSP formatting error", error.getMessage());
formattingRequest.onError("LSP formatting error", error.getMessage() != null ? error.getMessage() : error.getClass().getName());
}
}

Expand Down

0 comments on commit fff525c

Please sign in to comment.