forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractiveEnrichments.scala
417 lines (368 loc) · 14.2 KB
/
InteractiveEnrichments.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package dotty.tools.pc.utils
import java.util.Optional
import scala.annotation.tailrec
import scala.meta.internal.jdk.CollectionConverters.*
import scala.meta.internal.mtags.CommonMtagsEnrichments
import scala.meta.internal.mtags.KeywordWrapper
import scala.meta.pc.ContentType
import scala.meta.pc.OffsetParams
import scala.meta.pc.RangeParams
import scala.meta.pc.SymbolDocumentation
import scala.meta.pc.SymbolSearch
import scala.util.control.NonFatal
import scala.jdk.OptionConverters.*
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Denotations.*
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.NameOps.*
import dotty.tools.dotc.core.Names.*
import dotty.tools.dotc.core.StdNames.*
import dotty.tools.dotc.core.SymDenotations.NoDenotation
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.SourcePosition
import dotty.tools.dotc.util.Spans
import dotty.tools.dotc.util.Spans.Span
import dotty.tools.pc.SemanticdbSymbols
import org.eclipse.lsp4j as l
object InteractiveEnrichments extends CommonMtagsEnrichments:
extension (driver: InteractiveDriver)
def sourcePosition(
params: OffsetParams,
isZeroExtent: Boolean = true
): SourcePosition =
val uri = params.uri()
val source = driver.openedFiles(uri.nn)
val span = params match
case p: RangeParams if p.offset() != p.endOffset() =>
p.trimWhitespaceInRange.fold {
Spans.Span(p.offset(), p.endOffset())
} {
case trimmed: RangeParams =>
Spans.Span(trimmed.offset(), trimmed.endOffset())
case offset =>
Spans.Span(p.offset(), p.offset())
}
case _ if !isZeroExtent => Spans.Span(params.offset(), params.offset() + 1)
case _ => Spans.Span(params.offset())
new SourcePosition(source, span)
end sourcePosition
def localContext(params: OffsetParams): Context =
if driver.currentCtx.run.nn.units.isEmpty then
throw new RuntimeException(
"No source files were passed to the Scala 3 presentation compiler"
)
val unit = driver.currentCtx.run.nn.units.head
val pos = driver.sourcePosition(params)
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
val tpdPath =
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using
newctx
)
Interactive.contextOfPath(tpdPath)(using newctx)
end localContext
end extension
extension (pos: SourcePosition)
def offsetToPos(offset: Int): l.Position =
// dotty's `SourceFile.column` method treats tabs incorrectly.
// If a line starts with tabs, they just don't count as symbols, resulting in a wrong editRange.
// see: https://github.com/scalameta/metals/pull/3702
val lineStartOffest = pos.source.startOfLine(offset)
val line = pos.source.offsetToLine(lineStartOffest)
val column = offset - lineStartOffest
new l.Position(line, column)
def toLsp: l.Range =
new l.Range(
offsetToPos(pos.start),
offsetToPos(pos.end)
)
def withEnd(end: Int): SourcePosition =
pos.withSpan(pos.span.withEnd(end))
def withStart(end: Int): SourcePosition =
pos.withSpan(pos.span.withStart(end))
def focusAt(point: Int): SourcePosition =
pos.withSpan(pos.span.withPoint(point).focus)
def encloses(other: SourcePosition): Boolean =
pos.start <= other.start && pos.end >= other.end
def encloses(other: RangeParams): Boolean =
pos.start <= other.offset() && pos.end >= other.endOffset()
/**
* @return (adjusted position, should strip backticks)
*/
def adjust(
text: Array[Char],
forRename: Boolean = false,
)(using Context): (SourcePosition, Boolean) =
if !pos.span.isCorrect(text) then (pos, false)
else
val pos0 =
val span = pos.span
if span.exists && span.point > span.end then
pos.withSpan(
span
.withStart(span.point)
.withEnd(span.point + (span.end - span.start))
)
else pos
val pos1 =
if pos0.end > 0 && text(pos0.end - 1) == ',' then
pos0.withEnd(pos0.end - 1)
else pos0
val isBackticked =
text(pos1.start) == '`' &&
pos1.end > 0 &&
text(pos1.end - 1) == '`'
// when the old name contains backticks, the position is incorrect
val isOldNameBackticked = text(pos1.start) != '`' &&
pos1.start > 0 &&
text(pos1.start - 1) == '`' &&
text(pos1.end) == '`'
if isBackticked && forRename then
(pos1.withStart(pos1.start + 1).withEnd(pos1.end - 1), true)
else if isOldNameBackticked then
(pos1.withStart(pos1.start - 1).withEnd(pos1.end + 1), false)
else (pos1, false)
end adjust
end extension
extension (span: Span)
def isCorrect(text: Array[Char]): Boolean =
!span.isZeroExtent && span.exists && span.start < text.size && span.end <= text.size
extension (pos: RangeParams)
def encloses(other: SourcePosition): Boolean =
pos.offset() <= other.start && pos.endOffset() >= other.end
extension (sym: Symbol)(using Context)
def fullNameBackticked: String = fullNameBackticked(Set.empty[String])
def fullNameBackticked(backtickSoftKeyword: Boolean = true): String =
if backtickSoftKeyword then fullNameBackticked(Set.empty[String])
else fullNameBackticked(KeywordWrapper.Scala3SoftKeywords)
def fullNameBackticked(exclusions: Set[String]): String =
@tailrec
def loop(acc: List[String], sym: Symbol): List[String] =
if sym == NoSymbol || sym.isRoot || sym.isEmptyPackage then acc
else if sym.isPackageObject then loop(acc, sym.owner)
else
val v = this.nameBackticked(sym)(exclusions)
loop(v :: acc, sym.owner)
loop(Nil, sym).mkString(".")
def decodedName: String = sym.name.decoded
def companion: Symbol =
if sym.is(Module) then sym.companionClass else sym.companionModule
def nameBackticked: String = nameBackticked(Set.empty[String])
def nameBackticked(backtickSoftKeyword: Boolean = true): String =
if backtickSoftKeyword then nameBackticked(Set.empty[String])
else nameBackticked(KeywordWrapper.Scala3SoftKeywords)
def nameBackticked(exclusions: Set[String]): String =
KeywordWrapper.Scala3.backtickWrap(sym.decodedName, exclusions)
def withUpdatedTpe(tpe: Type): Symbol =
val upd = sym.copy(info = tpe)
val paramsWithFlags =
sym.paramSymss
.zip(upd.paramSymss)
.map((l1, l2) =>
l1.zip(l2)
.map((s1, s2) =>
s2.flags = s1.flags
s2
)
)
upd.rawParamss = paramsWithFlags
upd
end withUpdatedTpe
// Returns true if this symbol is locally defined from an old version of the source file.
def isStale: Boolean =
sym.sourcePos.span.exists && {
val source = ctx.source
if (source ne sym.source) && source.path == sym.source.path then
!source.content.startsWith(
sym.decodedName.toString(),
sym.sourcePos.span.point
)
else false
}
end extension
extension (name: Name)(using Context)
def decoded: String = name.stripModuleClassSuffix.show
extension (s: String)
def backticked: String = s.backticked()
def backticked(backtickSoftKeyword: Boolean = true): String =
if backtickSoftKeyword then KeywordWrapper.Scala3.backtickWrap(s)
else
KeywordWrapper.Scala3.backtickWrap(s, KeywordWrapper.Scala3SoftKeywords)
def stripBackticks: String = s.stripPrefix("`").stripSuffix("`")
extension (text: Array[Char])
def indexAfterSpacesAndComments: Int = {
var isInComment = false
var startedStateChange = false
val index = text.indexWhere {
case '/' if !isInComment && !startedStateChange =>
startedStateChange = true
false
case '*' if !isInComment && startedStateChange =>
startedStateChange = false
isInComment = true
false
case '/' if isInComment && startedStateChange =>
startedStateChange = false
isInComment = false
false
case '*' if isInComment && !startedStateChange =>
startedStateChange = true
false
case c if isInComment || c.isSpaceChar || c == '\t' =>
startedStateChange = false
false
case _ => true
}
if (startedStateChange) index - 1
else index
}
extension (search: SymbolSearch)
def symbolDocumentation(symbol: Symbol, contentType: ContentType = ContentType.MARKDOWN)(using
Context
): Option[SymbolDocumentation] =
def toSemanticdbSymbol(symbol: Symbol) =
SemanticdbSymbols.symbolName(
if !symbol.is(JavaDefined) && symbol.isPrimaryConstructor then
symbol.owner
else symbol
)
val sym = toSemanticdbSymbol(symbol)
def parentSymbols =
if symbol.name == nme.apply && symbol.maybeOwner.is(ModuleClass) then
List(
symbol.maybeOwner,
symbol.maybeOwner.companion,
).filter(_ != NoSymbol) ++ symbol.allOverriddenSymbols
else symbol.allOverriddenSymbols
val documentation =
if symbol.isLocal then Optional.empty
else
search.documentation(
sym,
() => parentSymbols.iterator.map(toSemanticdbSymbol).toList.asJava,
contentType,
).nn
documentation.nn.toScala
end symbolDocumentation
end extension
private val infixNames =
Set(nme.apply, nme.unapply, nme.unapplySeq)
extension (tree: Tree)
def qual: Tree =
tree match
case Apply(q, _) => q.qual
case TypeApply(q, _) => q.qual
case AppliedTypeTree(q, _) => q.qual
case Select(q, _) => q
case _ => tree
def seenFrom(sym: Symbol)(using Context): (Type, Symbol) =
try
val pre = tree.qual
val denot = sym.denot.asSeenFrom(pre.typeOpt.widenTermRefExpr)
(denot.info, sym.withUpdatedTpe(denot.info))
catch case NonFatal(e) => (sym.info, sym)
def isInfix(using ctx: Context) =
tree match
case Select(New(_), _) => false
case Select(_, name: TermName) if infixNames(name) => false
case Select(This(_), _) => false
// is a select statement without a dot `qual.name`
case sel @ Select(qual, _) if !sel.symbol.is(Synthetic) =>
val source = tree.source
!(qual.span.end until sel.nameSpan.start)
.map(source.apply)
.contains('.')
case _ => false
def children(using Context): List[Tree] =
val collector = new TreeAccumulator[List[Tree]]:
def apply(x: List[Tree], tree: Tree)(using Context): List[Tree] =
tree :: x
collector
.foldOver(Nil, tree)
.reverse
/**
* Returns the children of the tree that overlap with the given span.
*/
def enclosedChildren(span: Span)(using Context): List[Tree] =
tree.children
.filter(tree =>
tree.sourcePos.exists && tree.span.start <= span.end && tree.span.end >= span.start
)
end enclosedChildren
end extension
extension (imp: Import)
def selector(span: Span)(using Context): Option[Symbol] =
for sel <- imp.selectors.find(_.span.contains(span))
yield imp.expr.symbol.info.member(sel.name).symbol
private val forCompMethods =
Set(nme.map, nme.flatMap, nme.withFilter, nme.foreach)
extension (sel: Select)
def isForComprehensionMethod(using Context): Boolean =
val syntheticName = sel.name match
case name: TermName => forCompMethods(name)
case _ => false
val wrongSpan = sel.qualifier.span.contains(sel.nameSpan)
syntheticName && wrongSpan
extension (denot: Denotation)
def allSymbols: List[Symbol] =
denot match
case MultiDenotation(denot1, denot2) =>
List(
denot1.allSymbols,
denot2.allSymbols
).flatten
case NoDenotation => Nil
case _ =>
List(denot.symbol)
extension (path: List[Tree])
def expandRangeToEnclosingApply(
pos: SourcePosition
)(using Context): List[Tree] =
def tryTail(enclosing: List[Tree]): Option[List[Tree]] =
enclosing match
case Nil => None
case head :: tail =>
head match
case t: GenericApply
if t.fun.srcPos.span.contains(
pos.span
) && !t.typeOpt.isErroneous =>
tryTail(tail).orElse(Some(enclosing))
case in: Inlined =>
tryTail(tail).orElse(Some(enclosing))
case New(_) =>
tail match
case Nil => None
case Select(_, _) :: next =>
tryTail(next)
case _ =>
None
case sel @ Select(qual, nme.apply) if qual.span == sel.nameSpan =>
tryTail(tail).orElse(Some(enclosing))
case _ =>
None
path match
case head :: tail =>
tryTail(tail).getOrElse(path)
case _ =>
List(EmptyTree)
end expandRangeToEnclosingApply
end extension
extension (tpe: Type)
def deepDealias(using Context): Type =
tpe.dealias match
case app @ AppliedType(tycon, params) =>
AppliedType(tycon, params.map(_.deepDealias))
case aliasingBounds: AliasingBounds =>
aliasingBounds.derivedAlias(aliasingBounds.alias.dealias)
case TypeBounds(lo, hi) =>
TypeBounds(lo.dealias, hi.dealias)
case RefinedType(parent, name, refinedInfo) =>
RefinedType(parent.dealias, name, refinedInfo.deepDealias)
case dealised => dealised
extension[T] (list: List[T])
def get(n: Int): Option[T] = if 0 <= n && n < list.size then Some(list(n)) else None
end InteractiveEnrichments