From 99b10609f379b032b1a714d29f3a4bc7ea117042 Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Fri, 23 Aug 2024 14:19:24 +0200 Subject: [PATCH] feat: show expected type for hole --- .../main/dotty/tools/pc/HoverProvider.scala | 9 ++- .../tools/pc/tests/hover/HoverHoleSuite.scala | 75 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 presentation-compiler/test/dotty/tools/pc/tests/hover/HoverHoleSuite.scala diff --git a/presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala index fd363dbd37a2..2ba1b6fc7d52 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala @@ -25,6 +25,8 @@ import dotty.tools.dotc.util.SourcePosition import dotty.tools.pc.printer.ShortenedTypePrinter import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam import dotty.tools.pc.utils.InteractiveEnrichments.* +import dotty.tools.dotc.ast.untpd.InferredTypeTree +import dotty.tools.dotc.core.StdNames object HoverProvider: @@ -131,7 +133,12 @@ object HoverProvider: .flatMap(symTpe => search.symbolDocumentation(symTpe._1, contentType)) .map(_.docstring()) .mkString("\n") - printer.expressionType(exprTpw) match + + val expresionTypeOpt = + if symbol.name == StdNames.nme.??? then + InferExpectedType(search, driver, params).infer() + else printer.expressionType(exprTpw) + expresionTypeOpt match case Some(expressionType) => val forceExpressionType = !pos.span.isZeroExtent || ( diff --git a/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverHoleSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverHoleSuite.scala new file mode 100644 index 000000000000..cc9bd9f39fac --- /dev/null +++ b/presentation-compiler/test/dotty/tools/pc/tests/hover/HoverHoleSuite.scala @@ -0,0 +1,75 @@ +package dotty.tools.pc.tests.hover + +import dotty.tools.pc.base.BaseHoverSuite + +import org.junit.Test + +class HoverHoleSuite extends BaseHoverSuite: + @Test def basic = + check( + """object a { + | val x: Int = ?@@?? + |} + |""".stripMargin, + """|**Expression type**: + |```scala + |Int + |``` + |**Symbol signature**: + |```scala + |def ???: Nothing + |``` + |""".stripMargin + ) + + @Test def function = + check( + """object a { + | def m(i: Int) = ??? + | val x = m(??@@?) + |} + |""".stripMargin, + """|**Expression type**: + |```scala + |Int + |``` + |**Symbol signature**: + |```scala + |def ???: Nothing + |``` + |""".stripMargin + ) + + @Test def constructor = + check( + """object a { + | val x = List(1, ?@@??) + |} + |""".stripMargin, + """|**Expression type**: + |```scala + |Int + |``` + |**Symbol signature**: + |```scala + |def ???: Nothing + |``` + |""".stripMargin + ) + + @Test def bounds = + check( + """|trait Foo + |def foo[T <: Foo](a: T): Boolean = ??? + |val _ = foo(?@@??) + |""".stripMargin, + """|**Expression type**: + |```scala + |Foo + |``` + |**Symbol signature**: + |```scala + |def ???: Nothing + |``` + |""".stripMargin + )