|
| 1 | +package scala.build.bsp.buildtargets |
| 2 | + |
| 3 | +import ch.epfl.scala.bsp4j as b |
| 4 | + |
| 5 | +import scala.build.GeneratedSource |
| 6 | +import scala.build.input.Inputs |
| 7 | +import scala.build.internal.Constants |
| 8 | +import scala.build.options.Scope |
| 9 | +import scala.collection.mutable |
| 10 | + |
| 11 | +trait ManagesBuildTargetsImpl extends ManagesBuildTargets { |
| 12 | + |
| 13 | + import ManagesBuildTargets.* |
| 14 | + |
| 15 | + protected val projectNames = mutable.Map[Scope, ProjectName]() |
| 16 | + protected val generatedSources = mutable.Map[Scope, GeneratedSources]() |
| 17 | + |
| 18 | + def targetIds: List[b.BuildTargetIdentifier] = |
| 19 | + projectNames |
| 20 | + .toList |
| 21 | + .sortBy(_._1) |
| 22 | + .map(_._2) |
| 23 | + .flatMap(_.targetUriOpt) |
| 24 | + .map(uri => new b.BuildTargetIdentifier(uri)) |
| 25 | + |
| 26 | + def targetScopeIdOpt(scope: Scope): Option[b.BuildTargetIdentifier] = |
| 27 | + projectNames |
| 28 | + .get(scope) |
| 29 | + .flatMap(_.targetUriOpt) |
| 30 | + .map(uri => new b.BuildTargetIdentifier(uri)) |
| 31 | + |
| 32 | + def resetProjectNames(): Unit = |
| 33 | + projectNames.clear() |
| 34 | + def setProjectName(workspace: os.Path, name: String, scope: Scope): Unit = |
| 35 | + if (!projectNames.contains(scope)) |
| 36 | + projectNames(scope) = ProjectName(workspace, name) |
| 37 | + |
| 38 | + def newInputs(inputs: Inputs): Unit = { |
| 39 | + resetProjectNames() |
| 40 | + setProjectName(inputs.workspace, inputs.projectName, Scope.Main) |
| 41 | + setProjectName(inputs.workspace, inputs.scopeProjectName(Scope.Test), Scope.Test) |
| 42 | + } |
| 43 | + |
| 44 | + def setGeneratedSources(scope: Scope, sources: Seq[GeneratedSource]): Unit = { |
| 45 | + generatedSources(scope) = GeneratedSources(sources) |
| 46 | + } |
| 47 | + |
| 48 | + protected def targetWorkspaceDirOpt(id: b.BuildTargetIdentifier): Option[String] = |
| 49 | + projectNames.collectFirst { |
| 50 | + case (_, projName) if projName.targetUriOpt.contains(id.getUri) => |
| 51 | + (projName.bloopWorkspace / Constants.workspaceDirName).toIO.toURI.toASCIIString |
| 52 | + } |
| 53 | + protected def targetScopeOpt(id: b.BuildTargetIdentifier): Option[Scope] = |
| 54 | + projectNames.collectFirst { |
| 55 | + case (scope, projName) if projName.targetUriOpt.contains(id.getUri) => |
| 56 | + scope |
| 57 | + } |
| 58 | + protected def validTarget(id: b.BuildTargetIdentifier): Boolean = |
| 59 | + targetScopeOpt(id).nonEmpty |
| 60 | + |
| 61 | +} |
0 commit comments