Skip to content

Commit

Permalink
bugfix: hover for select chains ending with function Scala 2 (#5883)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored Nov 30, 2023
1 parent e6303c3 commit d5889c8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,12 @@ trait Completions { this: MetalsGlobal =>
// for named args apply becomes transparent but fun doesn't
case Apply(fun, args) =>
!fun.pos.isTransparent && args.forall(_.pos.isOffset)
// for synthetic val definition select on it becomes transparent
case sel @ Select(qual: Ident, _) =>
val qualifierIsSyntheticVal =
Option(qual.symbol).exists(sym => sym.isValue && sym.isSynthetic)
qualifierIsSyntheticVal &&
!sel.namePosition.isTransparent && sel.namePosition.encloses(pos)
case _ => false
}
}
Expand All @@ -728,6 +734,11 @@ trait Completions { this: MetalsGlobal =>
super.traverse(t)
} else {
t match {
// workaround for Scala 2.13,
// where position is not set properly for synthetic val definition
// we are interested only in its children, which have correct positions set
case v: ValDef if v.pos.isOffset && v.symbol.isSynthetic =>
super.traverse(v)
case mdef: MemberDef =>
val annTrees = mdef.mods.annotations match {
case Nil if mdef.symbol != null =>
Expand Down
87 changes: 85 additions & 2 deletions tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,45 @@ class HoverTermSuite extends BaseHoverSuite {
)

check(
"apply-chain".tag(IgnoreScala2),
"function-chain",
"""
|trait Consumer {
| def subConsumer: Consumer
| def consume(value: Int): Unit
|}
|
|object O {
| val consumer: Consumer = ???
| List(1).foreach(<<consumer.subConsumer.co@@nsume>>)
|}
|""".stripMargin,
"""|```scala
|def consume(value: Int): Unit
|```
|""".stripMargin
)

check(
"function-chain2",
"""
|trait Consumer {
| def subConsumer: Consumer
| def consume(value: Int): Unit
|}
|
|object O {
| val consumer: Consumer = ???
| List(1).foreach(<<cons@@umer>>.subConsumer.consume)
|}
|""".stripMargin,
"""|```scala
|val consumer: Consumer
|```
|""".stripMargin
)

check(
"function-chain3",
"""
|trait Consumer {
| def subConsumer: Consumer
Expand All @@ -529,7 +567,7 @@ class HoverTermSuite extends BaseHoverSuite {
|
|object O {
| val consumer: Consumer = ???
| val m = consumer.subConsumer.<<co@@nsume>>
| List(1).foreach(<<consumer.subConsumer.subConsumer.con@@sume>>)
|}
|""".stripMargin,
"""|```scala
Expand All @@ -538,4 +576,49 @@ class HoverTermSuite extends BaseHoverSuite {
|""".stripMargin
)

check(
"function-chain4",
"""
|trait Consumer {
| def subConsumer[T](i: T): T
| def consume(value: Int)(n: Int): Unit
|}
|
|object O {
| val consumer: Consumer = ???
| List(1).foreach(<<consumer.su@@bConsumer(consumer)>>.consume(1))
|}
|""".stripMargin,
"""|**Expression type**:
|```scala
|Consumer
|```
|**Symbol signature**:
|```scala
|def subConsumer[T](i: T): T
|```
|""".stripMargin
)

check(
"function-chain5",
"""
|trait Consumer {
| def subConsumer[T](i: T): T
| def consume(value: Int)(n: Int): Unit
|}
|
|object O {
| def w = {
| val consumer: Consumer = ???
| List(1).foreach(consumer.subConsumer(<<consu@@mer>>).consume(1))
| }
|}
|""".stripMargin,
"""|```scala
|val consumer: Consumer
|```
|""".stripMargin
)

}

0 comments on commit d5889c8

Please sign in to comment.