Skip to content

Commit 341dc28

Browse files
committed
Improve display of completion results
When available, we display the formatted documentation for the imported symbol. For type symbols, we display the full name of the symbol, and for term symbols we show their type.
1 parent ffa4691 commit 341dc28

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,16 @@ object DottyLanguageServer {
795795

796796
val label = sym.name.show
797797
val item = new lsp4j.CompletionItem(label)
798-
item.setDetail(sym.info.widenTermRefExpr.show)
798+
val detail = if (sym.isType) sym.showFullName else sym.info.widenTermRefExpr.show
799+
item.setDetail(detail)
800+
ParsedComment.docOf(sym).foreach { doc =>
801+
item.setDocumentation(markupContent(doc.renderAsMarkdown))
802+
}
799803
item.setKind(completionItemKind(sym))
800804
item
801805
}
802806

803-
def hoverContent(content: String): lsp4j.MarkupContent = {
807+
def markupContent(content: String): lsp4j.MarkupContent = {
804808
if (content.isEmpty) null
805809
else {
806810
val markup = new lsp4j.MarkupContent
@@ -824,7 +828,7 @@ object DottyLanguageServer {
824828
buf.append(comment.renderAsMarkdown)
825829
}
826830

827-
hoverContent(buf.toString)
831+
markupContent(buf.toString)
828832
}
829833

830834
/** Create an lsp4j.SymbolInfo from a Symbol and a SourcePosition */
@@ -869,7 +873,7 @@ object DottyLanguageServer {
869873
val tparamsLabel = if (signature.tparams.isEmpty) "" else signature.tparams.mkString("[", ", ", "]")
870874
val returnTypeLabel = signature.returnType.map(t => s": $t").getOrElse("")
871875
val label = s"${signature.name}$tparamsLabel$paramLists$returnTypeLabel"
872-
val documentation = signature.doc.map(DottyLanguageServer.hoverContent)
876+
val documentation = signature.doc.map(DottyLanguageServer.markupContent)
873877
val sig = new lsp4j.SignatureInformation(label)
874878
sig.setParameters(paramInfoss.flatten.asJava)
875879
documentation.foreach(sig.setDocumentation(_))
@@ -878,7 +882,7 @@ object DottyLanguageServer {
878882

879883
/** Convert `param` to `ParameterInformation` */
880884
private def paramToParameterInformation(param: Signatures.Param): lsp4j.ParameterInformation = {
881-
val documentation = param.doc.map(DottyLanguageServer.hoverContent)
885+
val documentation = param.doc.map(DottyLanguageServer.markupContent)
882886
val info = new lsp4j.ParameterInformation(param.show)
883887
documentation.foreach(info.setDocumentation(_))
884888
info

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ class CompletionTest {
2525
withSources(
2626
code"""object Foo { class MyClass }""",
2727
code"""import Foo.My${m1}"""
28-
).completion(m1, Set(("MyClass", Class, "Object{...}")))
28+
).completion(m1, Set(("MyClass", Class, "Foo.MyClass")))
2929
}
3030

3131
@Test def ImportCompleteClassNoPrefix: Unit = {
3232
withSources(
3333
code"""object Foo { class MyClass }""",
3434
code"""import Foo.${m1}"""
3535
).completion(m1, results => {
36-
val myClass = ("MyClass", Class, "Object{...}")
37-
assertTrue(results.contains(("MyClass", Class, "Object{...}")))
36+
val myClass = ("MyClass", Class, "Foo.MyClass")
37+
assertTrue(results.contains(("MyClass", Class, "Foo.MyClass")))
3838

3939
// Verify that apart from `MyClass`, we only have the methods that exists on `Foo`
4040
assertTrue((results - myClass).forall { case (_, kind, _) => kind == Method })
@@ -50,7 +50,7 @@ class CompletionTest {
5050
class MyClass""",
5151
code"""package b
5252
import a.My${m1}"""
53-
).completion(m1, Set(("MyClass", Class, "Object{...}")))
53+
).completion(m1, Set(("MyClass", Class, "a.MyClass")))
5454
}
5555

5656
@Test def importCompleteFromClass: Unit = {
@@ -87,15 +87,15 @@ class CompletionTest {
8787
object Foo""",
8888
code"""package pgk1
8989
import pkg0.F${m1}"""
90-
).completion(m1, Set(("Foo", Class, "Object{...}")))
90+
).completion(m1, Set(("Foo", Class, "pkg0.Foo")))
9191
}
9292

9393
@Test def importCompleteIncludePackage: Unit = {
9494
withSources(
9595
code"""package foo.bar
9696
class Fizz""",
9797
code"""import foo.b${m1}"""
98-
).completion(m1, Set(("bar", Module, "{...}")))
98+
).completion(m1, Set(("bar", Module, "foo.bar")))
9999
}
100100

101101
@Test def importCompleteIncludeMembers: Unit = {
@@ -113,13 +113,13 @@ class CompletionTest {
113113
("myDef", Method, "=> Int"),
114114
("myVar", Variable, "Int"),
115115
("myObject", Module, "MyObject.myObject"),
116-
("myClass", Class, "Object{...}"),
117-
("myTrait", Class, "Object{...}")))
116+
("myClass", Class, "MyObject.myClass"),
117+
("myTrait", Class, "MyObject.myTrait")))
118118
}
119119

120120
@Test def importJavaClass: Unit = {
121121
code"""import java.io.FileDesc${m1}""".withSource
122-
.completion(m1, Set(("FileDescriptor", Class, "Object{...}")))
122+
.completion(m1, Set(("FileDescriptor", Class, "java.io.FileDescriptor")))
123123
}
124124

125125
@Test def importJavaStaticMethod: Unit = {
@@ -141,6 +141,6 @@ class CompletionTest {
141141

142142
@Test def importRename: Unit = {
143143
code"""import java.io.{FileDesc${m1} => Foo}""".withSource
144-
.completion(m1, Set(("FileDescriptor", Class, "Object{...}")))
144+
.completion(m1, Set(("FileDescriptor", Class, "java.io.FileDescriptor")))
145145
}
146146
}

0 commit comments

Comments
 (0)