Skip to content

Commit

Permalink
Merge pull request #3683 from olafurpg/definition-broken
Browse files Browse the repository at this point in the history
Go to definition of synthetic symbols.
dos65 authored Mar 7, 2022
2 parents e8a5a8d + a45d894 commit 8ad56ce
Showing 3 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -26,8 +26,7 @@ class PcDefinitionProvider(val compiler: MetalsGlobal, params: OffsetParams) {
if (
tree.symbol == null ||
tree.symbol == NoSymbol ||
tree.symbol.isErroneous ||
tree.symbol.isSynthetic
tree.symbol.isErroneous
) {
DefinitionResultImpl.empty
} else if (tree.symbol.hasPackageFlag) {
@@ -43,7 +42,7 @@ class PcDefinitionProvider(val compiler: MetalsGlobal, params: OffsetParams) {
DefinitionResultImpl(
semanticdbSymbol(tree.symbol),
ju.Collections.singletonList(
new Location(params.uri().toString(), tree.symbol.pos.toLSP)
new Location(params.uri().toString(), tree.symbol.pos.focus.toLSP)
)
)
} else {
Original file line number Diff line number Diff line change
@@ -130,13 +130,15 @@ class PcDefinitionProvider(
case (imp: Import) :: _ =>
importedSymbols(imp, _.span.contains(pos.span))

// For constructor calls, return the `<init>` that was selected
case _ :: (_: New) :: (select: Select) :: _ =>
List(select.symbol)

// wildcard param
case head :: _ if (head.symbol.is(Param) && head.symbol.is(Synthetic)) =>
Nil
List(head.symbol)

case (head @ Select(target, name)) :: _
if head.symbol.is(Synthetic) && name == StdNames.nme.apply =>
val sym = target.symbol
if sym.is(Synthetic) && sym.is(Module) then List(sym.companionClass)
else List(target.symbol)

case head :: tl =>
if head.symbol.is(Synthetic) then enclosingSymbols(tl, pos, indexed)
88 changes: 83 additions & 5 deletions tests/cross/src/test/scala/tests/pc/PcDefinitionSuite.scala
Original file line number Diff line number Diff line change
@@ -30,13 +30,25 @@ class PcDefinitionSuite extends BasePcDefinitionSuite {
"""|
|object Main {
| for {
| <<x>> <- List(1)
| <<>>x <- List(1)
| y <- 1.to(x)
| z = y + x
| if y < @@x
| } yield y
|}
|""".stripMargin
|""".stripMargin,
compat = Map(
"3" ->
"""|object Main {
| for {
| <<x>> <- List(1)
| y <- 1.to(x)
| z = y + x
| if y < x
| } yield y
|}
|""".stripMargin
)
)

check(
@@ -225,7 +237,7 @@ class PcDefinitionSuite extends BasePcDefinitionSuite {
"named-arg-local",
"""|
|object Main {
| <<def foo(arg: Int): Unit = ()>>
| def <<>>foo(arg: Int): Unit = ()
|
| foo(a@@rg = 42)
|}
@@ -302,9 +314,33 @@ class PcDefinitionSuite extends BasePcDefinitionSuite {
"eta",
"""|
|object Main {
| List(1).map(@@_ + 2)
| List(1).map(<<>>@@_ + 2)
|}
|""".stripMargin
|""".stripMargin,
compat = Map(
"3" ->
"""|
|object Main {
| List(1).map(@@_ + 2)
|}
|""".stripMargin
)
)

check(
"eta-2",
"""|
|object Main {
| List(1).foldLeft(0)(_ + <<>>@@_)
|}
|""".stripMargin,
compat = Map(
"3" ->
"""|object Main {
| List(1).foldLeft(0)(_ + _)
|}
|""".stripMargin
)
)

check(
@@ -351,4 +387,46 @@ class PcDefinitionSuite extends BasePcDefinitionSuite {
|}
|""".stripMargin
)

check(
"synthetic-definition-case-class",
"""|
|class Main {
| case class <<>>User(name: String, age: Int)
| def hello(u: User): Unit = ()
| hello(Us@@er())
|}
|""".stripMargin,
compat = Map(
"3" ->
"""|
|class Main {
| case class <<User>>(name: String, age: Int)
| def hello(u: User): Unit = ()
| hello(User())
|}
|""".stripMargin
)
)

check(
"synthetic-definition-class-constructor",
"""|
|class Main {
| class <<>>User(name: String, age: Int)
| def hello(u: User): Unit = ()
| hello(new Us@@er())
|}
|""".stripMargin,
compat = Map(
"3" ->
"""|
|class Main {
| class <<User>>(name: String, age: Int)
| def hello(u: User): Unit = ()
| hello(new Us@@er())
|}
|""".stripMargin
)
)
}

0 comments on commit 8ad56ce

Please sign in to comment.