From a92d28ae4693a7cd68f2518edc2bf727b9c44bc5 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Thu, 18 Sep 2025 18:38:36 +0200 Subject: [PATCH] fix: go to definition and hover for named args in pattern match Co-Authored-By: Prince <98524116+ajafri2001@users.noreply.github.com> [Cherry-picked 47b6a29e2f8d87a96f51d13f8cb71b6116eb2c20] --- .../dotty/tools/pc/MetalsInteractive.scala | 12 +++++-- .../tests/definition/PcDefinitionSuite.scala | 31 +++++++++++++++++++ .../tools/pc/tests/hover/HoverTermSuite.scala | 12 +++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala b/presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala index 60a884eb2934..92da8c875dac 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala @@ -120,8 +120,9 @@ object MetalsInteractive: // For a named arg, find the target `DefDef` and jump to the param case NamedArg(name, _) :: Apply(fn, _) :: _ => val funSym = fn.symbol - if funSym.is(Synthetic) && funSym.owner.is(CaseClass) then - val sym = funSym.owner.info.member(name).symbol + lazy val owner = funSym.owner.companionClass + if funSym.is(Synthetic) && owner.is(CaseClass) then + val sym = owner.info.member(name).symbol List((sym, sym.info, None)) else val paramSymbol = @@ -130,6 +131,13 @@ object MetalsInteractive: val sym = paramSymbol.getOrElse(fn.symbol) List((sym, sym.info, None)) + case NamedArg(name, _) :: UnApply(s, _, _) :: _ => + lazy val owner = s.symbol.owner.companionClass + if s.symbol.is(Synthetic) && owner.is(CaseClass) then + val sym = owner.info.member(name).symbol + List((sym, sym.info, None)) + else Nil + case (_: untpd.ImportSelector) :: (imp: Import) :: _ => importedSymbols(imp, _.span.contains(pos.span)).map(sym => (sym, sym.info, None) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala index b530e37f3ebd..222a126ad819 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala @@ -639,3 +639,34 @@ class PcDefinitionSuite extends BasePcDefinitionSuite: | export scala.collection.immutable.V/*scala/collection/immutable/Vector. Vector.scala*/@@ector |""".stripMargin ) + + @Test def i7763 = + check( + """|case class MyItem(<>: String) + | + |def handle(item: MyItem) = + | item match { + | case MyItem(na@@me = n2) => println(n2) + | } + |""".stripMargin + ) + + @Test def `i7763-neg` = + check( + """|object MyItem: + | def unapply(name: String): Option[Int] = ??? + | + |def handle(item: String) = + | item match { + | case MyItem(na@@me = n2) => println(n2) + | } + |""".stripMargin + ) + + @Test def `i7763-apply` = + check( + """|case class MyItem(<>: String) + | + |def handle(item: String) = MyItem(na@@me = item) + |""".stripMargin + ) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala index 801d67da272c..e6ba32ee0906 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala @@ -870,3 +870,15 @@ class HoverTermSuite extends BaseHoverSuite: |""".stripMargin, "val aa: Int".hover ) + + @Test def i7763 = + check( + """|case class MyItem(name: String) + | + |def handle(item: MyItem) = + | item match { + | case MyItem(na@@me = n2) => println(n2) + | } + |""".stripMargin, + "val name: String".hover + )