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 20, 2024
1 parent 77eca36 commit a714b6a
Show file tree
Hide file tree
Showing 66 changed files with 232 additions and 27 deletions.
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,15 @@
/*
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.GivenImports2.{given Beta}
import test.organizeImports.GivenImports2.{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,6 +2,7 @@
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
OrganizeImports.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 = 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
@@ -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 @@ -4,6 +4,7 @@ OrganizeImports {
expandRelative = true
groupedImports = Explode
removeUnused = false
targetDialect = Auto
}
*/
import P._
Expand Down
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,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
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.organizeImports

import scala.collection.immutable.{Map, Seq, Vector}
import scala.collection.mutable.*
import scala.concurrent.{Channel as Ch, *}
import scala.util.{Random as _, *}

object CoalesceImportees
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test.organizeImports

import test.organizeImports.Givens._
import test.organizeImports.Givens.{B => B1, C => _, _, given}
import test.organizeImports.Givens.*
import test.organizeImports.Givens.{B as B1, C as _, *, given}

object CoalesceImporteesGivensAndNames
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package test.organizeImports

import test.organizeImports.Givens.{C => C1, _}
import test.organizeImports.Givens.{C as C1, *}

object CoalesceImporteesNoGivens
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package test.organizeImports

import test.organizeImports.Givens.{A => A1, B => _, _}
import test.organizeImports.Givens.{A as A1, B as _, *}

object CoalesceImporteesNoGivensNoNames
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test.organizeImports

import test.organizeImports.Givens._
import test.organizeImports.Givens.{A => A1, given}
import test.organizeImports.Givens.*
import test.organizeImports.Givens.{A as A1, given}

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

import scala.collection.Map
import scala.collection.Seq as _
import scala.collection.Set as ImmutableSet

object CurlyBracedSingleImportee
Loading

0 comments on commit a714b6a

Please sign in to comment.