Skip to content

Show docs in the IDE #4648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
override def sourcesRequired = false

private val myInitCtx: Context = {
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive)
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive).addMode(Mode.ReadComments)
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
val ctx = setup(settings.toArray, rootCtx)._2
ctx.initialize()(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.io.Codec
import dotc._
import ast.{Trees, tpd}
import core._, core.Decorators.{sourcePos => _, _}
import Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
import Comments._, Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
import classpath.ClassPathEntries
import reporting._, reporting.diagnostic.MessageContainer
import util._
Expand Down Expand Up @@ -340,13 +340,16 @@ class DottyLanguageServer extends LanguageServer
implicit val ctx = driver.currentCtx

val pos = sourcePosition(driver, uri, params.getPosition)
val tp = Interactive.enclosingType(driver.openedTrees(uri), pos)
val trees = driver.openedTrees(uri)
val tp = Interactive.enclosingType(trees, pos)
val tpw = tp.widenTermRefExpr

if (tpw == NoType) new Hover
else {
val str = tpw.show.toString
new Hover(List(JEither.forLeft(str)).asJava, null)
val symbol = Interactive.enclosingSourceSymbol(trees, pos)
val docComment = ctx.docCtx.flatMap(_.docstring(symbol))
val markedStrings = docMarkedStrings(docComment, tpw.show.toString)
new Hover(markedStrings.map(JEither.forRight(_)).asJava, null)
}
}

Expand Down Expand Up @@ -463,6 +466,19 @@ object DottyLanguageServer {
item
}

private def docMarkedStrings(comment: Option[Comment], typeInfo: String): List[lsp4j.MarkedString] = {

val docHover = comment.map { comment =>
new lsp4j.MarkedString("scala", comment.raw)
}

val typeInfoHover = new lsp4j.MarkedString()
typeInfoHover.setValue(typeInfo)

typeInfoHover :: docHover.toList
}


/** Create an lsp4j.SymbolInfo from a Symbol and a SourcePosition */
def symbolInfo(sym: Symbol, pos: SourcePosition)(implicit ctx: Context): lsp4j.SymbolInformation = {
def symbolKind(sym: Symbol)(implicit ctx: Context): lsp4j.SymbolKind = {
Expand Down
47 changes: 27 additions & 20 deletions language-server/test/dotty/tools/languageserver/HoverTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,60 @@ import dotty.tools.languageserver.util.Code._
class HoverTest {

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

@Test def hoverOnClassShowsDoc: Unit = {
code"""$m1 /** foo */ ${m2}class Foo $m3 $m4""".withSource
.hover(m1 to m2, Nil)
.hover(m2 to m3, List("Foo", "/** foo */"))
.hover(m3 to m4, Nil)
}

@Test def hoverOnClass0: Unit = {
code"""$m1 ${m2}class Foo $m3 $m4""".withSource
.hover(m1 to m2, "")
.hover(m2 to m3, "Foo")
.hover(m3 to m4, "")
.hover(m1 to m2, Nil)
.hover(m2 to m3, "Foo" :: Nil)
.hover(m3 to m4, Nil)
}

@Test def hoverOnClass1: Unit = {
code"""$m1 ${m2}class Foo { } $m3 $m4""".withSource
.hover(m1 to m2, "")
.hover(m2 to m3, "Foo")
.hover(m3 to m4, "")
.hover(m1 to m2, Nil)
.hover(m2 to m3, "Foo" :: Nil)
.hover(m3 to m4, Nil)
}

@Test def hoverOnValDef0: Unit = {
code"""class Foo {
| ${m1}val x = ${m2}8$m3; ${m4}x$m5
|}""".withSource
.hover(m1 to m2, "Int")
.hover(m2 to m3, "Int(8)")
.hover(m4 to m5, "Int")
.hover(m1 to m2, "Int" :: Nil)
.hover(m2 to m3, "Int(8)" :: Nil)
.hover(m4 to m5, "Int" :: Nil)
}

@Test def hoverOnValDef1: Unit = {
code"""class Foo {
| ${m1}final val x = 8$m2; ${m3}x$m4
|}""".withSource
.hover(m1 to m2, "Int(8)")
.hover(m3 to m4, "Int(8)")
.hover(m1 to m2, "Int(8)" :: Nil)
.hover(m3 to m4, "Int(8)" :: Nil)
}

@Test def hoverOnDefDef0: Unit = {
code"""class Foo {
| ${m1}def x = ${m2}8$m3; ${m4}x$m5
|}""".withSource
.hover(m1 to m2, "Int")
.hover(m2 to m3, "Int(8)")
.hover(m4 to m5, "Int")
.hover(m1 to m2, "Int" :: Nil)
.hover(m2 to m3, "Int(8)" :: Nil)
.hover(m4 to m5, "Int" :: Nil)
}

@Test def hoverMissingRef0: Unit = {
code"""class Foo {
| ${m1}x$m2
|}""".withSource
.hover(m1 to m2, "<error not found: x>")
.hover(m1 to m2, "<error not found: x>" :: Nil)
}

@Test def hoverFun0: Unit = {
Expand All @@ -65,10 +72,10 @@ class HoverTest {
| ${m5}y($m6)$m7
|}
""".withSource
.hover(m1 to m2, "String(\"abc\")")
.hover(m3 to m4, "String")
.hover(m5 to m6, "(): Int")
.hover(m6 to m7, "Int")
.hover(m1 to m2, "String(\"abc\")" :: Nil)
.hover(m3 to m4, "String" :: Nil)
.hover(m5 to m6, "(): Int" :: Nil)
.hover(m6 to m7, "Int" :: Nil)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class CodeTester(sources: List[SourceWithPositions], actions: List[Action]) {
* Perform a hover over `range`, verifies that result matches `expected`.
*
* @param range The range over which to hover.
* @param expected The expected result.
* @param expected The expected results.
*
* @see dotty.tools.languageserver.util.actions.CodeHover
*/
def hover(range: CodeRange, expected: String): this.type =
def hover(range: CodeRange, expected: List[String]): this.type =
doAction(new CodeHover(range, expected))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import dotty.tools.languageserver.util.{CodeRange, PositionContext}

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

import scala.collection.JavaConverters._

/**
* An action requesting for the info shown when `range` is hovered.
* This action corresponds to the `textDocument/hover` method of the Language Server Protocol.
*
* @param range The range of positions that should be hovered.
* @param expected The expected result.
*/
class CodeHover(override val range: CodeRange, expected: String) extends ActionOnRange {
class CodeHover(override val range: CodeRange, expected: List[String]) extends ActionOnRange {

override def onMarker(marker: CodeMarker): Exec[Unit] = {
val result = server.hover(marker.toTextDocumentPositionParams).get()
assertNull(result.getRange)
if (expected.isEmpty) assertNull(result.getContents)
else {
assertEquals(1, result.getContents.size)
val content = result.getContents.get(0)
assertTrue(content.isLeft)
assertEquals(expected, content.getLeft)
assertEquals(expected.size, result.getContents.size)
expected.zip(result.getContents.asScala).foreach { case (expected, actual) =>
assertTrue(actual.isRight)
assertEquals(expected, actual.getRight.getValue)
}
}
}

Expand Down