Skip to content

Commit 2dfcd8c

Browse files
committed
Set proper position for ValDefs generated from tuples
Previously, the span for ValDefs generated for tuples would encompas the entire expression, which led to difficulties identifying the exact path to the current position. Now, we set the span to be the same as the name underneath. Not sure if this is a proper solution, since normally ValDefs ecompans the entire span, but in this case it makes 2 different ValDef have the same span. An alternative solution would be to find the one with point nearer to the current position. This popped up within the Nightly tests we do in Metals.
1 parent d466f9f commit 2dfcd8c

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -1143,10 +1143,11 @@ object desugar {
11431143
) // no `_`
11441144

11451145
val ids = for ((named, _) <- vars) yield Ident(named.name)
1146-
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
11471146
val matchExpr =
11481147
if (tupleOptimizable) rhs
1149-
else Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
1148+
else
1149+
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
1150+
Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
11501151
vars match {
11511152
case Nil if !mods.is(Lazy) =>
11521153
matchExpr
@@ -1166,8 +1167,10 @@ object desugar {
11661167
val restDefs =
11671168
for (((named, tpt), n) <- vars.zipWithIndex if named.name != nme.WILDCARD)
11681169
yield
1169-
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
1170-
else derivedValDef(original, named, tpt, selector(n), mods)
1170+
val valDef =
1171+
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
1172+
else derivedValDef(original, named, tpt, selector(n), mods)
1173+
valDef.withSpan(named.span)
11711174
flatTree(firstDef :: restDefs)
11721175
}
11731176
}

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ object NavigateAST {
8282
case xs: List[?] => childPath(xs.iterator, path)
8383
case _ => path
8484
}
85+
// println(path1.head)
86+
// println(path1.head.span)
8587
if ((path1 ne path) &&
8688
((bestFit eq path) ||
8789
bestFit.head.span != path1.head.span &&

language-server/test/dotty/tools/languageserver/HoverTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class HoverTest {
234234

235235
@Test def tuple: Unit = {
236236
code"""|object A:
237-
| val (${m1}first${m2}, second) = (1, 2)""".withSource
238-
.hover(m1 to m2, hoverContent("Int"))
237+
| val (${m1}first${m2}, second) = (1.0, 2)""".withSource
238+
.hover(m1 to m2, hoverContent("Double"))
239239
}
240240
}

0 commit comments

Comments
 (0)