Skip to content

Commit 6528b74

Browse files
kasiaMarekWojciechMazur
authored andcommitted
fix: go to definition and hover for named args in pattern match
Co-Authored-By: Prince <[email protected]> [Cherry-picked 47b6a29]
1 parent 429c473 commit 6528b74

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ object MetalsInteractive:
120120
// For a named arg, find the target `DefDef` and jump to the param
121121
case NamedArg(name, _) :: Apply(fn, _) :: _ =>
122122
val funSym = fn.symbol
123-
if funSym.is(Synthetic) && funSym.owner.is(CaseClass) then
124-
val sym = funSym.owner.info.member(name).symbol
123+
lazy val owner = funSym.owner.companionClass
124+
if funSym.is(Synthetic) && owner.is(CaseClass) then
125+
val sym = owner.info.member(name).symbol
125126
List((sym, sym.info, None))
126127
else
127128
val paramSymbol =
@@ -130,6 +131,13 @@ object MetalsInteractive:
130131
val sym = paramSymbol.getOrElse(fn.symbol)
131132
List((sym, sym.info, None))
132133

134+
case NamedArg(name, _) :: UnApply(s, _, _) :: _ =>
135+
lazy val owner = s.symbol.owner.companionClass
136+
if s.symbol.is(Synthetic) && owner.is(CaseClass) then
137+
val sym = owner.info.member(name).symbol
138+
List((sym, sym.info, None))
139+
else Nil
140+
133141
case (_: untpd.ImportSelector) :: (imp: Import) :: _ =>
134142
importedSymbols(imp, _.span.contains(pos.span)).map(sym =>
135143
(sym, sym.info, None)

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,34 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
647647
| export scala.collection.immutable.V/*scala/collection/immutable/Vector. Vector.scala*/@@ector
648648
|""".stripMargin
649649
)
650+
651+
@Test def i7763 =
652+
check(
653+
"""|case class MyItem(<<name>>: String)
654+
|
655+
|def handle(item: MyItem) =
656+
| item match {
657+
| case MyItem(na@@me = n2) => println(n2)
658+
| }
659+
|""".stripMargin
660+
)
661+
662+
@Test def `i7763-neg` =
663+
check(
664+
"""|object MyItem:
665+
| def unapply(name: String): Option[Int] = ???
666+
|
667+
|def handle(item: String) =
668+
| item match {
669+
| case MyItem(na@@me = n2) => println(n2)
670+
| }
671+
|""".stripMargin
672+
)
673+
674+
@Test def `i7763-apply` =
675+
check(
676+
"""|case class MyItem(<<name>>: String)
677+
|
678+
|def handle(item: String) = MyItem(na@@me = item)
679+
|""".stripMargin
680+
)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,3 +926,15 @@ class HoverTermSuite extends BaseHoverSuite:
926926
|""".stripMargin,
927927
"val aa: Int".hover
928928
)
929+
930+
@Test def i7763 =
931+
check(
932+
"""|case class MyItem(name: String)
933+
|
934+
|def handle(item: MyItem) =
935+
| item match {
936+
| case MyItem(na@@me = n2) => println(n2)
937+
| }
938+
|""".stripMargin,
939+
"val name: String".hover
940+
)

0 commit comments

Comments
 (0)