Skip to content

Commit 53aecf1

Browse files
committed
Put doc and type info in separate hover
1 parent c91e83a commit 53aecf1

File tree

4 files changed

+43
-46
lines changed

4 files changed

+43
-46
lines changed

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

+10-12
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ class DottyLanguageServer extends LanguageServer
347347
import dotty.tools.dotc.core.Comments._
348348
val symbol = Interactive.enclosingSourceSymbol(trees, pos)
349349
val docComment = ctx.docCtx.flatMap(_.docstring(symbol))
350-
val markedString = docMarkedString(docComment, tpw.show.toString)
351-
new Hover(List(JEither.forRight(markedString)).asJava, null)
350+
val markedStrings = docMarkedStrings(docComment, tpw.show.toString)
351+
new Hover(markedStrings.map(JEither.forRight(_)).asJava, null)
352352
}
353353
}
354354

@@ -465,18 +465,16 @@ object DottyLanguageServer {
465465
item
466466
}
467467

468-
private def docMarkedString(comment: Option[Comment], info: String): lsp4j.MarkedString = {
468+
private def docMarkedStrings(comment: Option[Comment], typeInfo: String): List[lsp4j.MarkedString] = {
469469

470-
val formattedComment = comment.map { comment =>
471-
s"""```scala
472-
|${comment.raw}
473-
|```
474-
|""".stripMargin
475-
}.getOrElse("")
470+
val docHover = comment.map { comment =>
471+
new lsp4j.MarkedString("scala", comment.raw)
472+
}
473+
474+
val typeInfoHover = new lsp4j.MarkedString()
475+
typeInfoHover.setValue(typeInfo)
476476

477-
val markedString = new lsp4j.MarkedString()
478-
markedString.setValue(formattedComment + info)
479-
markedString
477+
docHover.toList :+ typeInfoHover
480478
}
481479

482480

language-server/test/dotty/tools/languageserver/HoverTest.scala

+23-26
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,60 @@ import dotty.tools.languageserver.util.Code._
77
class HoverTest {
88

99
@Test def hoverOnWhiteSpace0: Unit =
10-
code"$m1 $m2".withSource.hover(m1 to m2, "")
10+
code"$m1 $m2".withSource.hover(m1 to m2, Nil)
1111

1212
@Test def hoverOnClassShowsDoc: Unit = {
1313
code"""$m1 /** foo */ ${m2}class Foo $m3 $m4""".withSource
14-
.hover(m1 to m2, "")
15-
.hover(m2 to m3, """```scala
16-
|/** foo */
17-
|```
18-
|Foo""".stripMargin)
19-
.hover(m3 to m4, "")
14+
.hover(m1 to m2, Nil)
15+
.hover(m2 to m3, List("/** foo */", "Foo"))
16+
.hover(m3 to m4, Nil)
2017
}
2118

2219
@Test def hoverOnClass0: Unit = {
2320
code"""$m1 ${m2}class Foo $m3 $m4""".withSource
24-
.hover(m1 to m2, "")
25-
.hover(m2 to m3, "Foo")
26-
.hover(m3 to m4, "")
21+
.hover(m1 to m2, Nil)
22+
.hover(m2 to m3, "Foo" :: Nil)
23+
.hover(m3 to m4, Nil)
2724
}
2825

2926
@Test def hoverOnClass1: Unit = {
3027
code"""$m1 ${m2}class Foo { } $m3 $m4""".withSource
31-
.hover(m1 to m2, "")
32-
.hover(m2 to m3, "Foo")
33-
.hover(m3 to m4, "")
28+
.hover(m1 to m2, Nil)
29+
.hover(m2 to m3, "Foo" :: Nil)
30+
.hover(m3 to m4, Nil)
3431
}
3532

3633
@Test def hoverOnValDef0: Unit = {
3734
code"""class Foo {
3835
| ${m1}val x = ${m2}8$m3; ${m4}x$m5
3936
|}""".withSource
40-
.hover(m1 to m2, "Int")
41-
.hover(m2 to m3, "Int(8)")
42-
.hover(m4 to m5, "Int")
37+
.hover(m1 to m2, "Int" :: Nil)
38+
.hover(m2 to m3, "Int(8)" :: Nil)
39+
.hover(m4 to m5, "Int" :: Nil)
4340
}
4441

4542
@Test def hoverOnValDef1: Unit = {
4643
code"""class Foo {
4744
| ${m1}final val x = 8$m2; ${m3}x$m4
4845
|}""".withSource
49-
.hover(m1 to m2, "Int(8)")
50-
.hover(m3 to m4, "Int(8)")
46+
.hover(m1 to m2, "Int(8)" :: Nil)
47+
.hover(m3 to m4, "Int(8)" :: Nil)
5148
}
5249

5350
@Test def hoverOnDefDef0: Unit = {
5451
code"""class Foo {
5552
| ${m1}def x = ${m2}8$m3; ${m4}x$m5
5653
|}""".withSource
57-
.hover(m1 to m2, "Int")
58-
.hover(m2 to m3, "Int(8)")
59-
.hover(m4 to m5, "Int")
54+
.hover(m1 to m2, "Int" :: Nil)
55+
.hover(m2 to m3, "Int(8)" :: Nil)
56+
.hover(m4 to m5, "Int" :: Nil)
6057
}
6158

6259
@Test def hoverMissingRef0: Unit = {
6360
code"""class Foo {
6461
| ${m1}x$m2
6562
|}""".withSource
66-
.hover(m1 to m2, "<error not found: x>")
63+
.hover(m1 to m2, "<error not found: x>" :: Nil)
6764
}
6865

6966
@Test def hoverFun0: Unit = {
@@ -75,10 +72,10 @@ class HoverTest {
7572
| ${m5}y($m6)$m7
7673
|}
7774
""".withSource
78-
.hover(m1 to m2, "String(\"abc\")")
79-
.hover(m3 to m4, "String")
80-
.hover(m5 to m6, "(): Int")
81-
.hover(m6 to m7, "Int")
75+
.hover(m1 to m2, "String(\"abc\")" :: Nil)
76+
.hover(m3 to m4, "String" :: Nil)
77+
.hover(m5 to m6, "(): Int" :: Nil)
78+
.hover(m6 to m7, "Int" :: Nil)
8279
}
8380

8481
}

language-server/test/dotty/tools/languageserver/util/CodeTester.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class CodeTester(sources: List[SourceWithPositions], actions: List[Action]) {
2525
* Perform a hover over `range`, verifies that result matches `expected`.
2626
*
2727
* @param range The range over which to hover.
28-
* @param expected The expected result.
28+
* @param expected The expected results.
2929
*
3030
* @see dotty.tools.languageserver.util.actions.CodeHover
3131
*/
32-
def hover(range: CodeRange, expected: String): this.type =
32+
def hover(range: CodeRange, expected: List[String]): this.type =
3333
doAction(new CodeHover(range, expected))
3434

3535
/**

language-server/test/dotty/tools/languageserver/util/actions/CodeHover.scala

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ import dotty.tools.languageserver.util.{CodeRange, PositionContext}
55

66
import org.junit.Assert.{assertEquals, assertNull, assertTrue}
77

8+
import scala.collection.JavaConverters._
9+
810
/**
911
* An action requesting for the info shown when `range` is hovered.
1012
* This action corresponds to the `textDocument/hover` method of the Language Server Protocol.
1113
*
1214
* @param range The range of positions that should be hovered.
1315
* @param expected The expected result.
1416
*/
15-
class CodeHover(override val range: CodeRange, expected: String) extends ActionOnRange {
17+
class CodeHover(override val range: CodeRange, expected: List[String]) extends ActionOnRange {
1618

1719
override def onMarker(marker: CodeMarker): Exec[Unit] = {
1820
val result = server.hover(marker.toTextDocumentPositionParams).get()
1921
assertNull(result.getRange)
2022
if (expected.isEmpty) assertNull(result.getContents)
2123
else {
22-
assertEquals(1, result.getContents.size)
23-
val content = result.getContents.get(0)
24-
assertTrue(content.isRight)
25-
val markedString = content.getRight.getValue
26-
assertEquals(expected, markedString)
24+
assertEquals(expected.size, result.getContents.size)
25+
expected.zip(result.getContents.asScala).foreach { case (expected, actual) =>
26+
assertTrue(actual.isRight)
27+
assertEquals(expected, actual.getRight.getValue)
28+
}
2729
}
2830
}
2931

0 commit comments

Comments
 (0)