From eb6eae1cdb816d016b18e946ca433f97944deadd Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 27 Sep 2023 16:15:08 +0200 Subject: [PATCH] chore: Adjust implementation to recent bsp protocol changes --- .../metals/BuildServerConnection.scala | 2 +- .../metals/ForwardingMetalsBuildClient.scala | 4 +- .../metals/codelenses/RunTestCodeLens.scala | 7 +- .../internal/metals/debug/DebugProvider.scala | 98 +++++++++++-------- .../testProvider/TestSuitesProvider.scala | 10 +- tests/unit/src/main/scala/bill/Bill.scala | 12 ++- .../scala/tests/MetalsTestEnrichments.scala | 7 +- .../src/main/scala/tests/TestingServer.scala | 5 +- .../troubleshoot/ProblemResolverSuite.scala | 7 +- 9 files changed, 94 insertions(+), 58 deletions(-) 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 bb11df07f28..964eee0adae 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/BuildServerConnection.scala @@ -207,7 +207,7 @@ class BuildServerConnection private ( connection.flatMap { conn => if (conn.capabilities.getJvmRunEnvironmentProvider()) { register( - server => server.jvmRunEnvironment(params), + server => server.buildTargetJvmRunEnvironment(params), onFail = Some( ( empty, 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 a7238847985..9a8c29178f8 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ForwardingMetalsBuildClient.scala @@ -121,7 +121,7 @@ final class ForwardingMetalsBuildClient( @JsonNotification("build/taskStart") def buildTaskStart(params: TaskStartParams): Unit = { params.getDataKind match { - case TaskDataKind.COMPILE_TASK => + case TaskStartDataKind.COMPILE_TASK => if ( params.getMessage != null && params.getMessage.startsWith("Compiling") ) { @@ -159,7 +159,7 @@ final class ForwardingMetalsBuildClient( @JsonNotification("build/taskFinish") def buildTaskFinish(params: TaskFinishParams): Unit = { params.getDataKind match { - case TaskDataKind.COMPILE_REPORT => + case TaskFinishDataKind.COMPILE_REPORT => for { report <- params.asCompileReport compilation <- compilations.remove(report.getTarget) diff --git a/metals/src/main/scala/scala/meta/internal/metals/codelenses/RunTestCodeLens.scala b/metals/src/main/scala/scala/meta/internal/metals/codelenses/RunTestCodeLens.scala index c06b64af864..ac5cf3e3b01 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/codelenses/RunTestCodeLens.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/codelenses/RunTestCodeLens.scala @@ -277,7 +277,7 @@ final class RunTestCodeLens( className: String, ): List[l.Command] = { val params = { - val dataKind = b.DebugSessionParamsDataKind.SCALA_TEST_SUITES + val dataKind = b.TestParamsDataKind.SCALA_TEST_SUITES val data = singletonList(className).toJson sessionParams(target, dataKind, data) } @@ -322,7 +322,10 @@ final class RunTestCodeLens( dataKind: String, data: JsonElement, ): b.DebugSessionParams = { - new b.DebugSessionParams(List(target).asJava, dataKind, data) + val params = new b.DebugSessionParams(List(target).asJava) + params.setDataKind(dataKind) + params.setData(data) + params } private def command( diff --git a/metals/src/main/scala/scala/meta/internal/metals/debug/DebugProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/debug/DebugProvider.scala index e72c9df1bed..604c37b81ba 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/debug/DebugProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/debug/DebugProvider.scala @@ -272,11 +272,10 @@ class DebugProvider( ) envFromFile(envFile).map { envFromFile => main.setEnvironmentVariables((envFromFile ::: env).asJava) - new b.DebugSessionParams( - singletonList(target), - b.DebugSessionParamsDataKind.SCALA_MAIN_CLASS, - main.toJson, - ) + val params = new b.DebugSessionParams(singletonList(target)) + params.setDataKind(b.DebugSessionParamsDataKind.SCALA_MAIN_CLASS) + params.setData(main.toJson) + params } } @@ -360,13 +359,13 @@ class DebugProvider( if (mains.nonEmpty) { verifyMain(buildTarget, mains.toList, params) } else if (tests.nonEmpty) { - Future( - new b.DebugSessionParams( - singletonList(buildTarget), - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES, - tests.asJava.toJson, - ) - ) + Future { + val params = new b.DebugSessionParams(singletonList(buildTarget)) + params.setDataKind(b.TestParamsDataKind.SCALA_TEST_SUITES) + params.setData(tests.asJava.toJson) + params + } + } else { Future.failed(NoRunOptionException) } @@ -498,23 +497,29 @@ class DebugProvider( } } .map { tests => - new b.DebugSessionParams( - singletonList(target), - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES, - tests.asJava.toJson, + val params = new b.DebugSessionParams( + singletonList(target) + ) + params.setDataKind( + b.TestParamsDataKind.SCALA_TEST_SUITES ) + params.setData(tests.asJava.toJson) + params } case (Some(TestTarget), Some(target)) => Future { - new b.DebugSessionParams( - singletonList(target), - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES, + val params = new b.DebugSessionParams( + singletonList(target) + ) + params.setDataKind(b.TestParamsDataKind.SCALA_TEST_SUITES) + params.setData( testClasses(target).values .map(_.fullyQualifiedName) .toList .asJava - .toJson, + .toJson ) + params } } @@ -605,11 +610,14 @@ class DebugProvider( Option(params.jvmOptions).getOrElse(Nil.asJava), (envFromFile ::: env).asJava, ) - new b.DebugSessionParams( - singletonList(target.getId()), - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES_SELECTION, - scalaTestSuite.toJson, + val debugParams = new b.DebugSessionParams( + singletonList(target.getId()) ) + debugParams.setDataKind( + b.TestParamsDataKind.SCALA_TEST_SUITES_SELECTION + ) + debugParams.setData(scalaTestSuite.toJson) + debugParams } // should not really happen due to // `findMainClassAndItsBuildTarget` succeeding with non-empty list @@ -622,13 +630,14 @@ class DebugProvider( def createDebugSession( target: b.BuildTargetIdentifier ): Future[DebugSessionParams] = - Future.successful( - new b.DebugSessionParams( - singletonList(target), - b.DebugSessionParamsDataKind.SCALA_ATTACH_REMOTE, - ().toJson, + Future.successful { + val params = new b.DebugSessionParams( + singletonList(target) ) - ) + params.setDataKind(b.DebugSessionParamsDataKind.SCALA_ATTACH_REMOTE) + params.setData(().toJson) + params + } def startTestSuite( buildTarget: b.BuildTarget, @@ -644,17 +653,22 @@ class DebugProvider( suite.copy(tests = suite.tests.map(_.replace("$", "\\$"))) else suite }) - new b.DebugSessionParams( - singletonList(buildTarget.getId), - DebugProvider.ScalaTestSelection, - testSuites.toJson, + val params = new b.DebugSessionParams( + singletonList(buildTarget.getId) ) - } else - new b.DebugSessionParams( - singletonList(buildTarget.getId), - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES, - request.requestData.suites.map(_.className).toJson, + params.setDataKind( + b.TestParamsDataKind.SCALA_TEST_SUITES_SELECTION ) + params.setData(testSuites.toJson) + params + } else { + val params = new b.DebugSessionParams( + singletonList(buildTarget.getId) + ) + params.setDataKind(b.TestParamsDataKind.SCALA_TEST_SUITES) + params.setData(request.requestData.suites.map(_.className).toJson) + params + } Future.successful(debugSession) } for { @@ -728,11 +742,11 @@ class DebugProvider( parameters.getDataKind match { case b.DebugSessionParamsDataKind.SCALA_MAIN_CLASS => json.as[b.ScalaMainClass].map(_.getClassName) - case b.DebugSessionParamsDataKind.SCALA_TEST_SUITES => + case b.TestParamsDataKind.SCALA_TEST_SUITES => json.as[ju.List[String]].map(_.asScala.sorted.mkString(";")) case b.DebugSessionParamsDataKind.SCALA_ATTACH_REMOTE => Success("attach-remote-debug-session") - case DebugProvider.ScalaTestSelection => + case b.TestParamsDataKind.SCALA_TEST_SUITES_SELECTION => json.as[ScalaTestSuites].map { params => params.suites.asScala .map(suite => @@ -910,8 +924,6 @@ object DebugProvider { } } - val ScalaTestSelection = "scala-test-suites-selection" - case object WorkspaceErrorsException extends Exception( s"Cannot run class, since the workspace has errors." diff --git a/metals/src/main/scala/scala/meta/internal/metals/testProvider/TestSuitesProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/testProvider/TestSuitesProvider.scala index 5f4fbccea55..766764b8241 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/testProvider/TestSuitesProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/testProvider/TestSuitesProvider.scala @@ -139,9 +139,13 @@ final class TestSuitesProvider( cases.asScala.flatMap { entry => val c = ScalaTestSuiteSelection(fqn, List(entry.name).asJava) val params = new b.DebugSessionParams( - List(target).asJava, - b.DebugSessionParamsDataKind.SCALA_TEST_SUITES_SELECTION, - ScalaTestSuites(List(c).asJava, Nil.asJava, Nil.asJava).toJson, + List(target).asJava + ) + params.setDataKind( + b.TestParamsDataKind.SCALA_TEST_SUITES_SELECTION + ) + params.setData( + ScalaTestSuites(List(c).asJava, Nil.asJava, Nil.asJava).toJson ) def lens(name: String, cmd: BaseCommand) = new l.CodeLens( entry.location.getRange(), diff --git a/tests/unit/src/main/scala/bill/Bill.scala b/tests/unit/src/main/scala/bill/Bill.scala index e2ab884d344..276eb293ebf 100644 --- a/tests/unit/src/main/scala/bill/Bill.scala +++ b/tests/unit/src/main/scala/bill/Bill.scala @@ -75,7 +75,7 @@ object Bill { private var sleepBeforePingResponse: Option[Duration] = None val languages: util.List[String] = Collections.singletonList("scala") var client: BuildClient = _ - override def onConnectWithClient(server: BuildClient): Unit = + def onConnectWithClient(server: BuildClient): Unit = client = server var workspace: Path = Paths.get(".").toAbsolutePath.normalize() @@ -126,7 +126,11 @@ object Bill { scalaJars, ) val id = new BuildTargetIdentifier("id") - val capabilities = new BuildTargetCapabilities(true, false, false) + val capabilities = new BuildTargetCapabilities() + capabilities.setCanCompile(true) + capabilities.setCanDebug(false) + capabilities.setCanRun(false) + capabilities.setCanTest(false) val result = new BuildTarget( id, Collections.singletonList("tag"), @@ -354,7 +358,9 @@ object Bill { CompletableFuture.completedFuture { val count = RecursivelyDelete(out) val plural = if (count != 1) "s" else "" - new CleanCacheResult(s"deleted $count file$plural", true) + val result = new CleanCacheResult(true) + result.setMessage(s"deleted $count file$plural") + result } } diff --git a/tests/unit/src/main/scala/tests/MetalsTestEnrichments.scala b/tests/unit/src/main/scala/tests/MetalsTestEnrichments.scala index 135dc5b66b5..06cb7f64e54 100644 --- a/tests/unit/src/main/scala/tests/MetalsTestEnrichments.scala +++ b/tests/unit/src/main/scala/tests/MetalsTestEnrichments.scala @@ -86,12 +86,17 @@ object MetalsTestEnrichments { } ) val bti = new BuildTargetIdentifier("workspace") + val capabilities = new BuildTargetCapabilities() + capabilities.setCanCompile(true) + capabilities.setCanDebug(true) + capabilities.setCanRun(true) + capabilities.setCanTest(true) val buildTarget = new BuildTarget( bti, Nil.asJava, Nil.asJava, Nil.asJava, - new BuildTargetCapabilities(true, true, true), + capabilities, ) val scalaTarget = new ScalaBuildTarget( "org.scala-lang", diff --git a/tests/unit/src/main/scala/tests/TestingServer.scala b/tests/unit/src/main/scala/tests/TestingServer.scala index a4ae0d0ade5..eaf6bbb8396 100644 --- a/tests/unit/src/main/scala/tests/TestingServer.scala +++ b/tests/unit/src/main/scala/tests/TestingServer.scala @@ -659,8 +659,9 @@ final case class TestingServer( assertSystemExit(parameter) val targets = List(new b.BuildTargetIdentifier(buildTarget(target))) val params = - new b.DebugSessionParams(targets.asJava, kind, parameter.toJson) - + new b.DebugSessionParams(targets.asJava) + params.setDataKind(kind) + params.setData(parameter.toJson) executeCommandUnsafe(ServerCommands.StartDebugAdapter.id, Seq(params)) .collect { case DebugSession(_, uri) => scribe.info(s"Starting debug session for $uri") diff --git a/tests/unit/src/test/scala/tests/troubleshoot/ProblemResolverSuite.scala b/tests/unit/src/test/scala/tests/troubleshoot/ProblemResolverSuite.scala index 05f989726a9..7f954a3b987 100644 --- a/tests/unit/src/test/scala/tests/troubleshoot/ProblemResolverSuite.scala +++ b/tests/unit/src/test/scala/tests/troubleshoot/ProblemResolverSuite.scala @@ -258,13 +258,18 @@ class ProblemResolverSuite extends FunSuite { val scalaBinaryVersion = ScalaVersions.scalaBinaryVersionFromFullVersion(scalaVersion) val buildId = new BuildTargetIdentifier(id) + val capabilities = new BuildTargetCapabilities() + capabilities.setCanCompile(true) + capabilities.setCanDebug(true) + capabilities.setCanRun(true) + capabilities.setCanTest(true) val buildTarget = new BuildTarget( buildId, /* tags = */ Nil.asJava, /* languageIds = */ Nil.asJava, /* dependencies = */ Nil.asJava, - /* capabilities = */ new BuildTargetCapabilities(true, true, true), + /* capabilities = */ capabilities, ) buildTarget.setDisplayName(id) val scalaBuildTarget = new ScalaBuildTarget(