Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Feb 13, 2024
1 parent 11790cf commit 561bd17
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 67 deletions.
13 changes: 6 additions & 7 deletions frontend/src/main/scala/bloop/dap/BloopDebugToolsResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ class BloopDebugToolsResolver(logger: Logger) extends DebugToolsResolver {
}
}

override def resolveUnpickler(scalaVersion: ScalaVersion): Try[ClassLoader] = {
getOrTryUpdate(stepFilterCache, scalaVersion) {
val unpicklerModule = s"${BuildInfo.unpicklerName}_${scalaVersion.binaryVersion}"
val stepFilter = Artifact(BuildInfo.organization, unpicklerModule, BuildInfo.version)
val tastyCore = Artifact("org.scala-lang", "tasty-core_3", scalaVersion.value)
override def resolveDecoder(scalaVersion: ScalaVersion): Try[ClassLoader] = {
getOrTryUpdate(decoderCache, scalaVersion) {
val decoderModule = s"${BuildInfo.decoderName}_${scalaVersion.binaryVersion}"
val artifact = Artifact(BuildInfo.organization, decoderModule, BuildInfo.version)
DependencyResolution
.resolveWithErrors(List(stepFilter, tastyCore), logger)
.resolveWithErrors(List(artifact), logger)
.map(jars => toClassLoader(jars, true))
.toTry
}
Expand All @@ -66,5 +65,5 @@ class BloopDebugToolsResolver(logger: Logger) extends DebugToolsResolver {

object BloopDebugToolsResolver {
private val expressionCompilerCache: mutable.Map[ScalaVersion, ClassLoader] = mutable.Map.empty
private val stepFilterCache: mutable.Map[ScalaVersion, ClassLoader] = mutable.Map.empty
private val decoderCache: mutable.Map[ScalaVersion, ClassLoader] = mutable.Map.empty
}
24 changes: 12 additions & 12 deletions frontend/src/main/scala/bloop/dap/BloopDebuggee.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ private final class AttachRemoteDebugAdapter(
}

object BloopDebuggeeRunner {
def getEntries(
project: Project,
state: State
): (Seq[Module], Seq[Library], Seq[UnmanagedEntry]) = {
val dag = state.build.getDagFor(project)
val modules = getModules(dag, state.client)
val libraries = getLibraries(dag)
val unmanagedEntries =
getUnmanagedEntries(project, dag, state.client, modules ++ libraries)
(modules, libraries, unmanagedEntries)
}

def forMainClass(
projects: Seq[Project],
mainClass: ScalaMainClass,
Expand Down Expand Up @@ -265,6 +253,18 @@ object BloopDebuggeeRunner {
}
}

private def getEntries(
project: Project,
state: State
): (Seq[Module], Seq[Library], Seq[UnmanagedEntry]) = {
val dag = state.build.getDagFor(project)
val modules = getModules(dag, state.client)
val libraries = getLibraries(dag)
val unmanagedEntries =
getUnmanagedEntries(project, dag, state.client, modules ++ libraries)
(modules, libraries, unmanagedEntries)
}

private def getLibraries(dag: Dag[Project]): Seq[Library] = {
Dag
.dfs(dag, mode = Dag.PreOrder)
Expand Down
89 changes: 41 additions & 48 deletions frontend/src/main/scala/bloop/engine/tasks/CompileTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bloop.engine.tasks

import scala.collection.mutable
import scala.concurrent.Promise
import scala.jdk.CollectionConverters._

import bloop.CompileBackgroundTasks
import bloop.CompileInputs
Expand Down Expand Up @@ -36,10 +37,10 @@ import bloop.tracing.BraveTracer
import monix.execution.CancelableFuture
import monix.reactive.MulticastStrategy
import monix.reactive.Observable
import sbt.util.InterfaceUtil
import xsbti.VirtualFileRef
import xsbti.compile.analysis.ReadStamps
import xsbti.compile.analysis.Stamp
import xsbti.VirtualFileRef
import scala.jdk.CollectionConverters._

object CompileTask {
private implicit val logContext: DebugFilter = DebugFilter.Compilation
Expand Down Expand Up @@ -223,52 +224,6 @@ object CompileTask {
}
}

def registerUpdatedClasses(res: FinalNormalCompileResult, project: Project) = {
res.result.fromCompiler match {
case s: Success =>
val currentAnalysis =
Option(s.products.resultForFutureCompilationRuns.analysis().orElse(null))
val previous = res.result.previous
val previousAnalysis =
if (previous.isEmpty) None
else Option(previous.get.previous.analysis().orElse(null))

(currentAnalysis, previousAnalysis) match {
case (None, _) | (_, None) => ()
case (Some(curr), Some(prev)) =>
val currentStamps = curr.readStamps
val previousStamps = prev.readStamps
val newClasses = getNewClasses(currentStamps, previousStamps)
project.classObserver.onNext(newClasses)
}
case _ => ()
}
}

def getNewClasses(currentStamps: ReadStamps, previousStamps: ReadStamps): Seq[String] = {
def isNewer(current: Stamp, previous: Stamp) = {
if (previous == null) true
else {
val newHash = current.getHash
val oldHash = previous.getHash
newHash.isPresent && (!oldHash.isPresent || newHash.get != oldHash.get)
}
}

object ClassFile {
def unapply(vf: VirtualFileRef): Option[String] = {
val fqcn = vf.name
if (fqcn.toString.endsWith(".class")) Some(fqcn.stripSuffix(".class"))
else None
}
}

val oldStamps = previousStamps.getAllProductStamps
currentStamps.getAllProductStamps.asScala.collect {
case (file @ ClassFile(fqcn), stamp) if isNewer(stamp, oldStamps.get(file)) => fqcn
}.toSeq
}

def setup(inputs: CompileDefinitions.BundleInputs): Task[CompileBundle] = {
// Create a multicast observable stream to allow multiple mirrors of loggers
val (observer, obs) = {
Expand Down Expand Up @@ -389,6 +344,44 @@ object CompileTask {
}
}

private def registerUpdatedClasses(res: FinalNormalCompileResult, project: Project) = {
res.result.fromCompiler match {
case s: Success =>
val currentAnalysis =
InterfaceUtil.toOption(s.products.resultForFutureCompilationRuns.analysis)
val previous = res.result.previous
val previousAnalysis =
previous.flatMap(p => InterfaceUtil.toOption(p.previous.analysis))

currentAnalysis.zip(previousAnalysis).foreach {
case (curr, prev) =>
val currentStamps = curr.readStamps
val previousStamps = prev.readStamps
val newClasses = getNewClasses(currentStamps, previousStamps)
project.classObserver.onNext(newClasses)
}
case _ => ()
}
}

private def getNewClasses(currentStamps: ReadStamps, previousStamps: ReadStamps): Seq[String] = {
def isNewer(current: Stamp, previous: Stamp) = {
previous == null || {
val newHash = current.getHash
val oldHash = previous.getHash
newHash.isPresent && (!oldHash.isPresent || newHash.get != oldHash.get)
}
}

def isClassFile(vf: VirtualFileRef): Boolean = vf.name.toString.endsWith(".class")

val oldStamps = previousStamps.getAllProductStamps
currentStamps.getAllProductStamps.asScala.iterator.collect {
case (file, stamp) if isClassFile(file) && isNewer(stamp, oldStamps.get(file)) =>
file.name.stripSuffix(".class")
}.toSeq
}

case class ConfiguredCompilation(scalacOptions: List[String])
private def configureCompilation(
project: Project
Expand Down

0 comments on commit 561bd17

Please sign in to comment.