Skip to content

Commit

Permalink
Simplify import patch api.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Jan 28, 2017
1 parent 22522f1 commit fd40924
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/scalafix/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package object syntax {
def supersedes(patch: ImportPatch): Boolean =
i.ref.structure == patch.importer.ref.structure &&
(i.importee.is[Importee.Wildcard] ||
i.importee.structure == patch.importer.importee.structure)
i.importee.structure == patch.importee.structure)
}

implicit class XtensionToken(token: Token) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/scalafix/util/OrganizeImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ private[this] class OrganizeImports private (implicit ctx: RewriteCtx) {
patch match {
case _: AddGlobalImport =>
if (is.exists(_.supersedes(patch))) is
else is :+ patch.importer
else is ++ getCanonicalImports(patch.toImport)
case remove: RemoveGlobalImport =>
is.filter(_.structure == remove.importer.structure)
is.filterNot(_.structure == remove.importer.structure)
}
patches.foldLeft(removeUnused(globalImports))(combine)
}
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/scala/scalafix/util/Patch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ abstract class TokenPatch(val tok: Token, val newTok: String)
s"TokenPatch(${logger.reveal(tok.syntax)}, ${tok.structure}, $newTok)"
}

abstract class ImportPatch(val importer: CanonicalImport) extends TreePatch
abstract class ImportPatch(val importer: Importer) extends TreePatch {
def importee: Importee = importer.importees.head
def toImport: Import = Import(Seq(importer))
}
object TreePatch {
case class RemoveGlobalImport(override val importer: CanonicalImport)
case class RemoveGlobalImport(override val importer: Importer)
extends ImportPatch(importer)
case class AddGlobalImport(override val importer: CanonicalImport)
case class AddGlobalImport(override val importer: Importer)
extends ImportPatch(importer)
}

Expand Down
14 changes: 9 additions & 5 deletions scalafix-nsc/src/test/scala/scalafix/ImportPatches.sc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Small worksheet to demonstrate usage of import patches
import scala.collection.immutable.Seq
import scala.meta._
import scalafix.rewrite.RewriteCtx
import scalafix.util.CanonicalImport
import scalafix.util.Patch
import scalafix.util.TreePatch.AddGlobalImport

import scalafix.util.TreePatch.RemoveGlobalImport
import scala.meta._
import scalafix.util.logger
val code =
"""import scala.language.higherKinds
|import java.{util => ju}
Expand All @@ -13,7 +15,9 @@ val code =
|object a
""".stripMargin.parse[Source].get
implicit val ctx: RewriteCtx = RewriteCtx.fromCode(code)
val patch = AddGlobalImport(CanonicalImport.fromImportee(q"cats.data", importee"EitherT"))

val x = Patch.apply(code, Seq(patch))
val add = AddGlobalImport(importer"cats.data.EitherT")
val addRedundant = AddGlobalImport(importer"scala.collection.mutable._")
val remove = RemoveGlobalImport(importer"scala.language.higherKinds")

val x = Patch.apply(code, Seq(add, addRedundant, remove))
logger.elem(x)
4 changes: 4 additions & 0 deletions scalafix-nsc/src/test/scala/scalafix/SemanticTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import scala.{meta => m}
import scalafix.nsc.ScalafixNscPlugin
import scalafix.rewrite.ExplicitImplicit
import scalafix.rewrite.Rewrite
import scalafix.rewrite.RewriteCtx
import scalafix.util.DiffAssertions
import scalafix.util.FileOps
import scalafix.util.Patch
import scalafix.util.TreePatch.AddGlobalImport
import scalafix.util.TreePatch.RemoveGlobalImport
import scalafix.util.logger

import org.scalatest.FunSuite
Expand Down

0 comments on commit fd40924

Please sign in to comment.