Skip to content

Commit

Permalink
bugfix: Add back unintentionlly removed support for 2.11.x
Browse files Browse the repository at this point in the history
Previously, we would use the lates scalfix version by default, which doesn't support Scala 2.11 by default. Now, we fall back to the latest version that supported it.
  • Loading branch information
tgodzik committed Oct 23, 2023
1 parent 6446087 commit 74646c7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsQuickPickItem
import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
import scala.meta.internal.metals.{BuildInfo => V}
import scala.meta.internal.mtags.SemanticdbClasspath
import scala.meta.internal.semanticdb.TextDocuments
import scala.meta.io.AbsolutePath

import com.typesafe.config.ConfigFactory
import coursierapi.Dependency
import coursierapi.Repository
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.MessageType
import org.eclipse.{lsp4j => l}
import scalafix.interfaces.Scalafix
import scalafix.interfaces.ScalafixEvaluation
import scalafix.interfaces.ScalafixFileEvaluationError
import scalafix.internal.interfaces.ScalafixCoursier
import scalafix.internal.interfaces.ScalafixInterfacesClassloader

case class ScalafixProvider(
buffers: Buffers,
Expand Down Expand Up @@ -437,14 +441,32 @@ case class ScalafixProvider(
case None =>
statusBar.trackBlockingTask("Downloading scalafix") {
val scalafix =
Try(Scalafix.fetchAndClassloadInstance(scalaBinaryVersion))
if (scalaBinaryVersion == "2.11") Try(scala211Fallback)
else
Try(Scalafix.fetchAndClassloadInstance(scalaBinaryVersion))
scalafix.foreach(api => scalafixCache.update(scalaBinaryVersion, api))
scalafix
}
}

}

private def scala211Fallback: Scalafix = {
// last version that supports Scala 2.11.12
val latestSupporting = "0.10.4"
val jars = ScalafixCoursier.scalafixCliJars(
Repository.defaults(),
latestSupporting,
V.scala211,
)
val parent = new ScalafixInterfacesClassloader(
classOf[Scalafix].getClassLoader()
);
Scalafix.classloadInstance(
new URLClassLoader(jars.asScala.toArray, parent)
);
}

private def getRuleClassLoader(
scalfixRulesKey: ScalafixRulesClasspathKey,
scalafixClassLoader: ClassLoader,
Expand All @@ -457,8 +479,22 @@ case class ScalafixProvider(
) {
val rulesDependencies = scalfixRulesKey.usedRulesWithClasspath
val organizeImportRule =
// Scalafix version that supports Scala 2.11 doesn't have the rule built in
if (scalfixRulesKey.scalaBinaryVersion == "2.11")
Some(
Dependency.of(
"com.github.liancheng",
"organize-imports_" + scalfixRulesKey.scalaBinaryVersion,
"0.6.0",
)
)
else None

val allRules =
Try(
Embedded.rulesClasspath(rulesDependencies.toList)
Embedded.rulesClasspath(
rulesDependencies.toList ++ organizeImportRule
)
).map { paths =>
val classloader = Embedded.toClassLoader(
Classpath(paths.map(AbsolutePath(_))),
Expand All @@ -467,7 +503,7 @@ case class ScalafixProvider(
rulesClassloaderCache.update(scalfixRulesKey, classloader)
classloader
}
organizeImportRule
allRules
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tests.feature

import scala.meta.internal.metals.BuildInfo
import scala.meta.internal.metals.codeactions.SourceOrganizeImports

import tests.codeactions.BaseCodeActionLspSuite

class Scala211ActionsLspSuite
extends BaseCodeActionLspSuite("cross-code-actions") {

override protected val scalaVersion: String = BuildInfo.scala211

check(
"basic",
"""
|package a
|import scala.concurrent.duration._
|import scala.concurrent.Future<<>>
|import scala.concurrent.ExecutionContext.global
|
|object A {
| val d = Duration(10, MICROSECONDS)
| val k = Future.successful(1)
|}
|""".stripMargin,
s"${SourceOrganizeImports.title}",
"""
|package a
|import scala.concurrent.Future
|import scala.concurrent.duration._
|
|object A {
| val d = Duration(10, MICROSECONDS)
| val k = Future.successful(1)
|}
|""".stripMargin,
kind = List(SourceOrganizeImports.kind),
scalacOptions = List("-Ywarn-unused-import"),
)
}

0 comments on commit 74646c7

Please sign in to comment.