Skip to content

Commit

Permalink
feature: Add the possibility to discover the main class to run
Browse files Browse the repository at this point in the history
Previously, the only possibility to discover the main class to run was to try and run it. Now, we can also ask for it so that client editor can use that shell command.
  • Loading branch information
tgodzik committed Feb 10, 2023
1 parent df320fe commit 966d799
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ class MetalsLspService(
compilers,
statusBar,
sourceMapper,
() => userConfig,
)
)

Expand Down Expand Up @@ -1825,6 +1826,10 @@ class MetalsLspService(
Option(params.uri).map(_.toAbsolutePath)
)
}.asJavaObject
case ServerCommands.DiscoverMainClasses(unresolvedParams) =>
debugProvider
.runCommandDiscovery(unresolvedParams)
.asJavaObject
case ServerCommands.RunScalafix(params) =>
val uri = params.getTextDocument().getUri()
scalafixProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ object ServerCommands {
|""".stripMargin,
)

val DiscoverMainClasses = new ParametrizedCommand[DebugDiscoveryParams](
"discover-jvm-run-command",
"Discover main classes to run and return the object",
"""|Gets the DebugSession object that also contains a command to run in shell based
|on JVM environment including classpath, jvmOptions and environment parameters.
|""".stripMargin,
"""|DebugUnresolvedTestClassParams object
|Example:
|```json
|{
| "path": "path/to/file.scala",
| "runType": "run"
|}
|```
|""".stripMargin,
)

/** If uri is null discover all test suites, otherwise discover testcases in file */
final case class DiscoverTestParams(
@Nullable uri: String = null
Expand Down Expand Up @@ -666,6 +683,7 @@ object ServerCommands {
CleanCompile,
ConvertToNamedArguments,
CopyWorksheetOutput,
DiscoverMainClasses,
DiscoverTestSuites,
ExtractMemberDefinition,
GenerateBspConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import com.google.common.net.InetAddresses
import com.google.gson.JsonElement
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.MessageType
import scala.meta.internal.metals.UserConfiguration

/**
* @param supportsTestSelection test selection hasn't been defined in BSP spec yet.
Expand All @@ -83,6 +84,7 @@ class DebugProvider(
compilers: Compilers,
statusBar: StatusBar,
sourceMapper: SourceMapper,
userConfig: () => UserConfiguration,
) extends Cancelable {

import DebugProvider._
Expand Down Expand Up @@ -405,6 +407,43 @@ class DebugProvider(
}
}

/**
* Tries to discover the main class to run and returns
* DebugSessionParams that contains the shellCommand field.
* This is used so that clients can easily run the full command
* if they want.
*/
def runCommandDiscovery(
unresolvedParams: DebugDiscoveryParams
)(implicit ec: ExecutionContext): Future[b.DebugSessionParams] = {
debugDiscovery(unresolvedParams).map(enrichWithMainShellCommand)
}

private def enrichWithMainShellCommand(
params: b.DebugSessionParams
): b.DebugSessionParams = {
params.getData() match {
case json: JsonElement
if params.getDataKind == b.DebugSessionParamsDataKind.SCALA_MAIN_CLASS =>
json.as[b.ScalaMainClass] match {
case Success(main) if params.getTargets().size > 0 =>
val updatedData = buildTargetClasses.jvmRunEnvironment
.get(params.getTargets().get(0))
.zip(userConfig().usedJavaBinary) match {
case None =>
main.toJson
case Some((env, javaHome)) =>
ExtendedScalaMainClass(main, env, javaHome, workspace).toJson
}
params.setData(updatedData)
case _ =>
}

case _ =>
}
params
}

/**
* Given fully unresolved params this figures out the runType that was passed
* in and then discovers either the main methods for the build target the
Expand Down

0 comments on commit 966d799

Please sign in to comment.