|
1 | 1 | package fix
|
2 | 2 | // scalafmt: { maxColumn = 120 }
|
3 | 3 |
|
| 4 | +import scala.collection.JavaConverters._ |
4 | 5 | import scalafix._
|
5 | 6 | import scalafix.syntax._
|
6 | 7 | import scalafix.internal.util.SymbolOps
|
7 | 8 | import scala.meta._
|
| 9 | +import org.scalameta.logger |
8 | 10 |
|
9 | 11 | case class Collectionstrawman_v0(mirror: SemanticCtx) extends SemanticRewrite(mirror) {
|
10 | 12 | 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.augmentStrugming."), |
19 |
| - Symbol("_root_.scala.Predef.intArrayOps.") |
| 13 | + val unimports = Map( |
| 14 | + Symbol("_root_.scala.Predef.augmentString.") -> |
| 15 | + Symbol("_root_.strawman.collection.stringToStringOps."), |
| 16 | + Symbol("_root_.scala.Predef.intArrayOps.") -> |
| 17 | + Symbol("_root_.strawman.collection.arrayToArrayOps.") |
20 | 18 | )
|
21 | 19 |
|
22 | 20 | def ifSymbolFound(ctx: RewriteCtx): Patch = {
|
23 |
| - val toUnimport = ctx.mirror.database.names.flatMap { r => |
24 |
| - val norm = r.sym.normalized |
25 |
| - if (unimports.contains(norm)) norm :: Nil |
26 |
| - else Nil |
27 |
| - } |
28 |
| - val unimportss = toUnimport.toList.distinct.flatMap { sym => |
29 |
| - SymbolOps.toImporter(sym).toList.collect { |
30 |
| - case Importer(qual, Importee.Name(n) :: Nil) => |
31 |
| - Importer(qual, Importee.Unimport(n) :: Nil) |
32 |
| - } |
33 |
| - } |
34 |
| - val grouped = unimportss.groupBy(_.ref.syntax).collect { |
35 |
| - case (qual, iss @ is :: _) => |
36 |
| - val names = iss.collect { case Importer(_, name :: Nil) => name } |
37 |
| - Importer(is.ref, names) |
38 |
| - } |
39 |
| - grouped.map(ctx.addGlobalImport(_)).asPatch |
| 21 | + logger.elem(ctx.mirror.names) |
| 22 | + val toImport = ctx.mirror.names |
| 23 | + .flatMap(r => |
| 24 | + unimports.get { |
| 25 | + val x = r.sym.normalized |
| 26 | + logger.elem(x) |
| 27 | + x |
| 28 | + }) |
| 29 | + .map(ctx.addGlobalImport) |
| 30 | + toImport.asPatch |
40 | 31 | }
|
41 | 32 |
|
42 | 33 | def rewrite(ctx: RewriteCtx): Patch = {
|
43 | 34 | def p(name: String) =
|
44 | 35 | s"scala.Predef.$name" -> s"strawman.collection.immutable.$name"
|
45 |
| - def s(name: String) = |
46 |
| - s"scala.$name" -> s"strawman.collection.immutable.$name" |
| 36 | + def s(name: String, rename: Option[String] = None) = |
| 37 | + s"scala.$name" -> s"strawman.collection.immutable.${rename.getOrElse(name)}" |
47 | 38 | def i(name: String) =
|
48 | 39 | s"scala.collection.immutable.$name" -> s"strawman.collection.immutable.$name"
|
49 | 40 | def m(name: String) =
|
50 | 41 | s"scala.collection.mutable.$name" -> s"strawman.collection.mutable.$name"
|
51 |
| -// ifSymbolFound(ctx) + |
52 |
| - ctx.replaceSymbols( |
53 |
| - i("HashMap"), |
54 |
| - i("Map"), |
55 |
| - p("Map"), |
56 |
| - s("List"), |
57 |
| - i("List"), |
58 |
| - s("Nil"), |
59 |
| - i("Nil"), |
60 |
| - s("Stream"), |
61 |
| - s("`#::`"), |
62 |
| - s("Vector"), |
63 |
| - i("Vector"), |
64 |
| - m("ArrayBuffer") |
65 |
| - ) |
| 42 | + ifSymbolFound(ctx) + |
| 43 | + ctx.replaceSymbols( |
| 44 | + i("HashMap"), |
| 45 | + i("Map"), |
| 46 | + p("Map"), |
| 47 | + s("List"), |
| 48 | + i("List"), |
| 49 | + s("Nil"), |
| 50 | + i("Nil"), |
| 51 | + s("`::`"), |
| 52 | + i("`::`"), |
| 53 | + s("`+:`"), |
| 54 | + i("`+:`"), |
| 55 | + s("`:+`"), |
| 56 | + i("`:+`"), |
| 57 | + s("Stream", Some("LazyList")), |
| 58 | + s("`#::`"), |
| 59 | + s("`#::`"), |
| 60 | + s("Vector"), |
| 61 | + i("Vector"), |
| 62 | + m("ArrayBuffer") |
| 63 | + ) |
66 | 64 | }
|
67 | 65 | }
|
0 commit comments