Skip to content

Commit

Permalink
BSP: Fix mill-build module interpretation in IntelliJ IDEA (#1876)
Browse files Browse the repository at this point in the history
IntelliJ IDEA does not understand single files as sources, which we would need to handle `build.sc`. But when we use the project root directory, it creates a problem where IntelliJ may interpret a `root.Foo` class in a sub-project `sub/foo` as `sub.foo.src.root.Foo` class.

Pull request: #1876
  • Loading branch information
atty303 authored May 17, 2022
1 parent 3f18d00 commit 0fd40b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bsp/src/mill/bsp/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class MillBuildServer(
var clientInitialized = false
var shutdownRequested = false
var clientWantsSemanticDb = false
var clientIsIntelliJ = false

class State(val evaluator: Evaluator) {
private[this] object internal {
Expand Down Expand Up @@ -221,6 +222,11 @@ class MillBuildServer(
capabilities.setJvmRunEnvironmentProvider(true)
capabilities.setJvmTestEnvironmentProvider(true)

request.getDisplayName match {
case "IntelliJ-BSP" => clientIsIntelliJ = true
case _ => clientIsIntelliJ = false
}

request.getData match {
case d: JsonObject =>
log.debug(s"extra data: ${d} of type ${d.getClass}")
Expand Down Expand Up @@ -335,6 +341,15 @@ class MillBuildServer(
targetIds = sourcesParams.getTargets.asScala.toSeq,
agg = (items: Seq[SourcesItem]) => new SourcesResult(items.asJava)
) {
case (id, module: MillBuildTarget) if clientIsIntelliJ =>
T.task {
val sources = new SourcesItem(
id,
module.dummySources().map(p => sourceItem(p.path, true)).asJava
)
sources.setRoots(Seq(sanitizeUri(evaluator.rootModule.millSourcePath)).asJava)
sources
}
// case (id, `millBuildTarget`) =>
// T.task {
// new SourcesItem(
Expand Down
3 changes: 3 additions & 0 deletions scalalib/src/mill/scalalib/bsp/BspModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@ trait MillBuildTarget // (rootModule: BaseModule, ctx0: mill.define.Ctx)
os.makeDir(T.dest / "classes")
CompilationResult(T.dest / "dummy", PathRef(T.dest / "classes"))
}

/** Used in BSP IntelliJ, which can only work with directories */
def dummySources: Sources = T.sources(T.dest)
}

0 comments on commit 0fd40b0

Please sign in to comment.