pgrandjean
pgrandjean

Reputation: 766

How to configure IntelliJ to manage different Scala versions

I have a Scala project managed by SBT, with the following source folder structure:

src
 |-main
 |  |-scala
 |  |-scala-2
 |  |-scala-3 

The folder

SBT handles the three folders well, switching from Scala 2 to Scala 3 and testing both works fine and even releasing JARs for Scala 2 and 3 works OK.

However, I use IntelliJ for development and I can't get it to work fine with two concurrent Scala versions. For instance, it will handle folders scala and scala-2, but ignore scala-3. This means I have all the IntelliJ features (like code inspection, auto-complete, etc) working for the two first folder, but not for the third folder.

How to correctly configure IntelliJ to handle different Scala versions and source codes?

I'm using:

Upvotes: 3

Views: 948

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27595

Best you can do, is to disable some projects in IntelliJ and enable only the one you want to work on currently. For instance if I would like to only work on JVM 2.13 of projects cross compiled with sbt-projectmatrix (and sbt-commandmatrix):

// project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.9.0")
addSbtPlugin("com.indoorvivants" % "sbt-commandmatrix" % "0.0.5")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.11.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.7")
addSbtPlugin("org.jetbrains" % "sbt-ide-settings" % "1.1.0")
// build.sbt

val scala2_13version = "2.13.10"
val scala3version = "3.2.0"

// which version I want to use in my project
val ideScala = scala2_13version
Global / excludeLintKeys += ideSkipProject
val only1JvmScalaInIde = MatrixAction
  .ForPlatforms(VirtualAxis.jvm)
  .Configure(_.settings(ideSkipProject := (scalaVersion.value != ideScala)))
val noJsNoNativeInIde = MatrixAction
  .ForPlatforms(VirtualAxis.js, VirtualAxis.native)
  .Configure(_.settings(ideSkipProject := true))

val example = projectMatrix
  .in(file("example"))
  .someVariations(
    List(scala2_13version, scala3version),
    List(VirtualAxis.jvm, VirtualAxis.js, VirtualAxis.native)
  )(only1JvmScalaInIde, noJsNoNativeInIde)
  .settings(
    // ...
  )

val root = project
  .in(file("."))
  .aggregate(example.projectRefs: _*)

The crucial part is this

ideSkipProject := true

that you'd apply for all projects that you don't want IntelliJ to import (key comes from addSbtPlugin("org.jetbrains" % "sbt-ide-settings" % "1.1.0") plugin). You can adapt it to whatever other cross-compiling solution you are using.

Personally, I use this workflow above, where I set ideScala to either 2.13 or 3 and work only with 1 version at a time, and edit it and reload sbt when I need to switch. While these projects are not imported to IntelliJ, sbt still sees and builds all of them.

Upvotes: 2

Related Questions