From 5e3d0c4bf8ae2a4f7bc0676914d60520ff727ef5 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 20 May 2024 10:36:31 +0800 Subject: [PATCH] Fix: Append the log message to build output if it's from compilation report. - Store the id of the compilation task in an LRU cache. - Append the log message to the build output if it's from the compilation task. Signed-off-by: Sheng Chen --- .../gradle/bs/importer/GradleBuildClient.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java index fccb741f6..ff5e7d6bd 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Date; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -32,6 +33,7 @@ import ch.epfl.scala.bsp4j.MessageType; import ch.epfl.scala.bsp4j.PublishDiagnosticsParams; import ch.epfl.scala.bsp4j.ShowMessageParams; +import ch.epfl.scala.bsp4j.StatusCode; import ch.epfl.scala.bsp4j.TaskDataKind; import ch.epfl.scala.bsp4j.TaskFinishParams; import ch.epfl.scala.bsp4j.TaskProgressParams; @@ -60,6 +62,8 @@ public class GradleBuildClient implements BuildClient { private final JavaLanguageClient lsClient; + private final LruCache failedTaskCache = new LruCache<>(16); + public GradleBuildClient() { this.lsClient = JavaLanguageServerPlugin.getProjectsManager().getConnection(); } @@ -70,8 +74,12 @@ public void onBuildLogMessage(LogMessageParams params) { if (type == MessageType.LOG) { Utils.sendTelemetry(this.lsClient, params.getMessage()); } else { - this.lsClient.sendNotification(new ExecuteCommandParams(CLIENT_BUILD_LOG_CMD, - Arrays.asList(params.getMessage()))); + String command = CLIENT_BUILD_LOG_CMD; + if (type == MessageType.ERROR && failedTaskCache.contains(params.getTask().getId())) { + // append the compilation failure message to the build output channel. + command = CLIENT_APPEND_BUILD_LOG_CMD; + } + this.lsClient.sendNotification(new ExecuteCommandParams(command, Arrays.asList(params.getMessage()))); } } @@ -154,6 +162,9 @@ public void onBuildTaskFinish(TaskFinishParams params) { if (Objects.equals(params.getDataKind(), TaskDataKind.COMPILE_REPORT)) { String msg = params.getMessage() + "\n------\n"; lsClient.sendNotification(new ExecuteCommandParams(CLIENT_APPEND_BUILD_LOG_CMD, Arrays.asList(msg))); + if (params.getStatus() == StatusCode.ERROR) { + failedTaskCache.addAll((params.getTaskId().getParents())); + } } else { Either id = Either.forLeft(params.getTaskId().getId()); WorkDoneProgressEnd workDoneProgressEnd = new WorkDoneProgressEnd(); @@ -162,4 +173,22 @@ public void onBuildTaskFinish(TaskFinishParams params) { lsClient.notifyProgress(new ProgressParams(id, Either.forLeft(workDoneProgressEnd))); } } + + private class LruCache extends LinkedHashSet { + private final int maxSize; + + public LruCache(int maxSize) { + super(maxSize); + this.maxSize = maxSize; + } + + @Override + public boolean add(T element) { + if (size() >= maxSize) { + T oldestElement = iterator().next(); + remove(oldestElement); + } + return super.add(element); + } + } }