/
HoverProvider.scala
228 lines (207 loc) Β· 7.82 KB
/
HoverProvider.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
package scala.meta.internal.pc
import java.{util as ju}
import scala.meta.internal.metals.Report
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.printer.MetalsPrinter
import scala.meta.pc.HoverSignature
import scala.meta.pc.OffsetParams
import scala.meta.pc.SymbolSearch
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Constants.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.Names.*
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.SourcePosition
object HoverProvider:
def hover(
params: OffsetParams,
driver: InteractiveDriver,
search: SymbolSearch,
)(implicit reportContext: ReportContext): ju.Optional[HoverSignature] =
val uri = params.uri
val sourceFile = CompilerInterfaces.toSource(params.uri, params.text)
driver.run(uri, sourceFile)
given ctx: Context = driver.currentCtx
val pos = driver.sourcePosition(params)
val trees = driver.openedTrees(uri)
val indexedContext = IndexedContext(ctx)
def typeFromPath(path: List[Tree]) =
if path.isEmpty then NoType else path.head.tpe
val path = Interactive.pathTo(trees, pos)
val tp = typeFromPath(path)
val tpw = tp.widenTermRefExpr
// For expression we need to find all enclosing applies to get the exact generic type
val enclosing = path.expandRangeToEnclosingApply(pos)
if tp.isError || tpw == NoType || tpw.isError || path.isEmpty
then
def report =
val posId =
if path.isEmpty || path.head.sourcePos == null || !path.head.sourcePos.exists
then pos.start
else path.head.sourcePos.start
Report(
"empty-hover-scala3",
s"""|$uri
|pos: ${pos.toLsp}
|
|tp: $tp
|has error: ${tp.isError}
|
|tpw: $tpw
|has error: ${tpw.isError}
|
|path:
|- ${path.map(_.toString()).mkString("\n- ")}
|trees:
|- ${trees.map(_.toString()).mkString("\n- ")}
|""".stripMargin,
s"$uri::$posId",
)
end report
reportContext.unsanitized.create(report, ifVerbose = true)
ju.Optional.empty()
else
val skipCheckOnName =
!pos.isPoint // don't check isHoveringOnName for RangeHover
val printerContext =
driver.compilationUnits.get(uri) match
case Some(unit) =>
val newctx =
ctx.fresh.setCompilationUnit(unit)
MetalsInteractive.contextOfPath(enclosing)(using newctx)
case None => ctx
val printer = MetalsPrinter.standard(
IndexedContext(printerContext),
search,
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.Include,
)
MetalsInteractive.enclosingSymbolsWithExpressionType(
enclosing,
pos,
indexedContext,
skipCheckOnName,
) match
case Nil =>
fallbackToDynamics(path, printer)
case (symbol, tpe) :: _
if symbol.name == nme.selectDynamic || symbol.name == nme.applyDynamic =>
fallbackToDynamics(path, printer)
case symbolTpes @ ((symbol, tpe) :: _) =>
val exprTpw = tpe.widenTermRefExpr.metalsDealias
val hoverString =
tpw match
// https://github.com/lampepfl/dotty/issues/8891
case tpw: ImportType =>
printer.hoverSymbol(symbol, symbol.paramRef)
case _ =>
val (tpe, sym) =
if symbol.isType then (symbol.typeRef, symbol)
else enclosing.head.seenFrom(symbol)
val finalTpe =
if tpe != NoType then tpe
else tpw
printer.hoverSymbol(sym, finalTpe)
end match
end hoverString
val docString = symbolTpes
.flatMap(symTpe => search.symbolDocumentation(symTpe._1))
.map(_.docstring)
.mkString("\n")
printer.expressionType(exprTpw) match
case Some(expressionType) =>
val forceExpressionType =
!pos.span.isZeroExtent || (
!hoverString.endsWith(expressionType) &&
!symbol.isType &&
!symbol.is(Module) &&
!symbol.flags.isAllOf(EnumCase)
)
ju.Optional.of(
new ScalaHover(
expressionType = Some(expressionType),
symbolSignature = Some(hoverString),
docstring = Some(docString),
forceExpressionType = forceExpressionType,
)
)
case _ =>
ju.Optional.empty
end match
end match
end if
end hover
extension (pos: SourcePosition)
private def isPoint: Boolean = pos.start == pos.end
private def fallbackToDynamics(
path: List[Tree],
printer: MetalsPrinter,
)(using Context): ju.Optional[HoverSignature] = path match
case SelectDynamicExtractor(sel, n, name) =>
def findRefinement(tp: Type): Option[HoverSignature] =
tp match
case RefinedType(_, refName, tpe) if name == refName.toString() =>
val tpeString =
if n == nme.selectDynamic then s": ${printer.tpe(tpe.resultType)}"
else printer.tpe(tpe)
val valOrDef =
if n == nme.selectDynamic && !tpe.isInstanceOf[ExprType]
then "val"
else "def"
Some(
new ScalaHover(
expressionType = Some(tpeString),
symbolSignature = Some(s"$valOrDef $name$tpeString"),
)
)
case RefinedType(parent, _, _) =>
findRefinement(parent)
case _ => None
val refTpe = sel.tpe.metalsDealias match
case r: RefinedType => Some(r)
case t: (TermRef | TypeProxy) => Some(t.termSymbol.info.metalsDealias)
case _ => None
refTpe.flatMap(findRefinement).asJava
case _ =>
ju.Optional.empty()
end HoverProvider
object SelectDynamicExtractor:
def unapply(path: List[Tree])(using Context) =
path match
// the same tests as below, since 3.3.1-RC1 path starts with Select
case Select(_, _) :: Apply(
Select(Apply(reflSel, List(sel)), n),
List(Literal(Constant(name: String))),
) :: _
if (n == nme.selectDynamic || n == nme.applyDynamic) &&
nme.reflectiveSelectable == reflSel.symbol.name =>
Some(sel, n, name)
// tests `structural-types` and `structural-types1` in HoverScala3TypeSuite
case Apply(
Select(Apply(reflSel, List(sel)), n),
List(Literal(Constant(name: String))),
) :: _
if (n == nme.selectDynamic || n == nme.applyDynamic) &&
nme.reflectiveSelectable == reflSel.symbol.name =>
Some(sel, n, name)
// the same tests as below, since 3.3.1-RC1 path starts with Select
case Select(_, _) :: Apply(
Select(sel, n),
List(Literal(Constant(name: String))),
) :: _ if n == nme.selectDynamic || n == nme.applyDynamic =>
Some(sel, n, name)
// tests `selectable`, `selectable2` and `selectable-full` in HoverScala3TypeSuite
case Apply(
Select(sel, n),
List(Literal(Constant(name: String))),
) :: _ if n == nme.selectDynamic || n == nme.applyDynamic =>
Some(sel, n, name)
case _ => None
end match
end unapply
end SelectDynamicExtractor