Skip to content

Commit 882f2c7

Browse files
committed
chore: backport changes from metals
1 parent bfabc31 commit 882f2c7

File tree

10 files changed

+173
-23
lines changed

10 files changed

+173
-23
lines changed

presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala

+16-13
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,20 @@ object HoverProvider:
3636
val text = params.text().nn
3737
val sourceFile = SourceFile.virtual(uri, text)
3838
driver.run(uri, sourceFile)
39+
val unit = driver.compilationUnits.get(uri)
3940

40-
given ctx: Context = driver.currentCtx
41+
given ctx: Context =
42+
val ctx = driver.currentCtx
43+
unit.map(ctx.fresh.setCompilationUnit).getOrElse(ctx)
4144
val pos = driver.sourcePosition(params)
42-
val trees = driver.openedTrees(uri)
45+
val path = unit
46+
.map(unit => Interactive.pathTo(unit.tpdTree, pos.span))
47+
.getOrElse(Interactive.pathTo(driver.openedTrees(uri), pos))
4348
val indexedContext = IndexedContext(ctx)
4449

4550
def typeFromPath(path: List[Tree]) =
4651
if path.isEmpty then NoType else path.head.tpe
4752

48-
val path = Interactive.pathTo(trees, pos)
4953
val tp = typeFromPath(path)
5054
val tpw = tp.widenTermRefExpr
5155
// For expression we need to find all enclosing applies to get the exact generic type
@@ -72,7 +76,10 @@ object HoverProvider:
7276
|path:
7377
|- ${path.map(_.toString()).mkString("\n- ")}
7478
|trees:
75-
|- ${trees.map(_.toString()).mkString("\n- ")}
79+
|- ${unit
80+
.map(u => List(u.tpdTree))
81+
.getOrElse(driver.openedTrees(uri).map(_.tree))
82+
.map(_.toString()).mkString("\n- ")}
7683
|""".stripMargin,
7784
s"$uri::$posId"
7885
)
@@ -83,15 +90,9 @@ object HoverProvider:
8390
val skipCheckOnName =
8491
!pos.isPoint // don't check isHoveringOnName for RangeHover
8592

86-
val printerContext =
87-
driver.compilationUnits.get(uri) match
88-
case Some(unit) =>
89-
val newctx =
90-
ctx.fresh.setCompilationUnit(unit)
91-
Interactive.contextOfPath(enclosing)(using newctx)
92-
case None => ctx
93+
val printerCtx = Interactive.contextOfPath(path)
9394
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Include)(
94-
using IndexedContext(printerContext)
95+
using IndexedContext(printerCtx)
9596
)
9697
MetalsInteractive.enclosingSymbolsWithExpressionType(
9798
enclosing,
@@ -142,7 +143,8 @@ object HoverProvider:
142143
expressionType = Some(expressionType),
143144
symbolSignature = Some(hoverString),
144145
docstring = Some(docString),
145-
forceExpressionType = forceExpressionType
146+
forceExpressionType = forceExpressionType,
147+
contextInfo = printer.getUsedRenamesInfo
146148
)
147149
).nn
148150
case _ =>
@@ -176,6 +178,7 @@ object HoverProvider:
176178
new ScalaHover(
177179
expressionType = Some(tpeString),
178180
symbolSignature = Some(s"$valOrDef $name$tpeString"),
181+
contextInfo = printer.getUsedRenamesInfo
179182
)
180183
)
181184
case RefinedType(parent, _, _) =>

presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala

+17-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ final class PcSyntheticDecorationsProvider(
3939
val source =
4040
SourceFile.virtual(filePath.toString, sourceText)
4141
driver.run(uri, source)
42+
43+
given InferredType.Text = InferredType.Text(text)
4244
given ctx: Context = driver.currentCtx
4345
val unit = driver.currentCtx.run.nn.units.head
4446

@@ -219,11 +221,16 @@ object TypeParameters:
219221
end TypeParameters
220222

221223
object InferredType:
222-
def unapply(tree: Tree)(using Context) =
224+
opaque type Text = Array[Char]
225+
object Text:
226+
def apply(text: Array[Char]): Text = text
227+
228+
def unapply(tree: Tree)(using text: Text, cxt: Context) =
223229
tree match
224230
case vd @ ValDef(_, tpe, _)
225231
if isValidSpan(tpe.span, vd.nameSpan) &&
226-
!vd.symbol.is(Flags.Enum) =>
232+
!vd.symbol.is(Flags.Enum) &&
233+
!isValDefBind(text, vd) =>
227234
if vd.symbol == vd.symbol.sourceSymbol then
228235
Some(tpe.tpe, tpe.sourcePos.withSpan(vd.nameSpan), vd)
229236
else None
@@ -247,6 +254,14 @@ object InferredType:
247254
nameSpan.exists &&
248255
!nameSpan.isZeroExtent
249256

257+
/* If is left part of val definition bind:
258+
* val <<t>> @ ... =
259+
*/
260+
def isValDefBind(text: Text, vd: ValDef)(using Context) =
261+
val afterDef = text.drop(vd.nameSpan.end)
262+
val index = afterDef.indexAfterSpacesAndComments
263+
index >= 0 && index < afterDef.size && afterDef(index) == '@'
264+
250265
end InferredType
251266

252267
case class Synthetics(

presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class ShortenedTypePrinter(
6262
AbsOverride,
6363
Lazy
6464
)
65+
66+
private val foundRenames = collection.mutable.Map.empty[Symbol, String]
67+
68+
def getUsedRenamesInfo(using Context): List[String] =
69+
foundRenames.map { (from, to) =>
70+
s"type $to = ${from.showName}"
71+
}.toList
6572

6673
def expressionType(tpw: Type)(using Context): Option[String] =
6774
tpw match
@@ -117,8 +124,10 @@ class ShortenedTypePrinter(
117124

118125
prefixIterator.flatMap { owner =>
119126
val prefixAfterRename = ownersAfterRename(owner)
127+
val ownerRename = indexedCtx.rename(owner)
128+
ownerRename.foreach(rename => foundRenames += owner -> rename)
120129
val currentRenamesSearchResult =
121-
indexedCtx.rename(owner).map(Found(owner, _, prefixAfterRename))
130+
ownerRename.map(Found(owner, _, prefixAfterRename))
122131
lazy val configRenamesSearchResult =
123132
renameConfigMap.get(owner).map(Missing(owner, _, prefixAfterRename))
124133
currentRenamesSearchResult orElse configRenamesSearchResult
@@ -181,7 +190,9 @@ class ShortenedTypePrinter(
181190

182191
override protected def selectionString(tp: NamedType): String =
183192
indexedCtx.rename(tp.symbol) match
184-
case Some(value) => value
193+
case Some(value) =>
194+
foundRenames += tp.symbol -> value
195+
value
185196
case None => super.selectionString(tp)
186197

187198
override def toText(tp: Type): Text =

presentation-compiler/src/main/dotty/tools/pc/utils/MtagsEnrichments.scala

+28
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,34 @@ object MtagsEnrichments extends CommonMtagsEnrichments:
230230

231231
def stripBackticks: String = s.stripPrefix("`").stripSuffix("`")
232232

233+
extension (text: Array[Char])
234+
def indexAfterSpacesAndComments: Int = {
235+
var isInComment = false
236+
var startedStateChange = false
237+
val index = text.indexWhere {
238+
case '/' if !isInComment && !startedStateChange =>
239+
startedStateChange = true
240+
false
241+
case '*' if !isInComment && startedStateChange =>
242+
startedStateChange = false
243+
isInComment = true
244+
false
245+
case '/' if isInComment && startedStateChange =>
246+
startedStateChange = false
247+
isInComment = false
248+
false
249+
case '*' if isInComment && !startedStateChange =>
250+
startedStateChange = true
251+
false
252+
case c if isInComment || c.isSpaceChar || c == '\t' =>
253+
startedStateChange = false
254+
false
255+
case _ => true
256+
}
257+
if (startedStateChange) index - 1
258+
else index
259+
}
260+
233261
extension (search: SymbolSearch)
234262
def symbolDocumentation(symbol: Symbol)(using
235263
Context

presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala

+28
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,31 @@ class SyntheticDecorationsSuite extends BaseSyntheticDecorationsSuite:
498498
|""".stripMargin
499499
)
500500

