Skip to content

Commit fd5764d

Browse files
committed
NIT: Just to make reviewing easier:
- copy HasGeneratedSources.scala into ManagesBuildTargets.scala - copy HasGeneratedSourcesImpl.scala into ManagesBuildTargetsImpl.scala.
1 parent f5e1838 commit fd5764d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
10+
trait ManagesBuildTargets {
11+
def targetIds: List[b.BuildTargetIdentifier]
12+
def targetScopeIdOpt(scope: Scope): Option[b.BuildTargetIdentifier]
13+
def setProjectName(workspace: os.Path, name: String, scope: Scope): Unit
14+
def resetProjectNames(): Unit
15+
def newInputs(inputs: Inputs): Unit
16+
def setGeneratedSources(scope: Scope, sources: Seq[GeneratedSource]): Unit
17+
}
18+
19+
object ManagesBuildTargets {
20+
final case class GeneratedSources(
21+
sources: Seq[GeneratedSource]
22+
) {
23+
24+
lazy val uriMap: Map[String, GeneratedSource] =
25+
sources
26+
.flatMap { g =>
27+
g.reportingPath match {
28+
case Left(_) => Nil
29+
case Right(_) => Seq(g.generated.toNIO.toUri.toASCIIString -> g)
30+
}
31+
}
32+
.toMap
33+
}
34+
35+
final case class ProjectName(
36+
bloopWorkspace: os.Path,
37+
name: String,
38+
var targetUriOpt: Option[String] = None
39+
) {
40+
targetUriOpt =
41+
Some(
42+
(bloopWorkspace / Constants.workspaceDirName)
43+
.toIO
44+
.toURI
45+
.toASCIIString
46+
.stripSuffix("/") +
47+
"/?id=" + name
48+
)
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)