@@ -30,6 +30,18 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
30
30
Symbol (" _root_.scala.runtime.Tuple2Zipped.Ops.zipped." ),
31
31
Symbol (" _root_.scala.runtime.Tuple3Zipped.Ops.zipped." )
32
32
)
33
+ val setPlus =
34
+ SymbolMatcher .exact(
35
+ Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/Set;." )
36
+ )
37
+ val setMinus =
38
+ SymbolMatcher .exact(
39
+ Symbol (" _root_.scala.collection.SetLike#`-`(Ljava/lang/Object;)Lscala/collection/Set;." )
40
+ )
41
+ val mapPlus =
42
+ SymbolMatcher .exact(
43
+ Symbol (" _root_.scala.collection.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/Map;." )
44
+ )
33
45
val setPlus2 = SymbolMatcher .exact(
34
46
Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;Ljava/lang/Object;Lscala/collection/Seq;)Lscala/collection/Set;." )
35
47
)
@@ -72,6 +84,9 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
72
84
Symbol (" _root_.scala.collection.mutable.SetLike.retain." )
73
85
)
74
86
87
+ def startsWithParens (tree : Tree ): Boolean =
88
+ tree.tokens.headOption.map(_.is[Token .LeftParen ]).getOrElse(false )
89
+
75
90
def replaceMutableSet (ctx : RuleCtx ) =
76
91
ctx.tree.collect {
77
92
case retainSet(n : Name ) =>
@@ -182,7 +197,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
182
197
def replaceSetMapPlus2 (ctx : RuleCtx ): Patch = {
183
198
def rewritePlus (ap : Term .ApplyInfix , lhs : Term , op : Term .Name , rhs1 : Term , rhs2 : Term ): Patch = {
184
199
val tokensToReplace =
185
- if (ap.tokens.headOption.map(_.is[ Token . LeftParen ]).getOrElse( false )) {
200
+ if (startsWithParens(ap )) {
186
201
// don't drop surrounding parens
187
202
ap.tokens.slice(1 , ap.tokens.size - 1 )
188
203
} else ap.tokens
@@ -207,6 +222,33 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
207
222
}.asPatch
208
223
}
209
224
225
+ def replaceSetMapPlusMinus (ctx : RuleCtx ): Patch = {
226
+ def rewriteOp (op : Tree , rhs : Tree , doubleOp : String , col0 : String ): Patch = {
227
+ val col = " _root_.scala.collection." + col0
228
+ val callSite =
229
+ if (startsWithParens(rhs)) {
230
+ ctx.addLeft(rhs, col)
231
+ }
232
+ else {
233
+ ctx.addLeft(rhs, col + " (" ) +
234
+ ctx.addRight(rhs, " )" )
235
+ }
236
+
237
+ ctx.addRight(op, doubleOp) + callSite
238
+ }
239
+
240
+ ctx.tree.collect {
241
+ case Term .ApplyInfix (_, op @ setPlus(_), Nil , List (rhs)) =>
242
+ rewriteOp(op, rhs, " +" , " Set" )
243
+
244
+ case Term .ApplyInfix (lhs, op @ setMinus(_), Nil , List (rhs)) =>
245
+ rewriteOp(op, rhs, " -" , " Set" )
246
+
247
+ case Term .ApplyInfix (lhs, op @ mapPlus(_), Nil , List (rhs)) =>
248
+ rewriteOp(op, rhs, " +" , " Map" )
249
+ }.asPatch
250
+ }
251
+
210
252
def replaceMutSetMapPlus (ctx : RuleCtx ): Patch = {
211
253
def rewriteMutPlus (lhs : Term , op : Term .Name ): Patch = {
212
254
ctx.addRight(lhs, " .clone()" ) +
@@ -241,8 +283,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
241
283
242
284
243
285
override def fix (ctx : RuleCtx ): Patch = {
244
- // println(ctx.index.database)
245
-
246
286
replaceToList(ctx) +
247
287
replaceSymbols(ctx) +
248
288
replaceTupleZipped(ctx) +
@@ -252,6 +292,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
252
292
replaceMutableSet(ctx) +
253
293
replaceSymbolicFold(ctx) +
254
294
replaceSetMapPlus2(ctx) +
295
+ replaceSetMapPlusMinus(ctx) +
255
296
replaceMutSetMapPlus(ctx) +
256
297
replaceMutMapUpdated(ctx) +
257
298
replaceIterableSameElements(ctx)
0 commit comments