Skip to content

Commit e2f2425

Browse files
olafurpgjulienrf
authored andcommitted
Almost complete with rewrite.
Blocked by - scalameta/scalameta#1037 - scalacenter/scalafix#252 - scalacenter/scalafix#253
1 parent 3d46cfa commit e2f2425

File tree

6 files changed

+89
-14
lines changed

6 files changed

+89
-14
lines changed

build.sbt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
def scalafixVersion = _root_.scalafix.Versions.version
12
// Use a scala version supported by scalafix.
23
scalaVersion in ThisBuild := org.scalameta.BuildInfo.supportedScalaVersions.last
34

@@ -6,7 +7,7 @@ lazy val root = project
67
.aggregate(rewrites, input, output, tests)
78

89
lazy val rewrites = project.settings(
9-
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % "0.4.2"
10+
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % scalafixVersion
1011
)
1112

1213
lazy val input = project.settings(
@@ -27,7 +28,7 @@ lazy val output = project
2728

2829
lazy val tests = project
2930
.settings(
30-
libraryDependencies += "ch.epfl.scala" % "scalafix-testkit" % "0.4.2" % Test cross CrossVersion.full,
31+
libraryDependencies += "ch.epfl.scala" % "scalafix-testkit" % scalafixVersion % Test cross CrossVersion.full,
3132
buildInfoPackage := "fix",
3233
buildInfoKeys := Seq[BuildInfoKey](
3334
"inputSourceroot" ->

input/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
/* ONLY
22
rewrite = "scala:fix.Collectionstrawman_v0"
3+
patches.moveSymbols = [
4+
{ from = "scala.collection.immutable.HashMap",
5+
to = "strawman.collection.immutable.HashMap" }
6+
{ from = "scala.collection.immutable.Map",
7+
to = "strawman.collection.immutable.Map" }
8+
{ from = "scala.Predef.Map",
9+
to = "strawman.collection.immutable.Map" }
10+
{ from = "scala.collection.immutable.List",
11+
to = "strawman.collection.immutable.List" }
12+
{ from = "scala.collection.immutable.Nil",
13+
to = "strawman.collection.immutable.Nil" }
14+
{ from = "scala.package.Stream",
15+
to = "strawman.collection.immutable.LazyList" }
16+
{ from = "scala.package.`#::`",
17+
to = "strawman.collection.immutable.LazyList.`#::`" }
18+
{ from = "scala.package.Vector",
19+
to = "strawman.collection.immutable.Vector" }
20+
{ from = "scala.collection.mutable.ArrayBuffer",
21+
to = "strawman.collection.mutable.ArrayBuffer" }
22+
]
323
*/
424
package fix
525

output/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ object Collectionstrawman_v0_Seq {
3434

3535
object Collectionstrawman_v0_Map {
3636
val xs: Map[Int, String] = Map(1 -> "1", 2 -> "2", 3 -> "3")
37-
import strawman.collection.immutable.HashMap
3837
val ys = HashMap.empty
3938
}
4039

4140
object Collectionstrawman_v0_ArrayBuffer {
42-
import strawman.collection.mutable.ArrayBuffer
4341
val xs: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
4442
}
4543

project/plugins.sbt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.4.2")
2-
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.4.2")
3-
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-RC3")
1+
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.0-M1")
2+
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-RC6")
43
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
54

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
11
package fix
2+
// scalafmt: { maxColumn = 120 }
23

34
import scalafix._
5+
import scalafix.syntax._
6+
import scalafix.internal.util.SymbolOps
47
import scala.meta._
58

6-
case class Collectionstrawman_v0(mirror: Mirror)
7-
extends SemanticRewrite(mirror) {
9+
case class Collectionstrawman_v0(mirror: Mirror) extends SemanticRewrite(mirror) {
810
val immutableListSymbol = Symbol("_root_.scala.collection.immutable.List.")
11+
val unimports = Set(
12+
Symbol("_root_.scala.List."),
13+
Symbol("_root_.scala.Seq."),
14+
Symbol("_root_.scala.Vector."),
15+
Symbol("_root_.scala.`::`."),
16+
Symbol("_root_.scala.`#::`."),
17+
Symbol("_root_.scala.Predef.Map."),
18+
Symbol("_root_.scala.Predef.intArrayOps."),
19+
Symbol("_root_.scala.Predef.augmentString."),
20+
Symbol("_root_.scala.Predef.intArrayOps.")
21+
)
22+
23+
def normalize(symbol: Symbol): Symbol = {
24+
def loop(symbol: Symbol): Symbol = symbol match {
25+
case Symbol.Global(qual, Signature.Term("package")) => loop(qual)
26+
case Symbol.Global(qual, name) => Symbol.Global(loop(qual), name)
27+
case x => x
28+
}
29+
loop(symbol.normalized)
30+
}
31+
32+
def ifSymbolFound(ctx: RewriteCtx): Patch = {
33+
val toUnimport = ctx.mirror.database.names.flatMap {
34+
case (_, sym) =>
35+
val norm = normalize(sym)
36+
if (unimports.contains(norm)) norm :: Nil
37+
else Nil
38+
}
39+
val unimportss = toUnimport.toList.distinct.flatMap { sym =>
40+
SymbolOps.toImporter(sym).toList.collect {
41+
case Importer(qual, Importee.Name(n) :: Nil) =>
42+
Importer(qual, Importee.Unimport(n) :: Nil)
43+
}
44+
}
45+
val grouped = unimportss.groupBy(_.ref.syntax).collect {
46+
case (qual, iss @ is :: _) =>
47+
val names = iss.collect { case Importer(_, name :: Nil) => name }
48+
Importer(is.ref, names)
49+
}
50+
grouped.map(ctx.addGlobalImport(_)).asPatch
51+
}
52+
53+
// def isSymbol(tree: Tree, symbol: Symbol): Boolean = {
54+
// mirror.database.names.get(tree.pos).exists(normalize(_) == symbol)
55+
// }
56+
57+
def rangePatch(ctx: RewriteCtx): Patch = {
58+
// ctx.tree.collect {
59+
// case q"$lhs $op $rhs" if isSymbol(op, Symbol()) =>
60+
// op
61+
// }
62+
Patch.empty
63+
}
64+
965
def rewrite(ctx: RewriteCtx): Patch = {
10-
ctx.tree.collect {
11-
case name @ Term.Name(_) if name.symbol == immutableListSymbol =>
12-
ctx.addGlobalImport(importer"scala.{List => _}") +
13-
ctx.addGlobalImport(importer"strawman.collection.immutable.List")
14-
}.asPatch
66+
ctx.debugMirror()
67+
ifSymbolFound(ctx)
1568
}
1669
}

tests/src/test/scala/fix/Collectionstrawman_Tests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ class Collectionstrawman_Tests
99
AbsolutePath(BuildInfo.inputSourceroot),
1010
Seq(AbsolutePath(BuildInfo.outputSourceroot))
1111
) {
12+
override def assertNoDiff(a: String, b: String, c: String) = {
13+
println(a)
14+
super.assertNoDiff(a, b, c)
15+
}
1216
runAllTests()
1317
}

0 commit comments

Comments
 (0)