forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPcInlayHintsProvider.scala
390 lines (355 loc) · 13 KB
/
PcInlayHintsProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package dotty.tools.pc
import java.nio.file.Paths
import scala.annotation.tailrec
import scala.meta.internal.metals.ReportContext
import dotty.tools.pc.utils.InteractiveEnrichments.*
import dotty.tools.pc.printer.ShortenedTypePrinter
import scala.meta.internal.pc.InlayHints
import scala.meta.internal.pc.LabelPart
import scala.meta.internal.pc.LabelPart.*
import scala.meta.pc.InlayHintsParams
import scala.meta.pc.SymbolSearch
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.StdNames.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.util.Spans.Span
import org.eclipse.lsp4j.InlayHint
import org.eclipse.lsp4j.InlayHintKind
import org.eclipse.{lsp4j as l}
class PcInlayHintsProvider(
driver: InteractiveDriver,
params: InlayHintsParams,
symbolSearch: SymbolSearch,
)(using ReportContext):
val uri = params.uri().nn
val filePath = Paths.get(uri).nn
val sourceText = params.text().nn
val text = sourceText.toCharArray().nn
val source =
SourceFile.virtual(filePath.toString, sourceText)
driver.run(uri, source)
given InlayHintsParams = params
given InferredType.Text = InferredType.Text(text)
given ctx: Context = driver.currentCtx
val unit = driver.currentCtx.run.nn.units.head
val pos = driver.sourcePosition(params)
def provide(): List[InlayHint] =
val deepFolder = DeepFolder[InlayHints](collectDecorations)
Interactive
.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
.headOption
.getOrElse(unit.tpdTree)
.enclosedChildren(pos.span)
.flatMap(tpdTree => deepFolder(InlayHints.empty, tpdTree).result())
private def adjustPos(pos: SourcePosition): SourcePosition =
pos.adjust(text)._1
def collectDecorations(
inlayHints: InlayHints,
tree: Tree,
): InlayHints =
tree match
case ImplicitConversion(symbol, range) =>
val adjusted = adjustPos(range)
inlayHints
.add(
adjusted.startPos.toLsp,
labelPart(symbol, symbol.decodedName) :: LabelPart("(") :: Nil,
InlayHintKind.Parameter,
)
.add(
adjusted.endPos.toLsp,
LabelPart(")") :: Nil,
InlayHintKind.Parameter,
)
case ImplicitParameters(trees, pos) =>
inlayHints.add(
adjustPos(pos).toLsp,
ImplicitParameters.partsFromImplicitArgs(trees).map((label, maybeSymbol) =>
maybeSymbol match
case Some(symbol) => labelPart(symbol, label)
case None => LabelPart(label)
),
InlayHintKind.Parameter
)
case ValueOf(label, pos) =>
inlayHints.add(
adjustPos(pos).toLsp,
LabelPart("(") :: LabelPart(label) :: List(LabelPart(")")),
InlayHintKind.Parameter,
)
case TypeParameters(tpes, pos, sel)
if !syntheticTupleApply(sel) =>
val label = tpes.map(toLabelParts(_, pos)).separated("[", ", ", "]")
inlayHints.add(
adjustPos(pos).endPos.toLsp,
label,
InlayHintKind.Type,
)
case InferredType(tpe, pos, defTree)
if !isErrorTpe(tpe) =>
val adjustedPos = adjustPos(pos).endPos
if inlayHints.containsDef(adjustedPos.start) then inlayHints
else
inlayHints
.add(
adjustedPos.toLsp,
LabelPart(": ") :: toLabelParts(tpe, pos),
InlayHintKind.Type,
)
.addDefinition(adjustedPos.start)
case _ => inlayHints
private def toLabelParts(
tpe: Type,
pos: SourcePosition,
): List[LabelPart] =
val tpdPath =
Interactive.pathTo(unit.tpdTree, pos.span)
val indexedCtx = IndexedContext(Interactive.contextOfPath(tpdPath))
val printer = ShortenedTypePrinter(
symbolSearch
)(using indexedCtx)
def optDealias(tpe: Type): Type =
def isInScope(tpe: Type): Boolean =
tpe match
case tref: TypeRef =>
indexedCtx.lookupSym(
tref.currentSymbol
) == IndexedContext.Result.InScope
case AppliedType(tycon, args) =>
isInScope(tycon) && args.forall(isInScope)
case _ => true
if isInScope(tpe) then tpe
else tpe.deepDealias(using indexedCtx.ctx)
val dealiased = optDealias(tpe)
val tpeStr = printer.tpe(dealiased)
val usedRenames = printer.getUsedRenames
val parts = partsFromType(dealiased, usedRenames)
InlayHints.makeLabelParts(parts, tpeStr)
end toLabelParts
private val definitions = IndexedContext(ctx).ctx.definitions
private def syntheticTupleApply(tree: Tree): Boolean =
tree match
case sel: Select =>
if definitions.isTupleNType(sel.symbol.info.finalResultType) then
sel match
case Select(tupleClass: Ident, _)
if !tupleClass.span.isZeroExtent &&
tupleClass.span.exists &&
tupleClass.name.startsWith("Tuple") =>
val pos = tupleClass.sourcePos
!sourceText.slice(pos.start, pos.end).mkString.startsWith("Tuple")
case _ => true
else false
case _ => false
private def labelPart(symbol: Symbol, label: String) =
if symbol.source == pos.source then
LabelPart(
label,
pos = Some(symbol.sourcePos.toLsp.getStart().nn),
)
else
LabelPart(
label,
symbol = SemanticdbSymbols.symbolName(symbol),
)
private def partsFromType(
tpe: Type,
usedRenames: Map[Symbol, String],
): List[LabelPart] =
NamedPartsAccumulator(_ => true)(Nil, tpe)
.filter(_.symbol != NoSymbol)
.map { t =>
val label = usedRenames.get(t.symbol).getOrElse(t.symbol.decodedName)
labelPart(t.symbol, label)
}
private def isErrorTpe(tpe: Type): Boolean = tpe.isError
end PcInlayHintsProvider
object ImplicitConversion:
def unapply(tree: Tree)(using params: InlayHintsParams, ctx: Context) =
if (params.implicitConversions()) {
tree match
case Apply(fun: Ident, args) if isSynthetic(fun) && args.exists(!_.span.isZeroExtent) =>
implicitConversion(fun, args)
case Apply(Select(fun, name), args)
if name == nme.apply && isSynthetic(fun) && args.exists(!_.span.isZeroExtent) =>
implicitConversion(fun, args)
case _ => None
} else None
private def isSynthetic(tree: Tree)(using Context) =
tree.span.isSynthetic && tree.symbol.isOneOf(Flags.GivenOrImplicit)
private def implicitConversion(fun: Tree, args: List[Tree])(using Context) =
val lastArgPos =
args.lastOption.map(_.sourcePos).getOrElse(fun.sourcePos)
Some(
fun.symbol,
lastArgPos.withStart(fun.sourcePos.start),
)
end ImplicitConversion
object ImplicitParameters:
def unapply(tree: Tree)(using params: InlayHintsParams, ctx: Context) =
if (params.implicitParameters()) {
tree match
case Apply(fun, args)
if args.exists(isSyntheticArg) && !tree.sourcePos.span.isZeroExtent && !args.exists(isQuotes(_)) =>
val (implicitArgs, providedArgs) = args.partition(isSyntheticArg)
val pos = implicitArgs.head.sourcePos
Some(implicitArgs, pos)
case _ => None
} else None
@tailrec
def isSyntheticArg(tree: Tree)(using Context): Boolean = tree match
case tree: Ident =>
tree.span.isSynthetic && tree.symbol.isOneOf(Flags.GivenOrImplicit)
case Apply(fun, _ ) if tree.span.isZeroExtent => isSyntheticArg(fun)
case TypeApply(fun, _ ) if tree.span.isZeroExtent => isSyntheticArg(fun)
case _ => false
// Decorations for Quotes are rarely useful
private def isQuotes(tree: Tree)(using Context) =
tree.tpe.typeSymbol == defn.QuotesClass
def partsFromImplicitArgs(trees: List[Tree])(using Context): List[(String, Option[Symbol])] = {
@tailrec
def recurseImplicitArgs(
currentArgs: List[Tree],
remainingArgsLists: List[List[Tree]],
parts: List[(String, Option[Symbol])]
): List[(String, Option[Symbol])] =
(currentArgs, remainingArgsLists) match {
case (Nil, Nil) => parts
case (Nil, headArgsList :: tailArgsList) =>
if (headArgsList.isEmpty) {
recurseImplicitArgs(
headArgsList,
tailArgsList,
(")", None) :: parts
)
} else {
recurseImplicitArgs(
headArgsList,
tailArgsList,
(", ", None) :: (")", None) :: parts
)
}
case (arg :: remainingArgs, remainingArgsLists) =>
arg match {
case Apply(fun, args) =>
val applyLabel = (fun.symbol.decodedName, Some(fun.symbol))
recurseImplicitArgs(
args,
remainingArgs :: remainingArgsLists,
("(", None) :: applyLabel :: parts
)
case t if t.isTerm =>
val termLabel = (t.symbol.decodedName, Some(t.symbol))
if (remainingArgs.isEmpty)
recurseImplicitArgs(
remainingArgs,
remainingArgsLists,
termLabel :: parts
)
else
recurseImplicitArgs(
remainingArgs,
remainingArgsLists,
(", ", None) :: termLabel :: parts
)
case _ =>
recurseImplicitArgs(
remainingArgs,
remainingArgsLists,
parts
)
}
}
((")", None) :: recurseImplicitArgs(
trees,
Nil,
List(("(using ", None))
)).reverse
}
end ImplicitParameters
object ValueOf:
def unapply(tree: Tree)(using params: InlayHintsParams, ctx: Context) =
if (params.implicitParameters()) {
tree match
case Apply(ta @ TypeApply(fun, _), _)
if fun.span.isSynthetic && isValueOf(fun) =>
Some(
"new " + tpnme.valueOf.decoded.capitalize + "(...)",
fun.sourcePos,
)
case _ => None
} else None
private def isValueOf(tree: Tree)(using Context) =
val symbol = tree.symbol.maybeOwner
symbol.name.decoded == tpnme.valueOf.decoded.capitalize
end ValueOf
object TypeParameters:
def unapply(tree: Tree)(using params: InlayHintsParams, ctx: Context) =
if (params.typeParameters()) {
tree match
case TypeApply(sel: Select, _)
if sel.isForComprehensionMethod || sel.isInfix ||
sel.symbol.name == nme.unapply =>
None
case TypeApply(fun, args) if inferredTypeArgs(args) =>
val tpes = args.map(_.tpe.stripTypeVar.widen.finalResultType)
Some((tpes, fun.sourcePos.endPos, fun))
case _ => None
} else None
private def inferredTypeArgs(args: List[Tree]): Boolean =
args.forall {
case tt: TypeTree if tt.span.exists && !tt.span.isZeroExtent => true
case _ => false
}
end TypeParameters
object InferredType:
opaque type Text = Array[Char]
object Text:
def apply(text: Array[Char]): Text = text
def unapply(tree: Tree)(using params: InlayHintsParams, text: Text, ctx: Context) =
if (params.inferredTypes()) {
tree match
case vd @ ValDef(_, tpe, _)
if isValidSpan(tpe.span, vd.nameSpan) &&
!vd.symbol.is(Flags.Enum) &&
(isNotInUnapply(vd) || params.hintsInPatternMatch()) &&
!isValDefBind(text, vd) =>
if vd.symbol == vd.symbol.sourceSymbol then
Some(tpe.tpe, tpe.sourcePos.withSpan(vd.nameSpan), vd)
else None
case vd @ DefDef(_, _, tpe, _)
if isValidSpan(tpe.span, vd.nameSpan) &&
tpe.span.start >= vd.nameSpan.end &&
!vd.symbol.isConstructor &&
!vd.symbol.is(Flags.Mutable) =>
if vd.symbol == vd.symbol.sourceSymbol then
Some(tpe.tpe, tpe.sourcePos, vd)
else None
case bd @ Bind(
name,
Ident(nme.WILDCARD),
) if !bd.span.isZeroExtent && bd.symbol.isTerm && params.hintsInPatternMatch() =>
Some(bd.symbol.info, bd.namePos, bd)
case _ => None
} else None
private def isNotInUnapply(vd: ValDef)(using Context) =
vd.rhs.span.exists && vd.rhs.span.start > vd.nameSpan.end
private def isValidSpan(tpeSpan: Span, nameSpan: Span): Boolean =
tpeSpan.isZeroExtent &&
nameSpan.exists &&
!nameSpan.isZeroExtent
/* If is left part of val definition bind:
* val <<t>> @ ... =
*/
def isValDefBind(text: Text, vd: ValDef)(using Context) =
val afterDef = text.drop(vd.nameSpan.end)
val index = indexAfterSpacesAndComments(afterDef)
index >= 0 && index < afterDef.size && afterDef(index) == '@'
end InferredType