Skip to content

Commit

Permalink
feat: Add hover on class,object,type def in Scala 2
Browse files Browse the repository at this point in the history
Previously there was no hover shown on class/object/type definition.
  • Loading branch information
jkciesluk authored and tgodzik committed Aug 10, 2023
1 parent abd0b1f commit 1f4c43f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
17 changes: 17 additions & 0 deletions mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ class HoverProvider(val compiler: MetalsGlobal, params: OffsetParams)(implicit
range = pos,
Some(report)
)
// Class, object, type definitions, matches only if the cursor is over the definition name.
case t: MemberDef
if (t.namePosition
.includes(pos) || pos.includes(
t.namePosition
)) && t.symbol != null =>
val symbol = tree.symbol
val tpe = seenFromType(tree, symbol)
toHover(
symbol = symbol,
keyword = symbol.keyString,
seenFrom = tpe,
tpe = tpe,
pos = pos,
range = t.namePosition,
Some(report)
)
case _ =>
// Don't show hover for non-identifiers.
None
Expand Down
23 changes: 14 additions & 9 deletions tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,37 @@ class HoverDefnSuite extends BaseHoverSuite {

check(
"object",
"""object M@@yObject
"""object <<M@@yObject>>
|""".stripMargin,
"",
"object object.MyObject".hover,
compat = Map(
"3" -> "object MyObject: `object`".hover
),
)

check(
"trait",
"""trait M@@yTrait
"""trait <<M@@yTrait>>
|""".stripMargin,
"",
"abstract trait MyTrait: MyTrait".hover,
compat = Map(
"3" -> "trait MyTrait: MyTrait".hover
),
)

check(
"class",
"""trait M@@yClass
"""class <<M@@yClass>>
|""".stripMargin,
"",
compat = Map(
"3" -> "trait MyClass: MyClass".hover
),
"class MyClass: MyClass".hover,
)

check(
"type",
"""|object a {
| type <<M@@yType>> = Int
|}""".stripMargin,
"type MyType: MyType".hover,
)

check(
Expand Down
16 changes: 16 additions & 0 deletions tests/cross/src/test/scala/tests/hover/HoverNegativeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ class HoverNegativeSuite extends BaseHoverSuite {
|""".stripMargin,
)

checkNegative(
"object-keyword",
"""obj@@ect a {
| val x = 42
|}
|""".stripMargin,
)

checkNegative(
"type-keyword",
"""object a {
| ty@@pe Alpha = Int
|}
|""".stripMargin,
)

checkNegative(
"val-equal",
"""object a {
Expand Down

0 comments on commit 1f4c43f

Please sign in to comment.