Skip to content

Commit d3fcd0f

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 d3fcd0f

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 13 additions & 4 deletions
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,16 @@ 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+
if mods.is(Lazy) then
1171+
DefDef(named.name.asTermName, Nil, tpt, selector(n))
1172+
.withMods(mods &~ Lazy)
1173+
.withSpan(named.span)
1174+
else
1175+
valDef(
1176+
ValDef(named.name.asTermName, tpt, selector(n))
1177+
.withMods(mods)
1178+
.withSpan(named.span)
1179+
)
11711180
flatTree(firstDef :: restDefs)
11721181
}
11731182
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ 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"))
239+
}
240+
241+
@Test def multiAssigment: Unit = {
242+
code"""|val ${m1}x${m2}, ${m3}y${m4} = 42.0""".withSource
243+
.hover(m1 to m2, hoverContent("Double"))
244+
.hover(m3 to m4, hoverContent("Double"))
239245
}
240246
}

0 commit comments

Comments
 (0)