501+
@Test def `val-def-with-bind` =
502+
check(
503+
"""
504+
|object O {
505+
| val tupleBound @ (one, two) = ("1", "2")
506+
|}
507+
|""".stripMargin,
508+
"""
509+
|object O {
510+
| val tupleBound @ (one: String, two: String) = ("1", "2")
511+
|}
512+
|""".stripMargin
513+
)
514+
515+
@Test def `val-def-with-bind-and-comment` =
516+
check(
517+
"""
518+
|object O {
519+
| val tupleBound /* comment */ @ (one, two) = ("1", "2")
520+
|}
521+
|""".stripMargin,
522+
"""
523+
|object O {
524+
| val tupleBound /* comment */ @ (one: String, two: String) = ("1", "2")
525+
|}
526+
|""".stripMargin
527+
)
528+

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

+22
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,28 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
789789
|""".stripMargin
790790
)
791791

792+
@Test def `import-rename` =
793+
checkEdit(
794+
"""
795+
|package a
796+
|import scala.collection.{AbstractMap => AB}
797+
|
798+
|object Main {
799+
| def test(): AB[Int, String] = ???
800+
| val <<x>> = test()
801+
|}
802+
|""".stripMargin,
803+
"""
804+
|package a
805+
|import scala.collection.{AbstractMap => AB}
806+
|
807+
|object Main {
808+
| def test(): AB[Int, String] = ???
809+
| val x: AB[Int, String] = test()
810+
|}
811+
|""".stripMargin
812+
)
813+
792814
def checkEdit(
793815
original: String,
794816
expected: String

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverDefnSuite.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ class HoverDefnSuite extends BaseHoverSuite:
163163
"""package b.p@@kg
164164
|object Main
165165
|""".stripMargin,
166-
// TODO, doesn's show information on packages
167-
""
166+
"package b.pkg".hover,
168167
)
169168

170169
@Test def `pat-bind` =

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala

+45-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ class HoverTermSuite extends BaseHoverSuite:
419419
|object MyIntOut:
420420
| extension (i: MyIntOut) def uneven = i.value % 2 == 1
421421
|
422-
|val a = MyIntOut(1).un@@even
422+
|object Test:
423+
| val a = MyIntOut(1).un@@even
423424
|""".stripMargin,
424425
"""extension (i: MyIntOut) def uneven: Boolean
425426
|""".stripMargin.hover
@@ -593,4 +594,46 @@ class HoverTermSuite extends BaseHoverSuite:
593594
|extension [A](self: A) def inferredTypeArg[C](value: C): C
594595
|```
595596
|""".stripMargin
596-
)
597+
)
598+
599+
@Test def `import-rename` =
600+
check(
601+
"""
602+
|import scala.collection.{AbstractMap => AB}
603+
|import scala.collection.{Set => S}
604+
|
605+
|object Main {
606+
| def test(): AB[Int, String] = ???
607+
| <<val t@@t = test()>>
608+
|}
609+
|""".stripMargin,
610+
"""
611+
|```scala
612+
|type AB = AbstractMap
613+
|```
614+
|
615+
|```scala
616+
|val tt: AB[Int, String]
617+
|```""".stripMargin,
618+
)
619+
620+
@Test def `import-rename2` =
621+
check(
622+
"""
623+
|import scala.collection.{AbstractMap => AB}
624+
|import scala.collection.{Set => S}
625+
|
626+
|object Main {
627+
| <<def te@@st(d: S[Int], f: S[Char]): AB[Int, String] = ???>>
628+
|}
629+
|""".stripMargin,
630+
"""
631+
|```scala
632+
|type S = Set
633+
|type AB = AbstractMap
634+
|```
635+
|
636+
|```scala
637+
|def test(d: S[Int], f: S[Char]): AB[Int, String]
638+
|```""".stripMargin,
639+
)

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTypeSuite.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ class HoverTypeSuite extends BaseHoverSuite:
362362
|object MyIntOut:
363363
| extension (i: MyIntOut) def uneven = i.value % 2 == 1
364364
|
365-
|val a = MyIntOut(1).un@@even
365+
|object Test:
366+
| val a = MyIntOut(1).un@@even
366367
|""".stripMargin,
367368
"""|extension (i: MyIntOut) def uneven: Boolean
368369
|""".stripMargin.hover,

project/Build.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ object Build {
13411341
BuildInfoPlugin.buildInfoDefaultSettings
13421342

13431343
lazy val presentationCompilerSettings = {
1344-
val mtagsVersion = "1.2.0+54-1a664e80-SNAPSHOT"
1344+
val mtagsVersion = "1.2.0+67-30f8ab53-SNAPSHOT"
13451345

13461346
Seq(
13471347
resolvers ++= Resolver.sonatypeOssRepos("snapshots"),

0 commit comments

Comments
 (0)