@@ -66,7 +66,7 @@ case class CanBuildFrom(param: Name, cbf: Type) {
66
66
}.asPatch
67
67
68
68
val parameterType =
69
- ctx.replaceTree(cbf, " collection. BuildFrom" )
69
+ ctx.replaceTree(cbf, " BuildFrom" )
70
70
71
71
parameterType + cbfCalls
72
72
}
@@ -78,37 +78,28 @@ object CanBuildFromNothing {
78
78
ctx : RuleCtx ,
79
79
collectionCanBuildFrom : SymbolMatcher ,
80
80
nothing : SymbolMatcher ,
81
- toTpe : SymbolMatcher )(implicit index : SemanticdbIndex ): (Patch , Set [Tree ]) = {
82
- val handledTo = Set .newBuilder[Tree ]
83
-
84
- val patches =
85
- paramss.flatten.collect{
86
- case
87
- Term .Param (
88
- List (Mod .Implicit ()),
89
- param,
90
- Some (
91
- tpe @ Type .Apply (
92
- collectionCanBuildFrom(_),
93
- List (
94
- nothing(_),
95
- t,
96
- cct @ Type .Apply (
97
- cc,
98
- _
99
- )
81
+ toTpe : SymbolMatcher )(implicit index : SemanticdbIndex ): Patch = {
82
+ paramss.flatten.collect{
83
+ case
84
+ Term .Param (
85
+ List (Mod .Implicit ()),
86
+ param,
87
+ Some (
88
+ tpe @ Type .Apply (
89
+ collectionCanBuildFrom(_),
90
+ List (
91
+ nothing(_),
92
+ t,
93
+ cct @ Type .Apply (
94
+ cc,
95
+ _
100
96
)
101
97
)
102
- ),
103
- _
104
- ) => new CanBuildFromNothing (param, tpe, t, cct, cc, body, ctx, toTpe)
105
- }.map{cbf =>
106
- val (ps, ht) = cbf.toFactory
107
- handledTo ++= ht
108
- ps
109
- }.asPatch
110
-
111
- (patches, handledTo.result())
98
+ )
99
+ ),
100
+ _
101
+ ) => new CanBuildFromNothing (param, tpe, t, cct, cc, body, ctx, toTpe)
102
+ }.map(_.toFactory).asPatch
112
103
}
113
104
}
114
105
@@ -129,9 +120,7 @@ case class CanBuildFromNothing(param: Name,
129
120
body : Term ,
130
121
ctx : RuleCtx ,
131
122
toTpe : SymbolMatcher ) {
132
- def toFactory (implicit index : SemanticdbIndex ): (Patch , Set [Tree ]) = {
133
- val handledTo = Set .newBuilder[Tree ]
134
-
123
+ def toFactory (implicit index : SemanticdbIndex ): Patch = {
135
124
val matchCbf = SymbolMatcher .exact(ctx.index.symbol(param).get)
136
125
137
126
// cbf() / cbf.apply => cbf.newBuilder
@@ -160,11 +149,10 @@ case class CanBuildFromNothing(param: Name,
160
149
161
150
val matchCC = SymbolMatcher .exact(ctx.index.symbol(cc).get)
162
151
163
- // e.to[CC] => e.to(cbf )
152
+ // e.to[CC] => cbf.fromSpecific(e )
164
153
val toCalls =
165
154
body.collect {
166
- case ap @ Term .ApplyType (Term .Select (_, to @ toTpe(_)), List (cc2 @ matchCC(_))) =>
167
- handledTo += to
155
+ case ap @ Term .ApplyType (Term .Select (e, to @ toTpe(_)), List (cc2 @ matchCC(_))) =>
168
156
169
157
// e.to[CC](*cbf*) extract implicit parameter
170
158
val synth = ctx.index.synthetics.find(_.position.end == ap.pos.end).get
@@ -173,23 +161,25 @@ case class CanBuildFromNothing(param: Name,
173
161
// This is a bit unsafe
174
162
// https://github.com/scalameta/scalameta/issues/1636
175
163
if (implicitCbf.syntax == param.syntax) {
176
- trailingBrackets(to, ctx).map { case (open, close) =>
177
- ctx.replaceTree(cc2, implicitCbf.syntax) +
178
- ctx.replaceToken(open, " (" ) +
179
- ctx.replaceToken(close, " )" )
180
- }.asPatch
164
+
165
+ // .to[CC]
166
+ val apToRemove = ap.tokens.slice(e.tokens.end - ap.tokens.start, ap.tokens.size)
167
+
168
+ ctx.removeTokens(apToRemove) +
169
+ ctx.addLeft(e, implicitCbf.syntax + " .fromSpecific(" ) +
170
+ ctx.addRight(e, " )" )
181
171
} else Patch .empty
182
172
183
173
}.asPatch
184
174
185
175
// implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, CC[Int]] =>
186
- // implicit cbf: collection. Factory[Int, CC[Int]]
176
+ // implicit cbf: Factory[Int, CC[Int]]
187
177
val parameterType =
188
178
ctx.replaceTree(
189
179
tpe,
190
- Type .Apply (Type .Name (" collection. Factory" ), List (t, cct)).syntax
180
+ Type .Apply (Type .Name (" Factory" ), List (t, cct)).syntax
191
181
)
192
182
193
- ( parameterType + cbfCalls + toCalls, handledTo.result())
183
+ parameterType + cbfCalls + toCalls
194
184
}
195
185
}
0 commit comments