Skip to content

Commit

Permalink
fix: Go to definition for named constructor params
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk authored and tgodzik committed Aug 17, 2023
1 parent b7f370e commit 61c5c41
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object DefinitionAlternatives {
caseClassCompanionToType(symbol),
caseClassApplyOrCopy(symbol),
caseClassApplyOrCopyParams(symbol),
initParamToValue(symbol),
varGetter(symbol),
methodOwner(symbol),
objectInsteadOfAny(symbol)
Expand Down Expand Up @@ -47,6 +48,22 @@ object DefinitionAlternatives {
}
}

private def initParamToValue(symbol: Symbol): Option[Symbol] = {
Option(symbol).collect {
case GlobalSymbol(
GlobalSymbol(
GlobalSymbol(owner, signature),
Descriptor.Method("<init>", _)
),
Descriptor.Parameter(param)
) =>
GlobalSymbol(
GlobalSymbol(owner, Descriptor.Type(signature.name.value)),
Descriptor.Term(param)
)
}
}

/**
* If `case class Foo(a: Int)`, then resolve
* `a` in `Foo.apply(a = 1)`, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object NamedArguments/*NamedArguments.scala*/ {

// anonymous classes
@deprecated/*deprecated.scala*/(
message/*deprecated.scala fallback to scala.deprecated#*/ = "a",
since/*deprecated.scala fallback to scala.deprecated#*/ = susan/*NamedArguments.scala*/,/*unexpected: example.NamedArguments.susan.*/
message/*deprecated.scala fallback to scala.deprecated#message.*/ = "a",
since/*deprecated.scala fallback to scala.deprecated#since.*/ = susan/*NamedArguments.scala*/,/*unexpected: example.NamedArguments.susan.*/
) def b/*NamedArguments.scala*/ = 1

// vararg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object NamedArguments/*NamedArguments.scala*/ {

// anonymous classes
@deprecated/*deprecated.scala*/(
message/*deprecated.scala fallback to scala.deprecated#*/ = "a",
since/*deprecated.scala fallback to scala.deprecated#*/ = susan/*NamedArguments.scala*/,
message/*deprecated.scala fallback to scala.deprecated#message.*/ = "a",
since/*deprecated.scala fallback to scala.deprecated#since.*/ = susan/*NamedArguments.scala*/,
) def b/*NamedArguments.scala*/ = 1

// vararg
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/src/test/scala/tests/DefinitionLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,39 @@ class DefinitionLspSuite extends BaseLspSuite("definition") {
}
}

test("init-args") {
for {
_ <- initialize(
"""
|/metals.json
|{
| "a": { }
|}
|/a/src/main/scala/a/Main.scala
|class A(
| a: Int,
| b: Int
|) {}
|object Main {
| val aa = new A(a = 1, b = 2)
|}
|""".stripMargin
)
_ = client.messageRequests.clear()
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ = assertNoDiff(
server.workspaceDefinitions,
"""|/a/src/main/scala/a/Main.scala
|class A/*L0*/(
| a/*L1*/: Int/*Int.scala*/,
| b/*L2*/: Int/*Int.scala*/
|) {}
|object Main/*L4*/ {
| val aa/*L5*/ = new A/*L0*/(a/*L1*/ = 1, b/*L2*/ = 2)
|}
|""".stripMargin,
)
} yield ()
}

}

0 comments on commit 61c5c41

Please sign in to comment.