Skip to content

Commit

Permalink
Revert "Always display Expression Type on hovers"
Browse files Browse the repository at this point in the history
This reverts commit c457790.
  • Loading branch information
KacperFKorban committed Aug 31, 2021
1 parent 23e32a6 commit 4d956c8
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 350 deletions.
13 changes: 10 additions & 3 deletions mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class HoverProvider(val compiler: MetalsGlobal, params: OffsetParams) {
pos = pos,
range = pos
)
case _: Literal =>
case _: Literal if params.isInstanceOf[RangeParams] =>
val symbol = tree.symbol
toHover(
symbol = symbol,
Expand Down Expand Up @@ -204,7 +204,9 @@ class HoverProvider(val compiler: MetalsGlobal, params: OffsetParams) {
range: Position
): Option[Hover] = {
if (tpe == null || tpe.isErroneous || tpe == NoType) None
else if (symbol == null || symbol == NoSymbol || symbol.isErroneous) {
else if (
pos.start != pos.end && (symbol == null || symbol == NoSymbol || symbol.isErroneous)
) {
val context = doLocateContext(pos)
val history = new ShortenedNames(
lookupSymbol = name => context.lookupSymbol(name, _ => true) :: Nil
Expand Down Expand Up @@ -258,7 +260,12 @@ class HoverProvider(val compiler: MetalsGlobal, params: OffsetParams) {
} else {
""
}
val markdown = HoverMarkup(prettyType, prettySignature, docstring)
val markdown = HoverMarkup(
prettyType,
prettySignature,
docstring,
pos.start != pos.end
)
val hover = new Hover(markdown.toMarkupContent)
if (range.isRange) {
hover.setRange(range.toLSP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ import scala.meta.pc.CancelToken
import scala.meta.pc.OffsetParams
import scala.meta.pc.RangeParams

trait OffsetParamsUtils {
protected def syntaxURI(pos: Position): URI = {
val syntax = pos.input.syntax
try {
val uri = URI.create(syntax)
Paths.get(uri)
uri
} catch {
case _: IllegalArgumentException | _: URISyntaxException =>
Paths.get(syntax).toUri
}
}
}

case class CompilerOffsetParams(
uri: URI,
text: String,
offset: Int,
token: CancelToken = EmptyCancelToken
) extends OffsetParams

object CompilerOffsetParams {
object CompilerOffsetParams extends OffsetParamsUtils {

def fromPos(pos: Position, token: CancelToken): CompilerOffsetParams = {
val syntax = pos.input.syntax
val uri =
try {
val uri = URI.create(syntax)
Paths.get(uri)
uri
} catch {
case _: IllegalArgumentException | _: URISyntaxException =>
Paths.get(syntax).toUri
}
val uri = syntaxURI(pos)
CompilerOffsetParams(
uri,
pos.input.text,
Expand All @@ -55,19 +60,10 @@ case class CompilerRangeParams(
)
}

object CompilerRangeParams {
object CompilerRangeParams extends OffsetParamsUtils {

def fromPos(pos: Position, token: CancelToken): CompilerRangeParams = {
val syntax = pos.input.syntax
val uri =
try {
val uri = URI.create(syntax)
Paths.get(uri)
uri
} catch {
case _: IllegalArgumentException | _: URISyntaxException =>
Paths.get(syntax).toUri
}
val uri = syntaxURI(pos)
CompilerRangeParams(
uri,
pos.input.text,
Expand Down
19 changes: 12 additions & 7 deletions mtags/src/main/scala/scala/meta/internal/pc/HoverMarkup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ object HoverMarkup {
def apply(
expressionType: String,
symbolSignature: String,
docstring: String
docstring: String,
forceExpressionType: Boolean = false
): String = {
val markdown = new StringBuilder()
markdown
.append("**Expression type**:\n")
.append("```scala\n")
.append(expressionType)
.append("\n```\n")
val needsExpressionType =
forceExpressionType || !symbolSignature.endsWith(expressionType)
if (needsExpressionType) {
markdown
.append("**Expression type**:\n")
.append("```scala\n")
.append(expressionType)
.append("\n```\n")
}
if (symbolSignature.nonEmpty) {
markdown
.append("**Symbol signature**:\n")
.append(if (needsExpressionType) "**Symbol signature**:\n" else "")
.append("```scala\n")
.append(symbolSignature)
.append("\n```")
Expand Down
96 changes: 31 additions & 65 deletions tests/cross/src/test/scala/tests/hover/HoverDefnSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<val @@x = List(1)>>
|}
|""".stripMargin,
"""|List[Int]
|val x: List[Int]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "val x: List[Int]".hover
)
"""|val x: List[Int]
|""".stripMargin.hover
)

check(
Expand All @@ -24,12 +20,8 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<var @@x = List(1)>>
|}
|""".stripMargin,
"""|List[Int]
|var x: List[Int]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "var x: List[Int]".hover
)
"""|var x: List[Int]
|""".stripMargin.hover
)

check(
Expand All @@ -38,12 +30,8 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<def @@x = List(1)>>
|}
|""".stripMargin,
"""|List[Int]
|def x: List[Int]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "def x: List[Int]".hover
)
"""|def x: List[Int]
|""".stripMargin.hover
)

check(
Expand All @@ -52,12 +40,8 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<def @@method(x: Int) = List(x)>>
|}
|""".stripMargin,
"""|List[Int]
|def method(x: Int): List[Int]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "def method(x: Int): List[Int]".hover
)
"""|def method(x: Int): List[Int]
|""".stripMargin.hover
)

check(
Expand All @@ -66,12 +50,8 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<def @@empty[T] = Option.empty[T]>>
|}
|""".stripMargin,
"""|Option[T]
|def empty[T]: Option[T]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "def empty[T]: Option[T]".hover
)
"""|def empty[T]: Option[T]
|""".stripMargin.hover
)

check(
Expand All @@ -83,10 +63,7 @@ class HoverDefnSuite extends BaseHoverSuite {
"""
|Option[T]
|def empty[T: Ordering]: Option[T]
|""".stripMargin.hover,
compat = Map(
"3.0" -> "def empty[T: Ordering]: Option[T]".hover
)
|""".stripMargin.hover
)

check(
Expand All @@ -95,12 +72,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| List(1).map(<<@@x>> => )
|}
|""".stripMargin,
"""|Int
"""|```scala
|x: Int
|""".stripMargin.hover,
compat = Map(
"3.0" -> "x: Int".hover
)
|```
|""".stripMargin
)

check(
Expand All @@ -109,12 +84,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| def method(<<@@x: Int>>): Int = x
|}
|""".stripMargin,
"""|Int
"""|```scala
|x: Int
|""".stripMargin.hover,
compat = Map(
"3.0" -> "x: Int".hover
)
|```
|""".stripMargin
)

check(
Expand All @@ -123,9 +96,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| <<def t@@his(x: Int) = this()>>
|}
|""".stripMargin,
"""|a
"""|```scala
|def this(x: Int): a
|""".stripMargin.hover,
|```
|""".stripMargin,
compat = Map(
"3.0" -> "def <init>(x: Int): a".hover
)
Expand All @@ -137,12 +111,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| def this(<<@@x: Int>>) = this()
|}
|""".stripMargin,
"""|Int
"""|```scala
|x: Int
|""".stripMargin.hover,
compat = Map(
"3.0" -> "x: Int".hover
)
|```
|""".stripMargin
)

check(
Expand All @@ -151,12 +123,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| def method(implicit <<@@x: Int>>) = this()
|}
|""".stripMargin,
"""|Int
"""|```scala
|implicit x: Int
|""".stripMargin.hover,
compat = Map(
"3.0" -> "implicit x: Int".hover
)
|```
|""".stripMargin
)

check(
Expand All @@ -165,12 +135,10 @@ class HoverDefnSuite extends BaseHoverSuite {
| def method(implicit y: Int, <<@@x: Int>>) = this()
|}
|""".stripMargin,
"""|Int
"""|```scala
|implicit x: Int
|""".stripMargin.hover,
compat = Map(
"3.0" -> "implicit x: Int".hover
)
|```
|""".stripMargin
)

check(
Expand Down Expand Up @@ -228,8 +196,7 @@ class HoverDefnSuite extends BaseHoverSuite {
| }
|}
|""".stripMargin,
"""|Int
|head: Int""".stripMargin.hover,
"head: Int".hover,
compat = Map(
"3.0" -> "val head: Int".hover
)
Expand All @@ -244,8 +211,7 @@ class HoverDefnSuite extends BaseHoverSuite {
| }
|}
|""".stripMargin,
"""|Int
|value: Int""".stripMargin.hover,
"value: Int".hover,
compat = Map(
"3.0" -> "val value: Int".hover
)
Expand Down
Loading

0 comments on commit 4d956c8

Please sign in to comment.