Skip to content

Commit

Permalink
Merge pull request #2857 from tgodzik/scala3-organize-imports
Browse files Browse the repository at this point in the history
Enable organize imports for Scala 3
  • Loading branch information
tgodzik authored Jun 5, 2021
2 parents 3d71a54 + ab355b8 commit d3aa263
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ case class ScalafixProvider(
file: AbsolutePath,
scalaTarget: ScalaTarget
): Try[ScalafixEvaluation] = {
val scalaBinaryVersion = scalaTarget.scalaBinaryVersion
val defaultScalaVersion = scalaTarget.scalaBinaryVersion
val scalaBinaryVersion =
if (defaultScalaVersion.startsWith("3")) "2.13" else defaultScalaVersion

val targetRoot =
buildTargets.scalacOptions(scalaTarget.info.getId()).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import scala.meta.internal.metals.CodeAction
import scala.meta.internal.metals.MetalsEnrichments.XtensionString
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ScalaTarget
import scala.meta.internal.metals.ScalaVersions
import scala.meta.internal.metals.ScalacDiagnostic
import scala.meta.internal.metals.ScalafixProvider
import scala.meta.io.AbsolutePath
Expand Down Expand Up @@ -42,15 +41,8 @@ final class OrganizeImports(
target <- buildTargets.scalaTarget(buildId)
} yield target
scalaTarget match {
case Some(target)
if !ScalaVersions.isScala3Version(target.scalaVersion) =>
case Some(target) =>
organizeImportsEdits(file, target)
case Some(target)
if ScalaVersions.isScala3Version(target.scalaVersion) =>
scribe.info(
s"Organize import doesn't work on ${target.scalaVersion} files"
)
Future.successful(Seq())
case _ => Future.successful(Seq())
}
} else Future.successful(Seq())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests.feature

import scala.meta.internal.metals.BuildInfo
import scala.meta.internal.metals.codeactions.ExtractRenameMember
import scala.meta.internal.metals.codeactions.OrganizeImports
import scala.meta.internal.mtags.MtagsEnrichments.XtensionAbsolutePath

import munit.Location
Expand All @@ -23,6 +24,34 @@ class CrossCodeActionLspSuite
|""".stripMargin
)

check(
"organize-imports",
"""
|package a
|import scala.concurrent.{ExecutionContext, Future}
|import scala.util.Try<<>>
|
|object A {
| val executionContext: ExecutionContext = ???
| val k = Future.successful(1)
| val tr = Try{ new Exception("name") }
|}
|""".stripMargin,
s"${OrganizeImports.title}",
"""|package a
|import scala.concurrent.ExecutionContext
|import scala.concurrent.Future
|import scala.util.Try
|
|object A {
| val executionContext: ExecutionContext = ???
| val k = Future.successful(1)
| val tr = Try{ new Exception("name") }
|}
|""".stripMargin,
kind = List(OrganizeImports.kind)
)

checkExtractedMember(
"extract-enum",
"""|package a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ abstract class BaseCodeActionLspSuite(suiteName: String)
fileName: String = "A.scala"
)(implicit loc: Location): Unit = {
val scalacOptionsJson =
s""""scalacOptions": ["${scalacOptions.mkString("\",\"")}"]"""
if (scalacOptions.nonEmpty)
s""""scalacOptions": ["${scalacOptions.mkString("\",\"")}"],"""
else ""
val path = s"a/src/main/scala/a/$fileName"
val newPath = renamePath.getOrElse(path)
val fileContent = input.replace("<<", "").replace(">>", "")
Expand All @@ -50,7 +52,7 @@ abstract class BaseCodeActionLspSuite(suiteName: String)
cleanWorkspace()
for {
_ <- server.initialize(s"""/metals.json
|{"a":{$scalacOptionsJson, "scalaVersion" : "$scalaVersion"}}
|{"a":{$scalacOptionsJson "scalaVersion" : "$scalaVersion"}}
|$scalafixConf
|/$path
|$fileContent""".stripMargin)
Expand Down

0 comments on commit d3aa263

Please sign in to comment.