From 4f178f42a5669511c42e7df6d9c668e159c2b717 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 13 Dec 2023 17:16:21 +0100 Subject: [PATCH] improvement: Allow running organize imports on scripts --- .../metals/codeactions/OrganizeImports.scala | 2 +- .../tests/scalacli/ScalaCliActionsSuite.scala | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/codeactions/OrganizeImports.scala b/metals/src/main/scala/scala/meta/internal/metals/codeactions/OrganizeImports.scala index 9804c03ce70..818da536ef3 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/codeactions/OrganizeImports.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/codeactions/OrganizeImports.scala @@ -66,7 +66,7 @@ sealed abstract class OrganizeImports( } protected def isScalaOrSbt(file: AbsolutePath): Boolean = - Seq("scala", "sbt").contains(file.extension) + Seq("scala", "sbt", "sc").contains(file.extension) } object OrganizeImports {} diff --git a/tests/slow/src/test/scala/tests/scalacli/ScalaCliActionsSuite.scala b/tests/slow/src/test/scala/tests/scalacli/ScalaCliActionsSuite.scala index 2654facdd55..632018381e1 100644 --- a/tests/slow/src/test/scala/tests/scalacli/ScalaCliActionsSuite.scala +++ b/tests/slow/src/test/scala/tests/scalacli/ScalaCliActionsSuite.scala @@ -3,6 +3,7 @@ package tests.scalacli import scala.meta.internal.metals.BuildInfo import scala.meta.internal.metals.codeactions.CreateNewSymbol import scala.meta.internal.metals.codeactions.ImportMissingSymbol +import scala.meta.internal.metals.codeactions.SourceOrganizeImports import scala.meta.internal.mtags.BuildInfo.scalaCompilerVersion import scala.meta.internal.mtags.CoursierComplete @@ -158,4 +159,42 @@ class ScalaCliActionsSuite fileName = "A.sc", ) + checkScalaCLI( + "script-organize-imports", + s"""|//> using scala "${BuildInfo.scala213}" + | + |import scala.concurrent.Futur<<>>e + |import scala.concurrent.duration._ + |import scala.concurrent.ExecutionContext + |import scala.concurrent.ExecutionContext.global + | + |object A { + | implicit val ec: ExecutionContext = global + | val d = Duration(10, MICROSECONDS) + | val k = Future.successful(1) + | Future{ println("Hello!") } + |} + |""".stripMargin, + s"""|${SourceOrganizeImports.title} + |""".stripMargin, + s"""|//> using scala "${BuildInfo.scala213}" + | + |import scala.concurrent.ExecutionContext + |import scala.concurrent.ExecutionContext.global + |import scala.concurrent.Future + |import scala.concurrent.duration._ + | + |object A { + | implicit val ec: ExecutionContext = global + | val d = Duration(10, MICROSECONDS) + | val k = Future.successful(1) + | Future{ println("Hello!") } + |} + |""".stripMargin, + scalaCliOptions = List("-S", scalaVersion), + expectNoDiagnostics = false, + kind = List(SourceOrganizeImports.kind), + fileName = "A.sc", + ) + }