diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala index 28437936ab0..89b7a0bdff3 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala @@ -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 diff --git a/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala b/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala index bfb64513b0d..64e8d2223e5 100644 --- a/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala +++ b/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala @@ -137,9 +137,9 @@ class HoverDefnSuite extends BaseHoverSuite { check( "object", - """object M@@yObject + """object <> |""".stripMargin, - "", + "object object.MyObject".hover, compat = Map( "3" -> "object MyObject: `object`".hover ), @@ -147,9 +147,9 @@ class HoverDefnSuite extends BaseHoverSuite { check( "trait", - """trait M@@yTrait + """trait <> |""".stripMargin, - "", + "abstract trait MyTrait: MyTrait".hover, compat = Map( "3" -> "trait MyTrait: MyTrait".hover ), @@ -157,12 +157,17 @@ class HoverDefnSuite extends BaseHoverSuite { check( "class", - """trait M@@yClass + """class <> |""".stripMargin, - "", - compat = Map( - "3" -> "trait MyClass: MyClass".hover - ), + "class MyClass: MyClass".hover, + ) + + check( + "type", + """|object a { + | type <> = Int + |}""".stripMargin, + "type MyType: MyType".hover, ) check( diff --git a/tests/cross/src/test/scala/tests/hover/HoverNegativeSuite.scala b/tests/cross/src/test/scala/tests/hover/HoverNegativeSuite.scala index 897a86a5555..9e6193f520d 100644 --- a/tests/cross/src/test/scala/tests/hover/HoverNegativeSuite.scala +++ b/tests/cross/src/test/scala/tests/hover/HoverNegativeSuite.scala @@ -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 {