From 07f75c4f51f350757df97392b23be25334f99858 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Thu, 19 Oct 2023 16:42:57 +0200 Subject: [PATCH] refactor: move and rename bsp server status --- .../metals/BuildServerConnection.scala | 2 +- .../internal/metals/ConnectionBspStatus.scala | 50 ++++++++++++++++++ .../metals/ServerLivenessMonitor.scala | 52 +------------------ .../src/main/scala/tests/TestingClient.scala | 4 +- .../tests/ServerLivenessMonitorLspSuite.scala | 4 +- .../tests/ServerLivenessMonitorSuite.scala | 6 +-- 6 files changed, 60 insertions(+), 58 deletions(-) create mode 100644 metals/src/main/scala/scala/meta/internal/metals/ConnectionBspStatus.scala diff --git a/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala b/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala index b07c8d1cc63..46f9364836b 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala @@ -456,7 +456,7 @@ object BuildServerConnection { val tracePrinter = Trace.setupTracePrinter("BSP", bspTraceRoot) val bspStatusOpt = if (addLivenessMonitor) - Some(new BspStatus(languageClient, serverName, config.icons)) + Some(new ConnectionBspStatus(languageClient, serverName, config.icons)) else None val requestMonitorOpt = bspStatusOpt.map(new RequestMonitorImpl(_)) diff --git a/metals/src/main/scala/scala/meta/internal/metals/ConnectionBspStatus.scala b/metals/src/main/scala/scala/meta/internal/metals/ConnectionBspStatus.scala new file mode 100644 index 00000000000..e9bec2c11af --- /dev/null +++ b/metals/src/main/scala/scala/meta/internal/metals/ConnectionBspStatus.scala @@ -0,0 +1,50 @@ +package scala.meta.internal.metals + +import java.util.concurrent.atomic.AtomicBoolean + +import scala.meta.internal.metals.clients.language.MetalsLanguageClient +import scala.meta.internal.metals.clients.language.MetalsStatusParams +import scala.meta.internal.metals.clients.language.StatusType + +class ConnectionBspStatus( + client: MetalsLanguageClient, + serverName: String, + icons: Icons, +) { + private val isServerResponsive = new AtomicBoolean(false) + + def connected(): Unit = + if (isServerResponsive.compareAndSet(false, true)) + client.metalsStatus(ConnectionBspStatus.connectedParams(serverName, icons)) + def noResponse(): Unit = + if (isServerResponsive.compareAndSet(true, false)) { + scribe.debug("server liveness monitor detected no response") + client.metalsStatus(ConnectionBspStatus.noResponseParams(serverName, icons)) + } + def disconnected(): Unit = client.metalsStatus(ConnectionBspStatus.disconnectedParams) + + def isBuildServerResponsive: Boolean = isServerResponsive.get() +} + +object ConnectionBspStatus { + def connectedParams(serverName: String, icons: Icons): MetalsStatusParams = + MetalsStatusParams( + s"$serverName ${icons.link}", + "info", + show = true, + tooltip = s"Metals is connected to the build server ($serverName).", + ).withStatusType(StatusType.bsp) + + val disconnectedParams: MetalsStatusParams = + MetalsStatusParams("", hide = true).withStatusType(StatusType.bsp) + + def noResponseParams(serverName: String, icons: Icons): MetalsStatusParams = + MetalsStatusParams( + s"$serverName ${icons.error}", + "error", + show = true, + tooltip = s"Build sever ($serverName) is not responding.", + command = ServerCommands.ConnectBuildServer.id, + commandTooltip = "Reconnect.", + ).withStatusType(StatusType.bsp) +} diff --git a/metals/src/main/scala/scala/meta/internal/metals/ServerLivenessMonitor.scala b/metals/src/main/scala/scala/meta/internal/metals/ServerLivenessMonitor.scala index a5ae48829fd..3653180622f 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ServerLivenessMonitor.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ServerLivenessMonitor.scala @@ -4,14 +4,9 @@ import java.util.concurrent.Executors import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledFuture import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicBoolean import scala.concurrent.duration.Duration -import scala.meta.internal.metals.clients.language.MetalsLanguageClient -import scala.meta.internal.metals.clients.language.MetalsStatusParams -import scala.meta.internal.metals.clients.language.StatusType - import org.eclipse.lsp4j.jsonrpc.MessageConsumer import org.eclipse.lsp4j.jsonrpc.messages.Message import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage @@ -23,7 +18,7 @@ trait RequestMonitor { def lastIncoming: Option[Long] } -class RequestMonitorImpl(bspStatus: BspStatus) extends RequestMonitor { +class RequestMonitorImpl(bspStatus: ConnectionBspStatus) extends RequestMonitor { @volatile private var lastOutgoing_ : Option[Long] = None @volatile private var lastIncoming_ : Option[Long] = None @@ -59,7 +54,7 @@ class ServerLivenessMonitor( ping: () => Unit, metalsIdleInterval: Duration, pingInterval: Duration, - bspStatus: BspStatus, + bspStatus: ConnectionBspStatus, ) { @volatile private var lastPing: Long = 0 val scheduler: ScheduledExecutorService = Executors.newScheduledThreadPool(1) @@ -125,46 +120,3 @@ object ServerLivenessMonitor { object Running extends State } - -class BspStatus( - client: MetalsLanguageClient, - serverName: String, - icons: Icons, -) { - private val isServerResponsive = new AtomicBoolean(false) - - def connected(): Unit = - if (isServerResponsive.compareAndSet(false, true)) - client.metalsStatus(BspStatus.connectedParams(serverName, icons)) - def noResponse(): Unit = - if (isServerResponsive.compareAndSet(true, false)) { - scribe.debug("server liveness monitor detected no response") - client.metalsStatus(BspStatus.noResponseParams(serverName, icons)) - } - def disconnected(): Unit = client.metalsStatus(BspStatus.disconnectedParams) - - def isBuildServerResponsive: Boolean = isServerResponsive.get() -} - -object BspStatus { - def connectedParams(serverName: String, icons: Icons): MetalsStatusParams = - MetalsStatusParams( - s"$serverName ${icons.link}", - "info", - show = true, - tooltip = s"Metals is connected to the build server ($serverName).", - ).withStatusType(StatusType.bsp) - - val disconnectedParams: MetalsStatusParams = - MetalsStatusParams("", hide = true).withStatusType(StatusType.bsp) - - def noResponseParams(serverName: String, icons: Icons): MetalsStatusParams = - MetalsStatusParams( - s"$serverName ${icons.error}", - "error", - show = true, - tooltip = s"Build sever ($serverName) is not responding.", - command = ServerCommands.ConnectBuildServer.id, - commandTooltip = "Reconnect.", - ).withStatusType(StatusType.bsp) -} diff --git a/tests/unit/src/main/scala/tests/TestingClient.scala b/tests/unit/src/main/scala/tests/TestingClient.scala index eaa0f623542..2edbcaf5939 100644 --- a/tests/unit/src/main/scala/tests/TestingClient.scala +++ b/tests/unit/src/main/scala/tests/TestingClient.scala @@ -16,9 +16,9 @@ import scala.meta.internal.builds.BspErrorHandler import scala.meta.internal.builds.BuildTool import scala.meta.internal.builds.BuildTools import scala.meta.internal.decorations.PublishDecorationsParams -import scala.meta.internal.metals.BspStatus import scala.meta.internal.metals.Buffers import scala.meta.internal.metals.ClientCommands +import scala.meta.internal.metals.ConnectionBspStatus import scala.meta.internal.metals.FileOutOfScalaCliBspScope import scala.meta.internal.metals.Icons import scala.meta.internal.metals.Messages._ @@ -373,7 +373,7 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers) ) { bspError } else if ( - params.getMessage() == BspStatus + params.getMessage() == ConnectionBspStatus .noResponseParams("Bill", Icons.default) .logMessage(Icons.default) ) { diff --git a/tests/unit/src/test/scala/tests/ServerLivenessMonitorLspSuite.scala b/tests/unit/src/test/scala/tests/ServerLivenessMonitorLspSuite.scala index 3fcc0b08d7a..9ef6907d1cd 100644 --- a/tests/unit/src/test/scala/tests/ServerLivenessMonitorLspSuite.scala +++ b/tests/unit/src/test/scala/tests/ServerLivenessMonitorLspSuite.scala @@ -2,7 +2,7 @@ package tests import scala.concurrent.duration.Duration -import scala.meta.internal.metals.BspStatus +import scala.meta.internal.metals.ConnectionBspStatus import scala.meta.internal.metals.Icons import scala.meta.internal.metals.Messages import scala.meta.internal.metals.MetalsEnrichments._ @@ -60,7 +60,7 @@ class ServerLivenessMonitorLspSuite extends BaseLspSuite("liveness-monitor") { ) ) _ = Thread.sleep(sleepTime) - noResponseParams = BspStatus.noResponseParams( + noResponseParams = ConnectionBspStatus.noResponseParams( "Bill", Icons.default, ) diff --git a/tests/unit/src/test/scala/tests/ServerLivenessMonitorSuite.scala b/tests/unit/src/test/scala/tests/ServerLivenessMonitorSuite.scala index 7b60b765f4b..e609b400e3b 100644 --- a/tests/unit/src/test/scala/tests/ServerLivenessMonitorSuite.scala +++ b/tests/unit/src/test/scala/tests/ServerLivenessMonitorSuite.scala @@ -8,7 +8,7 @@ import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContextExecutorService import scala.concurrent.duration.Duration -import scala.meta.internal.metals.BspStatus +import scala.meta.internal.metals.ConnectionBspStatus import scala.meta.internal.metals.Icons import scala.meta.internal.metals.RequestMonitor import scala.meta.internal.metals.ServerLivenessMonitor @@ -23,7 +23,7 @@ class ServerLivenessMonitorSuite extends BaseSuite { val pingInterval = Duration("3s") val server = new ResponsiveServer(pingInterval) val client = new CountMessageRequestsClient - val bspStatus = new BspStatus(client, "responsive-server", Icons.default) + val bspStatus = new ConnectionBspStatus(client, "responsive-server", Icons.default) val livenessMonitor = new ServerLivenessMonitor( server, () => server.sendRequest(true), @@ -89,7 +89,7 @@ class CountMessageRequestsClient extends NoopLanguageClient { override def metalsStatus(params: MetalsStatusParams): Unit = if ( - params == BspStatus.noResponseParams("responsive-server", Icons.default) + params == ConnectionBspStatus.noResponseParams("responsive-server", Icons.default) ) { showMessageRequests += 1 }