@@ -112,8 +112,8 @@ final class InferredTypeProvider(
112
112
def imports : List [TextEdit ] =
113
113
printer.imports(autoImportsGen)
114
114
115
- def printType (tpe : Type ): String =
116
- printer.tpe(tpe)
115
+ def printTypeAscription (tpe : Type , spaceBefore : Boolean = false ): String =
116
+ ( if spaceBefore then " : " else " : " ) + printer.tpe(tpe)
117
117
118
118
path.headOption match
119
119
/* `val a = 1` or `var b = 2`
@@ -124,7 +124,7 @@ final class InferredTypeProvider(
124
124
* turns into
125
125
* `.map((a: Int) => a + a)`
126
126
*/
127
- case Some (vl @ ValDef (sym , tpt, rhs)) =>
127
+ case Some (vl @ ValDef (name , tpt, rhs)) =>
128
128
val isParam = path match
129
129
case head :: next :: _ if next.symbol.isAnonymousFunction => true
130
130
case head :: (b @ Block (stats, expr)) :: next :: _
@@ -136,9 +136,11 @@ final class InferredTypeProvider(
136
136
val endPos =
137
137
findNamePos(sourceText, vl, keywordOffset).endPos.toLsp
138
138
adjustOpt.foreach(adjust => endPos.setEnd(adjust.adjustedEndPos))
139
+ val spaceBefore = name.isOperatorName
140
+
139
141
new TextEdit (
140
142
endPos,
141
- " : " + printType (optDealias(tpt.typeOpt)) + {
143
+ printTypeAscription (optDealias(tpt.typeOpt), spaceBefore ) + {
142
144
if withParens then " )" else " "
143
145
}
144
146
)
@@ -197,7 +199,7 @@ final class InferredTypeProvider(
197
199
* turns into
198
200
* `def a[T](param : Int): Int = param`
199
201
*/
200
- case Some (df @ DefDef (name, _ , tpt, rhs)) =>
202
+ case Some (df @ DefDef (name, paramss , tpt, rhs)) =>
201
203
def typeNameEdit =
202
204
/* NOTE: In Scala 3.1.3, `List((1,2)).map((<<a>>,b) => ...)`
203
205
* turns into `List((1,2)).map((:Inta,b) => ...)`,
@@ -208,10 +210,12 @@ final class InferredTypeProvider(
208
210
if tpt.endPos.end > df.namePos.end then tpt.endPos.toLsp
209
211
else df.namePos.endPos.toLsp
210
212
213
+ val spaceBefore = name.isOperatorName && paramss.isEmpty
214
+
211
215
adjustOpt.foreach(adjust => end.setEnd(adjust.adjustedEndPos))
212
216
new TextEdit (
213
217
end,
214
- " : " + printType (optDealias(tpt.typeOpt))
218
+ printTypeAscription (optDealias(tpt.typeOpt), spaceBefore )
215
219
)
216
220
end typeNameEdit
217
221
@@ -239,9 +243,10 @@ final class InferredTypeProvider(
239
243
*/
240
244
case Some (bind @ Bind (name, body)) =>
241
245
def baseEdit (withParens : Boolean ) =
246
+ val spaceBefore = name.isOperatorName
242
247
new TextEdit (
243
248
bind.endPos.toLsp,
244
- " : " + printType (optDealias(body.typeOpt)) + {
249
+ printTypeAscription (optDealias(body.typeOpt), spaceBefore ) + {
245
250
if withParens then " )" else " "
246
251
}
247
252
)
@@ -272,9 +277,10 @@ final class InferredTypeProvider(
272
277
* `for(t: Int <- 0 to 10)`
273
278
*/
274
279
case Some (i @ Ident (name)) =>
280
+ val spaceBefore = name.isOperatorName
275
281
val typeNameEdit = new TextEdit (
276
282
i.endPos.toLsp,
277
- " : " + printType (optDealias(i.typeOpt.widen))
283
+ printTypeAscription (optDealias(i.typeOpt.widen), spaceBefore )
278
284
)
279
285
typeNameEdit :: imports
280
286
0 commit comments