Skip to content

Commit a8a3942

Browse files
committed
IDE: Use MarkedString in hover
1 parent f343e44 commit a8a3942

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._
@@ -348,9 +348,9 @@ class DottyLanguageServer extends LanguageServer
348348
else {
349349
import dotty.tools.dotc.core.Comments._
350350
val symbol = Interactive.enclosingSourceSymbol(trees, pos)
351-
val doc = ctx.docCtx.flatMap(_.docstring(symbol)).map(_.raw + " / ").getOrElse("")
352-
val str = tpw.show.toString
353-
new Hover(List(JEither.forLeft(doc + str)).asJava, null)
351+
val docComment = ctx.docCtx.flatMap(_.docstring(symbol))
352+
val markedString = docMarkedString(docComment, tpw.show.toString)
353+
new Hover(List(JEither.forRight(markedString)).asJava, null)
354354
}
355355
}
356356

@@ -467,6 +467,21 @@ object DottyLanguageServer {
467467
item
468468
}
469469

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