Skip to content

Commit

Permalink
Don't add semanticdb plugin for Scala 3.
Browse files Browse the repository at this point in the history
For Scala 3 semanticdb is produced natively by the compiler behind the
`-Xsemanticdb` flag, so the plugin isn't necessary. This change ensures
the correct scalacOptions are set when using Scala 3 with
ScalaMetalsSupport.
  • Loading branch information
ckipp01 committed Dec 1, 2021
1 parent 267ff30 commit bbc4ae7
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions scalalib/src/mill/scalalib/bsp/ScalaMetalsSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,44 @@ package mill.scalalib.bsp
import mill.api.experimental
import mill.{Agg, T}
import mill.define.Target
import mill.scalalib.api.Util.isScala3
import mill.scalalib.{Dep, DepSyntax, ScalaModule}
import mill.api.Result

/*+ Enable some common settings required to properly support Metals Language Server (via BSP). */
@experimental
trait ScalaMetalsSupport extends ScalaModule {

/** The semanticDB version to use. It needs to support your configured Scala versions. */
def semanticDbVersion: T[String]
def semanticDbVersion: T[String] = T { "" }

override def scalacPluginIvyDeps: Target[Agg[Dep]] = T {
super.scalacPluginIvyDeps() ++ Agg(
ivy"org.scalameta:::semanticdb-scalac:${semanticDbVersion()}"
)
if (!isScala3(scalaVersion()) && semanticDbVersion().isEmpty) {
val msg =
"""|
|When using ScalaMetalsSupport with Scala 2 you must provide a semanticDbVersion
|
|def semanticDbVersion = Some(???)
|""".stripMargin
Result.Failure(msg)
} else {
Result.Success(
super.scalacPluginIvyDeps() ++ Agg(
ivy"org.scalameta:::semanticdb-scalac:${semanticDbVersion()}"
)
)
}
}

/** Adds some options and configures the semanticDB plugin. */
override def mandatoryScalacOptions: Target[Seq[String]] = T {
super.mandatoryScalacOptions() ++ Seq("-Yrangepos", s"-P:semanticdb:sourceroot:${T.workspace}")
super.mandatoryScalacOptions() ++ {
if (isScala3(scalaVersion())) {
Seq("-Xsemanticdb")
} else {
Seq("-Yrangepos", s"-P:semanticdb:sourceroot:${T.workspace}")
}
}
}

/** Filters options unsupported by Metals. */
Expand Down

0 comments on commit bbc4ae7

Please sign in to comment.