Skip to content

Commit

Permalink
OrganizeImports: support Scala3 syntax as output
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Jan 21, 2024
1 parent 77eca36 commit e673412
Show file tree
Hide file tree
Showing 64 changed files with 247 additions and 31 deletions.
12 changes: 12 additions & 0 deletions docs/rules/OrganizeImports.md
Original file line number Diff line number Diff line change
Expand Up @@ -1374,11 +1374,16 @@ OrganizeImports {
Before:

```scala
import scala.collection.immutable.{List => L}
import scala.collection.mutable.{Map, Buffer => _, Seq => S, _}
```

After:

```scala
import scala.collection.immutable.{List => L}
import scala.collection.mutable.Map
import scala.collection.mutable.{Buffer => _, Seq => S, _}
```

#### `Scala3`
Expand All @@ -1392,10 +1397,17 @@ OrganizeImports {
Before:

```scala
import scala.collection.immutable.{List => L}
import scala.collection.mutable.{Map, Buffer => _, Seq => S, _}
import scala.concurrent.ExecutionContext.Implicits.{given scala.concurrent.ExecutionContext}
```

After:

```scala
import scala.collection.immutable.List as L
import scala.collection.mutable.Map
import scala.collection.mutable.{Buffer as _, Seq as S, *}
import scala.concurrent.ExecutionContext.Implicits.given scala.concurrent.ExecutionContext
```

Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,12 @@ class OrganizeImports(

importer match {
case Importer(_, Importee.Wildcard() :: Nil) =>
syntax.patch(syntax.lastIndexOfSlice("._"), ".\u0001", 2)
val wildcardSyntax = Importee.Wildcard().syntax
syntax.patch(
syntax.lastIndexOfSlice(s".$wildcardSyntax"),
".\u0001",
2
)

case _ if importer.isCurlyBraced =>
syntax
Expand Down Expand Up @@ -714,9 +719,48 @@ class OrganizeImports(
private def importerSyntax(importer: Importer): String =
importer.pos match {
case pos: Position.Range =>
// Position found, implies that `importer` was directly parsed from the source code. Returns
// the original parsed text to preserve the original source level formatting.
pos.text
// Position found, implies that `importer` was directly parsed from the source code. Use
// the original parsed text to preserve the original source level formatting, but patch
// importee that have specific Scala 3 syntax.
val syntax = new StringBuilder(pos.text)
def patchSyntax(
t: Tree,
newSyntax: String,
stripEnclosingBraces: Boolean = false
) = {
val start = t.pos.start - pos.start
syntax.replace(start, t.pos.end - pos.start, newSyntax)
val end = t.pos.start - pos.start + newSyntax.length

if (stripEnclosingBraces)
(
syntax.take(start).lastIndexOf('{'),
syntax.indexOf('}', end)
) match {
case (from, to) if from != -1 && to != -1 =>
syntax.delete(end, to + 1)
syntax.delete(from, start)
case _ =>
}
}
val Importer(_, importees) = importer
val optionalBraces =
importees.length == 1 && targetDialect.allowAsForImportRename
// traverse & patch backwards to avoid shifting indices
importees.reverse.foreach {
case i @ Importee.Rename(_, _) =>
patchSyntax(i, i.copy().syntax, optionalBraces)
case i @ Importee.Unimport(_) =>
patchSyntax(i, i.copy().syntax, optionalBraces)
case i @ Importee.Wildcard() =>
patchSyntax(i, i.copy().syntax)
case i @ Importee.GivenAll() =>
patchSyntax(i, i.copy().syntax, importees.length == 1)
case i @ Importee.Given(_) =>
patchSyntax(i, i.copy().syntax, importees.length == 1)
case _ =>
}
syntax.toString

case Position.None =>
// Position not found, implies that `importer` is derived from certain existing import
Expand All @@ -742,7 +786,9 @@ class OrganizeImports(
def isCurlyBraced: Boolean = {
val importees @ Importees(_, renames, unimports, _, _, _) =
importer.importees
renames.nonEmpty || unimports.nonEmpty || importees.length > 1

importees.length > 1 ||
((renames.length == 1 || unimports.length == 1) && !targetDialect.allowAsForImportRename)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 2
removeUnused = false
targetDialect = Scala3
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 2
removeUnused = false
targetDialect = Scala3
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 2
removeUnused = false
targetDialect = Scala3
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 2
removeUnused = false
targetDialect = Scala3
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

import test.organizeImports.GivenImports.Beta
import test.organizeImports.GivenImports.Alpha
import test.organizeImports.GivenImports.{given Beta, given Alpha}
import test.organizeImports.GivenImports.{given Beta, given test.organizeImports.GivenImports.Gamma, given Alpha}
import test.organizeImports.GivenImports2.{given Beta, given}
import scala.util.Either

object ExpandGiven
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

import test.organizeImports.GivenImports._
import test.organizeImports.GivenImports.{alpha => _, given}
import test.organizeImports.GivenImports.*
import test.organizeImports.GivenImports.{alpha as _, given}
import test.organizeImports.GivenImports.{given Beta}
import test.organizeImports.GivenImports.{gamma => _, given}
import test.organizeImports.GivenImports.{gamma as _, given}
import test.organizeImports.GivenImports.{given Zeta}

import test.organizeImports.GivenImports2.{alpha => _}
import test.organizeImports.GivenImports2.{beta => _}
import test.organizeImports.GivenImports2.{alpha as _}
import test.organizeImports.GivenImports2.beta as _
import test.organizeImports.GivenImports2.{given Gamma}
import test.organizeImports.GivenImports2.{given Zeta}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = AggressiveMerge
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Scala3
*/
package test.organizeImports

import test.organizeImports.GivenImports.Beta
import test.organizeImports.GivenImports.Alpha
import test.organizeImports.GivenImports.{given Beta}
import test.organizeImports.GivenImports.given test.organizeImports.GivenImports.Gamma
import test.organizeImports.GivenImports.{given Alpha}
import scala.util.Either

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Scala3
*/

package test.organizeImports

import test.organizeImports.GivenImports.*
import test.organizeImports.GivenImports.{ given Alpha, given Beta }
import test.organizeImports.GivenImports2.{ given Gamma }

object MergeImportsFormatPreservingGiven
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 3
removeUnused = false
targetDialect = Auto
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

import scala.collection.{Map}
import scala.collection.{Seq => _}
import scala.collection.{Set => ImmutableSet}

object CurlyBracedSingleImportee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Explode
OrganizeImports.targetDialect = Auto
*/

package test.organizeImports

import test.organizeImports.ExplodeImports.FormatPreserving.g1.{ a, b }
import test.organizeImports.ExplodeImports.FormatPreserving.g2.{ c => C, _ }
import test.organizeImports.ExplodeImports.FormatPreserving.g2.{ c => C, _ }

object ExplodeImportsFormatPreserving
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = AggressiveMerge
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Auto
*/
package test.organizeImports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Keep
importsOrder = Keep
targetDialect = Auto
}
*/
package test.organizeImports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Keep
importsOrder = SymbolsFirst
targetDialect = Auto
}
*/
package test.organizeImports
Expand All @@ -15,6 +16,9 @@ import scala.concurrent.duration
import scala.concurrent.{Promise, Future}

import test.organizeImports.QuotedIdent.`a.b`.`{ d }`.e
import test.organizeImports.QuotedIdent.`a.b`.`{ d }`.{ e => E }
import test.organizeImports.QuotedIdent.`a.b`
import test.organizeImports.QuotedIdent.{`a.b` => ab}
import test.organizeImports.QuotedIdent.`a.b`.{c => _, _}
import test.organizeImports.QuotedIdent._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.targetDialect = Auto
*/

package test.organizeImports

import test.organizeImports.MergeImports.FormatPreserving.g1.{ a, b }
import test.organizeImports.MergeImports.FormatPreserving.g1.{ a, b => B }
import test.organizeImports.MergeImports.FormatPreserving.g2._
import test.organizeImports.MergeImports.FormatPreserving.g2.{ d => D }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test.organizeImports

import scala.collection.Map
import scala.collection.{Seq => _}
import scala.collection.{Set => ImmutableSet}

object CurlyBracedSingleImportee
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package test.organizeImports

import test.organizeImports.QuotedIdent._
import test.organizeImports.QuotedIdent.{`a.b` => ab}
import test.organizeImports.QuotedIdent.`a.b`
import test.organizeImports.QuotedIdent.`a.b`.{c => _, _}
import test.organizeImports.QuotedIdent.`a.b`.`{ d }`.{ e => E }
import test.organizeImports.QuotedIdent.`a.b`.`{ d }`.e

import scala.concurrent._
Expand Down
Loading

0 comments on commit e673412

Please sign in to comment.