Skip to content

Commit

Permalink
bugfix: Correctly choose the folder to use for a command
Browse files Browse the repository at this point in the history
This was actually my bad, since I noticed it while working on the migration to Scala 3, where this was actually reported as a compilation error.

Anyway, I added a test and did a small refactor so that it's easier to keep things consistent (such as expected title etc.)

Fixes #5611
  • Loading branch information
tgodzik committed Sep 4, 2023
1 parent f804c97 commit 53af3a8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object ServerCommands {

val DisconnectBuildServer = new Command(
"build-disconnect",
"Disconnect to build server",
"Disconnect from old build server",
"""Unconditionally cancel existing build server connection without reconnecting""",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,43 @@ class WorkspaceChoicePopup(
def interactiveChooseFolder(
actionName: String
)(implicit ec: ExecutionContext): Future[Option[MetalsLspService]] = {
def choicesParams(): ShowMessageRequestParams = {
val params = new ShowMessageRequestParams()
params.setMessage(
s"For which folder would you like to $actionName?"
)
params.setType(MessageType.Info)
params.setActions(
folders()
.map(folder => new MessageActionItem(folder.getVisibleName))
.asJava
)
params
}

val currentFolders = folders()
if (currentFolders.length == 1) Future.successful(currentFolders.headOption)
else {
languageClient
.showMessageRequest(choicesParams())
.showMessageRequest(
WorkspaceChoicePopup
.choicesParams(actionName, currentFolders.map(_.getVisibleName))
)
.asScala
.map { item => currentFolders.find(_.getVisibleName == item) }
.map { item =>
currentFolders.find(_.getVisibleName == item.getTitle())
}
}
}
}

object WorkspaceChoicePopup {
def choicesParams(
actionName: String,
folders: List[String],
): ShowMessageRequestParams = {
val params = new ShowMessageRequestParams()

val lowerCaseActionName =
if (actionName.nonEmpty) actionName.head.toLower + actionName.tail
else actionName

params.setMessage(
s"For which folder would you like to $lowerCaseActionName?"
)
params.setType(MessageType.Info)
params.setActions(
folders
.map(folder => new MessageActionItem(folder))
.asJava
)
params
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -642,29 +642,29 @@ class WorkspaceLspService(
case ServerCommands.RestartBuildServer() =>
onCurrentFolder(
_.restartBspServer().ignoreValue,
"restart BSP server",
ServerCommands.RestartBuildServer.title,
).asJavaObject
case ServerCommands.GenerateBspConfig() =>
onCurrentFolder(
_.generateBspConfig(),
"generate BSP config",
ServerCommands.GenerateBspConfig.title,
).asJavaObject
case ServerCommands.ImportBuild() =>
onCurrentFolder(
_.slowConnectToBuildServer(forceImport = true),
"import build",
ServerCommands.ImportBuild.title,
default = () => BuildChange.None,
).asJavaObject
case ServerCommands.ConnectBuildServer() =>
onCurrentFolder(
_.quickConnectToBuildServer(),
"connect build server",
ServerCommands.ConnectBuildServer.title,
default = () => BuildChange.None,
).asJavaObject
case ServerCommands.DisconnectBuildServer() =>
onCurrentFolder(
_.disconnectOldBuildServer(),
"disconnect old build server",
ServerCommands.DisconnectBuildServer.title,
).asJavaObject
case ServerCommands.DecodeFile(uri) =>
getServiceFor(uri).decodeFile(uri).asJavaObject
Expand Down Expand Up @@ -734,17 +734,26 @@ class WorkspaceLspService(
.asJava
}.asJavaObject
case ServerCommands.BspSwitch() =>
onCurrentFolder(_.switchBspServer(), "switch BSP server").asJavaObject
onCurrentFolder(
_.switchBspServer(),
ServerCommands.BspSwitch.title,
).asJavaObject
case ServerCommands.OpenIssue() =>
Future
.successful(Urls.openBrowser(githubNewIssueUrlCreator.buildUrl()))
.asJavaObject
case OpenBrowserCommand(url) =>
Future.successful(Urls.openBrowser(url)).asJavaObject
case ServerCommands.CascadeCompile() =>
onCurrentFolder(_.cascadeCompile(), "cascade compile").asJavaObject
onCurrentFolder(
_.cascadeCompile(),
ServerCommands.CascadeCompile.title,
).asJavaObject
case ServerCommands.CleanCompile() =>
onCurrentFolder(_.cleanCompile(), "clean compile").asJavaObject
onCurrentFolder(
_.cleanCompile(),
ServerCommands.CleanCompile.title,
).asJavaObject
case ServerCommands.CancelCompile() =>
foreachSeq(_.cancelCompile(), ignoreValue = true)
case ServerCommands.PresentationCompilerRestart() =>
Expand Down Expand Up @@ -799,7 +808,7 @@ class WorkspaceLspService(
)
)
},
"go to log",
ServerCommands.GotoLog.title,
).asJavaObject

case ServerCommands.StartDebugAdapter(params) if params.getData != null =>
Expand Down Expand Up @@ -901,14 +910,14 @@ class WorkspaceLspService(
)
onCurrentFolder(
_.interactivePopupChoiceReset(),
"interactive reset choice",
ServerCommands.ResetChoicePopup.title,
)
}).asJavaObject

case ServerCommands.ResetNotifications() =>
onCurrentFolder(
_.resetNotifications(),
"reset dismissed notifications",
ServerCommands.ResetNotifications.title,
).asJavaObject

case ServerCommands.NewScalaFile(args) =>
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/src/main/scala/tests/TestingClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import scala.meta.internal.metals.ClientCommands
import scala.meta.internal.metals.FileOutOfScalaCliBspScope
import scala.meta.internal.metals.Messages._
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ServerCommands
import scala.meta.internal.metals.ServerLivenessMonitor
import scala.meta.internal.metals.TextEdits
import scala.meta.internal.metals.WorkspaceChoicePopup
import scala.meta.internal.metals.clients.language.MetalsInputBoxParams
import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
import scala.meta.internal.metals.clients.language.MetalsSlowTaskParams
Expand Down Expand Up @@ -322,6 +324,13 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers)
)
}

def choicesMessage = WorkspaceChoicePopup
.choicesParams(
ServerCommands.ConnectBuildServer.title.toLowerCase(),
Nil,
)
.getMessage()

CompletableFuture.completedFuture {
messageRequests.addLast(params.getMessage)
showMessageRequestHandler(params).getOrElse {
Expand Down Expand Up @@ -360,6 +369,8 @@ class TestingClient(workspace: AbsolutePath, val buffers: Buffers)
)
) {
regenerateAndRestartScalaCliBuildSever
} else if (params.getMessage() == choicesMessage) {
params.getActions().asScala.head
} else {
throw new IllegalArgumentException(params.toString)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/src/test/scala/tests/WorkspaceFoldersSuite.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tests

import scala.meta.internal.metals.ServerCommands
import scala.meta.internal.metals.{BuildInfo => V}

class WorkspaceFoldersSuite
Expand Down Expand Up @@ -32,6 +33,15 @@ class WorkspaceFoldersSuite
expectError = false,
)
_ = assert(server.fullServer.folderServices.length == 2)
_ <- server.executeCommand(ServerCommands.ConnectBuildServer)
_ = assertNoDiff(
server.client.workspaceMessageRequests,
"For which folder would you like to connect to build server?",
)
_ = assertNoDiff(
server.client.workspaceShowMessages,
"",
)
_ = assertNoDiff(
server.workspaceSymbol("MyObject"),
s"""|a.MyObjectA
Expand Down

0 comments on commit 53af3a8

Please sign in to comment.