Skip to content

Commit 4ef884b

Browse files
authored
fix: go to definition and hover for named args in pattern match (#23956)
fixes: scalameta/metals#7763
2 parents a00e629 + 47b6a29 commit 4ef884b

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
@@ -938,3 +938,15 @@ class HoverTermSuite extends BaseHoverSuite:
938938
|""".stripMargin,
939939
"val aa: Int".hover
940940
)
941+
942+
@Test def i7763 =
943+
check(
944+
"""|case class MyItem(name: String)
945+
|
946+
|def handle(item: MyItem) =
947+
| item match {
948+
| case MyItem(na@@me = n2) => println(n2)
949+
| }
950+
|""".stripMargin,
951+
"val name: String".hover
952+
)

0 commit comments

Comments
 (0)