-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: add debug adapter for running main class to metals [skip…
… ci]
- Loading branch information
1 parent
a06a024
commit f09ed2d
Showing
10 changed files
with
501 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
metals/src/main/scala/scala/meta/internal/metals/debug/server/DebugLogger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package scala.meta.internal.metals.debug.server | ||
|
||
import ch.epfl.scala.debugadapter.Logger | ||
|
||
class DebugLogger extends Logger { | ||
|
||
override def debug(msg: => String): Unit = scribe.debug(msg) | ||
|
||
override def info(msg: => String): Unit = scribe.info(msg) | ||
|
||
override def warn(msg: => String): Unit = scribe.warn(msg) | ||
|
||
override def error(msg: => String): Unit = scribe.error(msg) | ||
|
||
override def trace(t: => Throwable): Unit = | ||
scribe.trace(s"$t: ${t.getStackTrace().mkString("\n\t")}") | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
metals/src/main/scala/scala/meta/internal/metals/debug/server/DebugeeParamsCreator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package scala.meta.internal.metals.debug.server | ||
|
||
import scala.meta.internal.metals.BuildTargets | ||
import ch.epfl.scala.bsp4j.BuildTargetIdentifier | ||
import ch.epfl.scala.debugadapter.Library | ||
import ch.epfl.scala.debugadapter.UnmanagedEntry | ||
import ch.epfl.scala.debugadapter.Module | ||
import scala.meta.internal.metals.ScalaTarget | ||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import ch.epfl.scala.debugadapter.ScalaVersion | ||
import ch.epfl.scala.debugadapter.SourceDirectory | ||
import ch.epfl.scala.bsp4j.MavenDependencyModule | ||
import ch.epfl.scala.debugadapter.SourceJar | ||
import scala.meta.io.AbsolutePath | ||
import scala.meta.internal.metals.JavaTarget | ||
|
||
class DebugeeParamsCreator(buildTargets: BuildTargets) { | ||
def create(id: BuildTargetIdentifier) = { | ||
val optScalaTarget = buildTargets.scalaTarget(id) | ||
val optJavaTarget = buildTargets.javaTarget(id) | ||
for { | ||
name <- optScalaTarget | ||
.map(_.displayName) | ||
.orElse(optJavaTarget.map(_.displayName)) | ||
data <- buildTargets.targetData(id) | ||
} yield { | ||
|
||
val libraries = data.buildTargetDependencyModules | ||
.get(id) | ||
.filter(_.nonEmpty) | ||
.getOrElse(Nil) | ||
val debugLibs = libraries.flatMap(createLibrary(_)) | ||
val includedInLibs = debugLibs | ||
.flatMap(_.sourceEntries.flatMap { | ||
case SourceJar(jar) => Some(jar) | ||
case _ => None | ||
}) | ||
.toSet | ||
|
||
val classpath = optScalaTarget | ||
.map(_.scalac.classpath.toAbsoluteClasspath.toSeq) | ||
.orElse { | ||
buildTargets.targetClasspath(id).map(_.map(AbsolutePath(_))) | ||
} | ||
.getOrElse(Nil) | ||
|
||
val filteredClassPath = classpath.collect { | ||
case path if includedInLibs(path.toNIO) => UnmanagedEntry(path.toNIO) | ||
}.toList | ||
|
||
val modules = buildTargets | ||
.allInverseDependencies(id) | ||
.flatMap(id => | ||
buildTargets.scalaTarget(id).map(createModule(_)).orElse { | ||
buildTargets.javaTarget(id).map(createModule(_)) | ||
} | ||
) | ||
.toSeq | ||
|
||
new DebugeeProject( | ||
buildTargets.scalaTarget(id).map(_.scalaVersion), | ||
name, | ||
modules, | ||
libraries.flatMap(createLibrary(_)), | ||
filteredClassPath, | ||
classpath, | ||
) | ||
} | ||
} | ||
|
||
def createLibrary(lib: MavenDependencyModule) = { | ||
def getWithClassifier(s: String) = | ||
Option(lib.getArtifacts()) | ||
.flatMap(_.asScala.find(_.getClassifier() == s)) | ||
.flatMap(_.getUri().toAbsolutePathSafe) | ||
for { | ||
sources <- getWithClassifier("sources") | ||
jar <- getWithClassifier(null) | ||
} yield new Library( | ||
lib.getName(), | ||
lib.getVersion(), | ||
jar.toNIO, | ||
Seq(SourceJar(sources.toNIO)), | ||
) | ||
} | ||
|
||
def createModule(target: ScalaTarget) = { | ||
val scalaVersion = ScalaVersion(target.scalaVersion) | ||
new Module( | ||
target.displayName, | ||
Some(scalaVersion), | ||
target.scalac.getOptions().asScala.toSeq, | ||
target.classDirectory.toAbsolutePath.toNIO, | ||
sources(target.id), | ||
) | ||
} | ||
|
||
def createModule(target: JavaTarget) = | ||
new Module( | ||
target.displayName, | ||
None, | ||
Nil, | ||
target.classDirectory.toAbsolutePath.toNIO, | ||
sources(target.id), | ||
) | ||
|
||
private def sources(id: BuildTargetIdentifier) = | ||
buildTargets.sourceItemsToBuildTargets | ||
.filter(_._2.iterator.asScala.contains(id)) | ||
.collect { case (path, _) => | ||
SourceDirectory(path.toNIO) | ||
} | ||
.toSeq | ||
} | ||
|
||
case class DebugeeProject( | ||
scalaVersion: Option[String], | ||
name: String, | ||
modules: Seq[Module], | ||
libraries: Seq[Library], | ||
unmanagedEntries: Seq[UnmanagedEntry], | ||
classpath: Seq[AbsolutePath], | ||
) |
Oops, something went wrong.