Skip to content

Commit

Permalink
handle restart after timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 10, 2023
1 parent 52138d0 commit 6ff50be
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
12 changes: 11 additions & 1 deletion metals/src/main/scala/scala/meta/internal/bsp/BspConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BspConnector(
statusBar: StatusBar,
bspConfigGenerator: BspConfigGenerator,
currentConnection: () => Option[BuildServerConnection],
restartBspServer: () => Future[Boolean],
)(implicit ec: ExecutionContext) {

/**
Expand Down Expand Up @@ -100,10 +101,19 @@ class BspConnector(
if details.getName() == SbtBuildTool.name =>
tables.buildServers.chooseServer(SbtBuildTool.name)
val shouldReload = SbtBuildTool.writeSbtMetalsPlugins(projectRoot)
def restartSbtBuildServer() = currentConnection()
.withFilter(_.isSbt)
.map(_ => restartBspServer().ignoreValue)
.getOrElse(Future.successful(()))
val connectionF =
for {
_ <- SbtBuildTool(projectRoot, () => userConfiguration)
.ensureCorrectJavaVersion(shellRunner, projectRoot, client)
.ensureCorrectJavaVersion(
shellRunner,
projectRoot,
client,
restartSbtBuildServer,
)
connection <- bspServers.newServer(
projectRoot,
bspTraceRoot,
Expand Down
33 changes: 23 additions & 10 deletions metals/src/main/scala/scala/meta/internal/builds/SbtBuildTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.util.concurrent.TimeoutException

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

import scala.meta.inputs.Input
import scala.meta.internal.metals.MetalsEnrichments._
Expand Down Expand Up @@ -196,20 +197,32 @@ case class SbtBuildTool(
shellRunner: ShellRunner,
workspace: AbsolutePath,
languageClient: LanguageClient,
restartSbtBuildServer: () => Future[Unit],
)(implicit ex: ExecutionContext): Future[Unit] =
if (checkCorrectJavaVersion(workspace, userConfig().javaHome)) {
Future.successful(())
} else {
languageClient
.showMessageRequest(Messages.SbtServerJavaHomeUpdate.params())
.asScala
.flatMap {
case Messages.SbtServerJavaHomeUpdate.restart =>
shutdownBspServer(shellRunner).ignoreValue
case _ => Future.successful(())
}
.withTimeout(10, TimeUnit.SECONDS)
.recover { case _: TimeoutException => Future.successful(()) }
val promise = Promise[Unit]()
val future: Future[Unit] =
languageClient
.showMessageRequest(Messages.SbtServerJavaHomeUpdate.params())
.asScala
.flatMap {
case Messages.SbtServerJavaHomeUpdate.restart =>
if (promise.isCompleted) {
// executes when user chooses `restart` after the timeout
restartSbtBuildServer()
} else shutdownBspServer(shellRunner).ignoreValue
case _ =>
promise.trySuccess(())
Future.successful(())
}
.withTimeout(15, TimeUnit.SECONDS)
.recover { case _: TimeoutException =>
Future.successful(())
}
future.onComplete(promise.tryComplete(_))
promise.future
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ class MetalsLspService(
statusBar,
bspConfigGenerator,
() => bspSession.map(_.mainConnection),
restartBspServer,
)

private val workspaceSymbols: WorkspaceSymbolProvider =
Expand Down

0 comments on commit 6ff50be

Please sign in to comment.