Skip to content

Commit d9141bb

Browse files
committed
IDE: Use MarkedString in hover
1 parent 6b59ea1 commit d9141bb

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import scala.io.Codec
1919
import dotc._
2020
import ast.{Trees, tpd}
2121
import core._, core.Decorators.{sourcePos => _, _}
22-
import Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
22+
import Comments.Comment, Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
2323
import classpath.ClassPathEntries
2424
import reporting._, reporting.diagnostic.MessageContainer
2525
import util._
@@ -346,9 +346,9 @@ class DottyLanguageServer extends LanguageServer
346346
else {
347347
import dotty.tools.dotc.core.Comments._
348348
val symbol = Interactive.enclosingSourceSymbol(trees, pos)
349-
val doc = ctx.docCtx.flatMap(_.docstring(symbol)).map(_.raw + " / ").getOrElse("")
350-
val str = tpw.show.toString
351-
new Hover(List(JEither.forLeft(doc + str)).asJava, null)
349+
val docComment = ctx.docCtx.flatMap(_.docstring(symbol))
350+
val markedString = docMarkedString(docComment, tpw.show.toString)
351+
new Hover(List(JEither.forRight(markedString)).asJava, null)
352352
}
353353
}
354354

@@ -465,6 +465,21 @@ object DottyLanguageServer {
465465
item
466466
}
467467

468+
private def docMarkedString(comment: Option[Comment], info: String): lsp4j.MarkedString = {
469+
470+
val formattedComment = comment.map { comment =>
471+
s"""```scala
472+
|${comment.raw}
473+
|```
474+
|""".stripMargin
475+
}.getOrElse("")
476+
477+
val markedString = new lsp4j.MarkedString()
478+
markedString.setValue(formattedComment + info)
479+
markedString
480+
}
481+
482+
468483
/** Create an lsp4j.SymbolInfo from a Symbol and a SourcePosition */
469484
def symbolInfo(sym: Symbol, pos: SourcePosition)(implicit ctx: Context): lsp4j.SymbolInformation = {
470485
def symbolKind(sym: Symbol)(implicit ctx: Context): lsp4j.SymbolKind = {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class HoverTest {
1212
@Test def hoverOnClassShowsDoc: Unit = {
1313
code"""$m1 /** foo */ ${m2}class Foo $m3 $m4""".withSource
1414
.hover(m1 to m2, "")
15-
.hover(m2 to m3, "/** foo */ / Foo")
15+
.hover(m2 to m3, """```scala
16+
|/** foo */
17+
|```
18+
|Foo""".stripMargin)
1619
.hover(m3 to m4, "")
1720
}
1821

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class CodeHover(override val range: CodeRange, expected: String) extends ActionO
2121
else {
2222
assertEquals(1, result.getContents.size)
2323
val content = result.getContents.get(0)
24-
assertTrue(content.isLeft)
25-
assertEquals(expected, content.getLeft)
24+
assertTrue(content.isRight)
25+
val markedString = content.getRight.getValue
26+
assertEquals(expected, markedString)
2627
}
2728
}
2829

0 commit comments

Comments
 (0)