From f31ad61adabe59da9006acc79334ccec453bf95d Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Tue, 30 Apr 2024 14:07:54 +0200 Subject: [PATCH] fix: delias type members in hover --- .../meta/internal/pc/HoverProvider.scala | 6 +- .../internal/mtags/MtagsEnrichments.scala | 10 +++- .../scala/tests/hover/HoverDefnSuite.scala | 10 +++- .../scala/tests/hover/HoverTermSuite.scala | 55 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) 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 714e6f3e75e..1dbe9cbfed6 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 @@ -238,7 +238,8 @@ class HoverProvider( lookupSymbol = name => context.lookupSymbol(name, _ => true) :: Nil, renames = re ) - val prettyType = metalsToLongString(tpe.widen.finalResultType, history) + val prettyType = + metalsToLongString(tpe.widen.finalResultType.map(_.dealias), history) val lspRange = if (range.isRange) Some(range.toLsp) else None Some( new ScalaHover( @@ -282,7 +283,8 @@ class HoverProvider( val flags = List(symbolFlagString(symbol), keyword, name) .filterNot(_.isEmpty) .mkString(" ") - val prettyType = metalsToLongString(tpe.widen.finalResultType, history) + val prettyType = + metalsToLongString(tpe.widen.finalResultType.map(_.dealias), history) val macroSuffix = if (symbol.isMacro) " = macro" else "" diff --git a/mtags/src/main/scala-3/scala/meta/internal/mtags/MtagsEnrichments.scala b/mtags/src/main/scala-3/scala/meta/internal/mtags/MtagsEnrichments.scala index 7cccab7d877..fa70710f208 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/mtags/MtagsEnrichments.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/mtags/MtagsEnrichments.scala @@ -21,8 +21,11 @@ import dotty.tools.dotc.core.Names.* import dotty.tools.dotc.core.StdNames.* import dotty.tools.dotc.core.SymDenotations.NoDenotation import dotty.tools.dotc.core.Symbols.* +import dotty.tools.dotc.core.Types.AliasingBounds import dotty.tools.dotc.core.Types.AppliedType +import dotty.tools.dotc.core.Types.RefinedType import dotty.tools.dotc.core.Types.Type +import dotty.tools.dotc.core.Types.TypeBounds import dotty.tools.dotc.interactive.Interactive import dotty.tools.dotc.interactive.InteractiveDriver import dotty.tools.dotc.util.SourcePosition @@ -375,8 +378,13 @@ object MtagsEnrichments extends ScalametaCommonEnrichments: def metalsDealias(using Context): Type = tpe.dealias match case app @ AppliedType(tycon, params) => - // we dealias applied type params by hand, because `dealias` doesn't do it AppliedType(tycon, params.map(_.metalsDealias)) + case aliasingBounds: AliasingBounds => + aliasingBounds.derivedAlias(aliasingBounds.alias.dealias) + case TypeBounds(lo, hi) => + TypeBounds(lo.dealias, hi.dealias) + case RefinedType(parent, name, refinedInfo) => + RefinedType(parent.dealias, name, refinedInfo.metalsDealias) case dealised => dealised end MtagsEnrichments diff --git a/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala b/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala index cc78de43532..151ba57bbd1 100644 --- a/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala +++ b/tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala @@ -167,7 +167,15 @@ class HoverDefnSuite extends BaseHoverSuite { """|object a { | type <> = Int |}""".stripMargin, - "type MyType: MyType".hover + """|**Expression type**: + |```scala + |Int + |``` + |**Symbol signature**: + |```scala + |type MyType: MyType + |``` + |""".stripMargin ) check( diff --git a/tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala b/tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala index 38f5f11a210..38e90114900 100644 --- a/tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala +++ b/tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala @@ -718,4 +718,59 @@ class HoverTermSuite extends BaseHoverSuite { |""".stripMargin, "".stripMargin ) + + check( + "dealias type members in val definition", + """object Obj { + | trait A extends Sup { self => + | type T + | def member : T + | } + | val x: A { type T = Int} = ??? + | + | <> + | + |}""".stripMargin, + """def member: Int""".stripMargin.hover + ) + + check( + "dealias-type-members-in-argument-of-anonymous-function", + """object Obj { + | trait A extends Sup { self => + | type T + | def fun( + | body: A { type T = self.T} => Unit + | ) = + | () + | } + | val x: A { type T = Int} = ??? + | + | x.fun { <> => + | () + | } + | + |}""".stripMargin, + """|**Expression type**: + |```scala + |A{type T = Int} + |``` + |**Symbol signature**: + |```scala + |yy: A{type T = dealias-type-members-in-argument-of-anonymous-function.Obj.x.T} + |``` + |""".stripMargin, + compat = Map( + "3" -> """|**Expression type**: + |```scala + |A{type T = Int} + |``` + |**Symbol signature**: + |```scala + |yy: A{type T = x.T} + |``` + |""".stripMargin + ) + ) + }