@@ -30,6 +30,8 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
30
30
Symbol (" _root_.scala.Predef.Map#" )
31
31
)
32
32
33
+ val CollectionSet : TypeMatcher = TypeMatcher (Symbol (" _root_.scala.collection.Set#" ))
34
+
33
35
def replaceSymbols (ctx : RuleCtx ): Patch = {
34
36
ctx.replaceSymbols(
35
37
" scala.collection.LinearSeq" -> " scala.collection.immutable.List" ,
@@ -52,6 +54,18 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
52
54
Symbol (" _root_.scala.runtime.Tuple2Zipped.Ops.zipped." ),
53
55
Symbol (" _root_.scala.runtime.Tuple3Zipped.Ops.zipped." )
54
56
)
57
+ val setPlus =
58
+ SymbolMatcher .exact(
59
+ Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/Set;." )
60
+ )
61
+ val setMinus =
62
+ SymbolMatcher .exact(
63
+ Symbol (" _root_.scala.collection.SetLike#`-`(Ljava/lang/Object;)Lscala/collection/Set;." )
64
+ )
65
+ val mapPlus =
66
+ SymbolMatcher .exact(
67
+ Symbol (" _root_.scala.collection.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/Map;." )
68
+ )
55
69
val setPlus2 = SymbolMatcher .exact(
56
70
Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;Ljava/lang/Object;Lscala/collection/Seq;)Lscala/collection/Set;." )
57
71
)
@@ -110,6 +124,9 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
110
124
Symbol (" _root_.scala.collection.IterableLike#zip(Lscala/collection/GenIterable;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;." )
111
125
)
112
126
127
+ def startsWithParens (tree : Tree ): Boolean =
128
+ tree.tokens.headOption.map(_.is[Token .LeftParen ]).getOrElse(false )
129
+
113
130
def replaceMutableSet (ctx : RuleCtx ) =
114
131
ctx.tree.collect {
115
132
case retainSet(n : Name ) =>
@@ -220,7 +237,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
220
237
def replaceSetMapPlus2 (ctx : RuleCtx ): Patch = {
221
238
def rewritePlus (ap : Term .ApplyInfix , lhs : Term , op : Term .Name , rhs1 : Term , rhs2 : Term ): Patch = {
222
239
val tokensToReplace =
223
- if (ap.tokens.headOption.map(_.is[ Token . LeftParen ]).getOrElse( false )) {
240
+ if (startsWithParens(ap )) {
224
241
// don't drop surrounding parens
225
242
ap.tokens.slice(1 , ap.tokens.size - 1 )
226
243
} else ap.tokens
@@ -245,6 +262,34 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
245
262
}.asPatch
246
263
}
247
264
265
+ def replaceSetMapPlusMinus (ctx : RuleCtx ): Patch = {
266
+ def rewriteOp (op : Tree , rhs : Tree , doubleOp : String , col0 : String ): Patch = {
267
+ val col = " _root_.scala.collection." + col0
268
+ val callSite =
269
+ if (startsWithParens(rhs)) {
270
+ ctx.addLeft(rhs, col)
271
+ }
272
+ else {
273
+ ctx.addLeft(rhs, col + " (" ) +
274
+ ctx.addRight(rhs, " )" )
275
+ }
276
+
277
+ ctx.addRight(op, doubleOp) + callSite
278
+ }
279
+
280
+ ctx.tree.collect {
281
+ case Term .ApplyInfix (CollectionSet (), op @ setPlus(_), Nil , List (rhs)) =>
282
+ rewriteOp(op, rhs, " +" , " Set" )
283
+
284
+ case Term .ApplyInfix (CollectionSet (), op @ setMinus(_), Nil , List (rhs)) =>
285
+ rewriteOp(op, rhs, " -" , " Set" )
286
+
287
+ case Term .ApplyInfix (_, op @ mapPlus(_), Nil , List (rhs)) =>
288
+ rewriteOp(op, rhs, " +" , " Map" )
289
+ }.asPatch
290
+ }
291
+
292
+
248
293
def replaceMutSetMapPlus (ctx : RuleCtx ): Patch = {
249
294
def rewriteMutPlus (lhs : Term , op : Term .Name ): Patch = {
250
295
ctx.addRight(lhs, " .clone()" ) +
@@ -299,7 +344,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
299
344
ctx.addRight(ap, " .toMap" )
300
345
}.asPatch
301
346
}
302
-
347
+
303
348
override def fix (ctx : RuleCtx ): Patch = {
304
349
replaceToList(ctx) +
305
350
replaceSymbols(ctx) +
@@ -310,6 +355,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
310
355
replaceMutableSet(ctx) +
311
356
replaceSymbolicFold(ctx) +
312
357
replaceSetMapPlus2(ctx) +
358
+ replaceSetMapPlusMinus(ctx) +
313
359
replaceMutSetMapPlus(ctx) +
314
360
replaceMutMapUpdated(ctx) +
315
361
replaceIterableSameElements(ctx) +
0 commit comments