From 716ae39447c71c6cf6f5b618167678867e7dbbe8 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Tue, 18 Jul 2023 17:54:36 +0200 Subject: [PATCH] improvement: Strip ANSI coloring from BazelBsp logs --- .../metals/ForwardingMetalsBuildClient.scala | 25 +++++++++++-------- .../internal/metals/MetalsEnrichments.scala | 3 +++ .../scala/tests/bazel/BazelLspSuite.scala | 4 ++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala b/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala index c6ed5d98969..911f28fa6a4 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala @@ -94,17 +94,22 @@ final class ForwardingMetalsBuildClient( def onBuildShowMessage(params: l.MessageParams): Unit = languageClient.showMessage(params) - def onBuildLogMessage(params: l.MessageParams): Unit = - params.getType match { - case l.MessageType.Error => - scribe.error(params.getMessage) - case l.MessageType.Warning => - scribe.warn(params.getMessage) - case l.MessageType.Info => - scribe.info(params.getMessage) - case l.MessageType.Log => - scribe.info(params.getMessage) + def onBuildLogMessage(params: l.MessageParams): Unit = { + // NOTE: BazelBsp adds coloring to the log message after `workspaceBuildTargets` request + val noANSICodes = filterANSICodes(params.getMessage).trim() + if (noANSICodes.nonEmpty) { + params.getType match { + case l.MessageType.Error => + scribe.error(noANSICodes) + case l.MessageType.Warning => + scribe.warn(noANSICodes) + case l.MessageType.Info => + scribe.info(noANSICodes) + case l.MessageType.Log => + scribe.info(noANSICodes) + } } + } def onBuildPublishDiagnostics(params: b.PublishDiagnosticsParams): Unit = { diagnostics.onBuildPublishDiagnostics(params) diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala index 1e63c30db56..965e9f31b29 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala @@ -1082,4 +1082,7 @@ object MetalsEnrichments def filerANSIColorCodes(str: String): String = str.replaceAll("\u001B\\[[;\\d]*m", "") + def filterANSICodes(str: String): String = + str.replaceAll("\u001b\\[1A\u001b\\[K|\u001B\\[[;\\d]*m", "") + } diff --git a/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala b/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala index c8359197750..6f2266d5b51 100644 --- a/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala +++ b/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala @@ -28,7 +28,9 @@ class BazelLspSuite test("basic") { cleanWorkspace() for { - _ <- initialize(BazelBuildLayout(workspaceLayout, scalaVersion, bazelVersion)) + _ <- initialize( + BazelBuildLayout(workspaceLayout, scalaVersion, bazelVersion) + ) _ = assertNoDiff( client.workspaceMessageRequests, List(