Skip to content

Commit

Permalink
store dont show bsp error in db
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Sep 29, 2023
1 parent 1dcbaeb commit bca9601
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- If should show bsp errors
create table show_bsp_errors(
should_show bit primary key
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import scala.meta.internal.metals.ClientCommands
import scala.meta.internal.metals.ConcurrentHashSet
import scala.meta.internal.metals.Directories
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Tables
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.io.AbsolutePath

Expand All @@ -20,19 +21,18 @@ import org.eclipse.{lsp4j => l}
class BspErrorHandler(
languageClient: MetalsLanguageClient,
workspaceFolder: AbsolutePath,
restartBspServer: () => Future[Boolean],
currentSession: () => Option[BspSession],
tables: Tables,
)(implicit context: ExecutionContext) {

protected def logsPath: AbsolutePath =
workspaceFolder.resolve(Directories.log)
private val lastError = new AtomicReference[String]("")
private val dismissedErrors = ConcurrentHashSet.empty[String]
@volatile private var doNotShowErrors = false

def onError(message: String): Future[Unit] = {
if (
!doNotShowErrors &&
tables.showBspErrors.shouldShow().getOrElse(true) &&
shouldShowBspError &&
!dismissedErrors.contains(message)
) {
Expand Down Expand Up @@ -63,12 +63,10 @@ class BspErrorHandler(
.flatMap(findLine(_))
.getOrElse(0)
Future.successful(gotoLogs(errorMsgStartLine))
case BspErrorHandler.restartBuildServer =>
restartBspServer().ignoreValue
case BspErrorHandler.dismiss =>
Future.successful(dismissedErrors.add(message)).ignoreValue
case BspErrorHandler.doNotShowErrors =>
Future.successful { doNotShowErrors = true }
Future.successful { tables.showBspErrors.set(false) }.ignoreValue
case _ => Future.successful(())
}
}
Expand Down Expand Up @@ -110,12 +108,12 @@ object BspErrorHandler {
if (message.length() <= MESSAGE_MAX_LENGTH) {
(
makeShortMessage(message),
List(restartBuildServer, dismiss, doNotShowErrors),
List(dismiss, doNotShowErrors),
)
} else {
(
makeLongMessage(message),
List(goToLogs, restartBuildServer, dismiss, doNotShowErrors),
List(goToLogs, dismiss, doNotShowErrors),
)
}
val params = new l.ShowMessageRequestParams()
Expand All @@ -135,8 +133,6 @@ object BspErrorHandler {

val goToLogs = new l.MessageActionItem("Go to logs.")
val dismiss = new l.MessageActionItem("Dismiss.")
val restartBuildServer =
new l.MessageActionItem("Restart build server.")
val doNotShowErrors = new l.MessageActionItem("Stop showing bsp errors.")

val errorHeader = "Encountered an error in the build server:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ class MetalsLspService(
new BspErrorHandler(
languageClient,
folder,
restartBspServer,
() => bspSession,
tables,
)

private val buildClient: ForwardingMetalsBuildClient =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scala.meta.internal.metals

import java.sql.Connection

import scala.meta.internal.metals.JdbcEnrichments._

class ShowBspErrors(conn: () => Connection) {
final val explicit = "EXPLICIT"

def shouldShow(): Option[Boolean] = {
conn()
.query(
"select should_show from show_bsp_errors"
)(_ => ()) { rs => rs.getBoolean(1) }
.headOption
}

def set(shouldShowBspError: Boolean): Int =
conn().update(
s"merge into show_bsp_errors values ?;"
) { _.setBoolean(1, shouldShowBspError) }

def reset(): Unit = conn().update("delete from show_bsp_errors")(_ => ())
}
2 changes: 2 additions & 0 deletions metals/src/main/scala/scala/meta/internal/metals/Tables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class Tables(
new ChosenBuildTool(() => connection)
val fingerprints =
new Fingerprints(() => connection)
val showBspErrors =
new ShowBspErrors(() => connection)

private val ref: AtomicReference[ConnectionState] =
new AtomicReference(ConnectionState.Empty)
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/src/test/scala/tests/BspErrorHandlerSuite.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package tests

import java.nio.file.Paths
import java.util.concurrent.Executors

import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import scala.meta.internal.builds.BspErrorHandler
import scala.meta.internal.metals.Buffers
import scala.meta.internal.metals.Directories
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Tables
import scala.meta.io.AbsolutePath

import com.google.gson.JsonObject
import org.eclipse.lsp4j.MessageActionItem

class BspErrorHandlerSuite extends BaseSuite {
class BspErrorHandlerSuite extends BaseTablesSuite {
implicit val ec: ExecutionContext =
ExecutionContext.fromExecutorService(Executors.newCachedThreadPool())

val workspace: AbsolutePath = AbsolutePath(Paths.get("."))
.resolve("target/bsp-error-suite/")
.createDirectories()
val client = new TestingClient(workspace, Buffers())
val errorHandler = new TestBspErrorHandler(client, workspace)
val exampleError1 = "an error"
val exampleError2 = "a different error"
val longError: String =
Expand All @@ -34,14 +30,16 @@ class BspErrorHandlerSuite extends BaseSuite {
|""".stripMargin

test("handle-bsp-error") {
val errorHandler = new TestBspErrorHandler(client, workspace, tables)

FileLayout.fromString(
s"""|/.metals/metals.log
|
|""".stripMargin,
workspace,
)

client.bspError = BspErrorHandler.restartBuildServer
client.bspError = new MessageActionItem("OK")

for {
_ <- errorHandler.onError(exampleError1)
Expand Down Expand Up @@ -97,12 +95,13 @@ class BspErrorHandlerSuite extends BaseSuite {
class TestBspErrorHandler(
val languageClient: TestingClient,
workspaceFolder: AbsolutePath,
tables: Tables,
)(implicit context: ExecutionContext)
extends BspErrorHandler(
languageClient,
workspaceFolder,
() => Future.successful(true),
() => None,
tables,
) {
override def shouldShowBspError: Boolean = true

Expand Down

0 comments on commit bca9601

Please sign in to comment.