@@ -2,10 +2,11 @@ package fix
2
2
3
3
import scalafix ._
4
4
import scalafix .syntax ._
5
+ import scalafix .util ._
5
6
import scala .meta ._
6
7
7
- case class Collectionstrawman_v0 (sctx : SemanticCtx )
8
- extends SemanticRewrite (sctx ) {
8
+ case class Collectionstrawman_v0 (index : SemanticdbIndex )
9
+ extends SemanticRule (index, " Collectionstrawman_v0 " ) {
9
10
10
11
implicit class XtensionSymbolCollection (symbol : Symbol ) {
11
12
def name = symbol match {
@@ -27,10 +28,10 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
27
28
" intArrayOps" -> " genericArrayOps"
28
29
)
29
30
30
- def replaceExtensionMethods (ctx : RewriteCtx ): Patch = {
31
+ def replaceExtensionMethods (ctx : RuleCtx ): Patch = {
31
32
val toImport = for {
32
- r <- ctx.semanticCtx .names
33
- in = r.sym .normalized
33
+ r <- ctx.index .names
34
+ in = r.symbol .normalized
34
35
out <- unimports.get(in).toList
35
36
} yield {
36
37
val name = in.name
@@ -51,20 +52,20 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
51
52
" _root_.scala.runtime.RichInt#to(I)Lscala/collection/immutable/Range/Inclusive;." )
52
53
val rangeSymbol = Symbol (
53
54
" _root_.scala.runtime.RichInt#until(I)Lscala/collection/immutable/Range;." )
54
- def replaceRange (ctx : RewriteCtx ): Patch = {
55
+ def replaceRange (ctx : RuleCtx ): Patch = {
55
56
ctx.tree.collect {
56
- case tree @ Term .ApplyInfix (lhs, op, targs , arg :: Nil )
57
+ case tree @ Term .ApplyInfix (lhs, op, _ , arg :: Nil )
57
58
if op.symbol.contains(inclusiveRange) =>
58
59
ctx.replaceTree(tree, q " Range.inclusive( $lhs, $arg) " .syntax) +
59
60
ctx.addGlobalImport(rangeImport)
60
- case tree @ Term .ApplyInfix (lhs, op, targs , arg :: Nil )
61
+ case tree @ Term .ApplyInfix (lhs, op, _ , arg :: Nil )
61
62
if op.symbol.contains(rangeSymbol) =>
62
63
ctx.replaceTree(tree, q " Range( $lhs, $arg) " .syntax) +
63
64
ctx.addGlobalImport(rangeImport)
64
65
}
65
66
}.asPatch
66
67
67
- def replaceSymbols (ctx : RewriteCtx ): Patch = {
68
+ def replaceSymbols (ctx : RuleCtx ): Patch = {
68
69
def p (name : String ) =
69
70
s " scala.Predef. $name" -> s " strawman.collection.immutable. $name"
70
71
def s (name : String , rename : Option [String ] = None ) =
@@ -101,57 +102,40 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
101
102
)
102
103
}
103
104
104
- case class SymbolMatcher (symbols : Symbol * ) {
105
- def unapply (arg : Tree ): Option [(Tree , Symbol )] =
106
- sctx.symbol(arg.pos).flatMap { sym =>
107
- if (symbols.exists(_.isSameNormalized(sym))) Some (arg -> sym)
108
- else None
109
- }
110
- }
111
-
112
- object WithSymbol {
113
- def unapply (arg : Tree ): Option [(Tree , Symbol )] =
114
- sctx.symbol(arg.pos).map(x => arg -> x)
115
- }
116
-
117
- val toGenericX = SymbolMatcher (
105
+ val toGenericX = SymbolMatcher .normalized(
118
106
Symbol (" _root_.scala.collection.TraversableOnce.toMap." )
119
107
)
120
- val toImmutableX = SymbolMatcher (
108
+ val toImmutableX = SymbolMatcher .normalized (
121
109
Symbol (" _root_.scala.collection.TraversableOnce.toList." ),
122
110
Symbol (" _root_.scala.collection.TraversableOnce.toSet." )
123
111
)
124
- val toTpe = SymbolMatcher (
112
+ val toTpe = SymbolMatcher .normalized (
125
113
Symbol (" _root_.scala.collection.TraversableLike.to." )
126
114
)
127
- val iterator = SymbolMatcher (
115
+ val iterator = SymbolMatcher .normalized (
128
116
Symbol (" _root_.scala.collection.LinearSeqLike.iterator." ),
129
117
Symbol (" _root_.scala.collection.TraversableLike.toIterator." )
130
118
)
131
119
132
- def replaceToList (ctx : RewriteCtx ) =
120
+ def replaceToList (ctx : RuleCtx ) =
133
121
ctx.tree.collect {
134
- case iterator(n : Name , _) =>
135
- ctx.replaceTree(n, " iterator()" )
136
- case toImmutableX(n : Name , s) =>
137
- ctx.replaceTree(n, s " to(strawman.collection.immutable. ${s.name.stripPrefix(" to" )}) " )
138
- case toGenericX(n : Name , s) =>
139
- ctx.replaceTree(n, s " to(strawman.collection. ${s.name.stripPrefix(" to" )}) " )
140
- case toTpe(n : Name , _) =>
141
- ctx.debug(n)
122
+ case iterator(t : Name ) =>
123
+ ctx.replaceTree(t, " iterator()" )
124
+ case toImmutableX(t @ Name (n)) =>
125
+ ctx.replaceTree(t, s " to(strawman.collection.immutable. ${n.stripPrefix(" to" )}) " )
126
+ case toGenericX(t @ Name (n)) =>
127
+ ctx.replaceTree(t, s " to(strawman.collection. ${n.stripPrefix(" to" )}) " )
128
+ case toTpe(n : Name ) =>
142
129
(for {
143
130
name <- n.tokens.lastOption
144
- _ = ctx.debug(name)
145
131
open <- ctx.tokenList.find(name)(t => t.is[Token .LeftBracket ])
146
- _ = ctx.debug(open)
147
- close <- ctx.matching.close(open.asInstanceOf [Token .LeftBracket ])
148
- replacedTokens = ctx.tokenList.slice(open, close)
132
+ close <- ctx.matchingParens.close(open.asInstanceOf [Token .LeftBracket ])
149
133
} yield
150
134
ctx.replaceToken(open, " (" ) +
151
135
ctx.replaceToken(close, " )" )).getOrElse(Patch .empty)
152
136
}.asPatch
153
137
154
- def rewrite (ctx : RewriteCtx ): Patch = {
138
+ override def fix (ctx : RuleCtx ): Patch = {
155
139
replaceToList(ctx) +
156
140
replaceSymbols(ctx) +
157
141
replaceExtensionMethods(ctx) +
0 commit comments