Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Fix a corner case about quoted identifiers containing curly braces (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
liancheng authored May 4, 2020
1 parent becbc0c commit c7bb8d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
5 changes: 2 additions & 3 deletions input/src/main/scala/fix/ExpandRelativeQuotedIdent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package fix

import QuotedIdent.`a.b`
import `a.b`.c
import `a.b`.`{ d }`

object ExpandRelativeQuotedIdent {
val refC = c
}
object ExpandRelativeQuotedIdent
5 changes: 2 additions & 3 deletions output/src/main/scala/fix/ExpandRelativeQuotedIdent.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package fix

import fix.QuotedIdent.`a.b`
import fix.QuotedIdent.`a.b`.`{ d }`
import fix.QuotedIdent.`a.b`.c

object ExpandRelativeQuotedIdent {
val refC = c
}
object ExpandRelativeQuotedIdent
29 changes: 21 additions & 8 deletions rules/src/main/scala/fix/OrganizeImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class OrganizeImports(config: OrganizeImportsConfig) extends SemanticRule("Organ
case Explode => explodeImportees(importers)
case Keep => importers
}
} map coalesceImportees map sortImportees
} map (coalesceImportees _ andThen sortImportees _)

config.importsOrder match {
case Ascii =>
Expand Down Expand Up @@ -252,17 +252,30 @@ object OrganizeImports {

private def prettyPrintImportGroup(group: Seq[Importer]): String =
group
.map(fixedImporterSyntax)
.map("import " + _)
.map { i => "import " + fixedImporterSyntax(i) }
.mkString("\n")

// Hack: The scalafix pretty-printer decides to add spaces after open and before close braces in
// HACK: The scalafix pretty-printer decides to add spaces after open and before close braces in
// imports, i.e., "import a.{ b, c }" instead of "import a.{b, c}". Unfortunately, this behavior
// cannot be overriden. This function removes the unwanted spaces as a workaround.
private def fixedImporterSyntax(importer: Importer): String =
importer.syntax
.replace("{ ", "{")
.replace(" }", "}")
private def fixedImporterSyntax(importer: Importer): String = {
// NOTE: We need to check whether the input importer is curly braced first and then replace the
// first "{ " and the last " }" if any. Naive string replacements is not sufficient, e.g., a
// quoted-identifier like "`{ d }`" may cause broken output.
val isCurlyBraced = importer.importees match {
case Importees(_, _ :: _, _, _) => true // At least one rename
case Importees(_, _, _ :: _, _) => true // At least one unimport
case importees if importees.length > 1 => true // Multiple importees
case _ => false
}

val syntax = importer.syntax

(isCurlyBraced, syntax lastIndexOfSlice " }") match {
case (true, index) if index > -1 => syntax.patch(index, "}", 2).replaceFirst("\\{ ", "{")
case _ => syntax
}
}

@tailrec private def topQualifierOf(term: Term): Term.Name =
term match {
Expand Down
1 change: 1 addition & 0 deletions shared/src/main/scala/fix/QuotedIdent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package fix
object QuotedIdent {
object `a.b` {
object c
object `{ d }`
}
}

0 comments on commit c7bb8d2

Please sign in to comment.