Skip to content

Commit 8b80e75

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 4331311 commit 8b80e75

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
@@ -794,12 +794,16 @@ object DottyLanguageServer {
794794

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

802-
def hoverContent(content: String): lsp4j.MarkupContent = {
806+
def markupContent(content: String): lsp4j.MarkupContent = {
803807
if (content.isEmpty) null
804808
else {
805809
val markup = new lsp4j.MarkupContent
@@ -823,7 +827,7 @@ object DottyLanguageServer {
823827
buf.append(comment.renderAsMarkdown)
824828
}
825829

826-
hoverContent(buf.toString)
830+
markupContent(buf.toString)
827831
}
828832

829833
/** Create an lsp4j.SymbolInfo from a Symbol and a SourcePosition */
@@ -868,7 +872,7 @@ object DottyLanguageServer {
868872
val tparamsLabel = if (signature.tparams.isEmpty) "" else signature.tparams.mkString("[", ", ", "]")
869873
val returnTypeLabel = signature.returnType.map(t => s": $t").getOrElse("")
870874
val label = s"${signature.name}$tparamsLabel$paramLists$returnTypeLabel"
871-
val documentation = signature.doc.map(DottyLanguageServer.hoverContent)
875+
val documentation = signature.doc.map(DottyLanguageServer.markupContent)
872876
val sig = new lsp4j.SignatureInformation(label)
873877
sig.setParameters(paramInfoss.flatten.asJava)
874878
documentation.foreach(sig.setDocumentation(_))
@@ -877,7 +881,7 @@ object DottyLanguageServer {
877881

878882
/** Convert `param` to `ParameterInformation` */
879883
private def paramToParameterInformation(param: Signatures.Param): lsp4j.ParameterInformation = {
880-
val documentation = param.doc.map(DottyLanguageServer.hoverContent)
884+
val documentation = param.doc.map(DottyLanguageServer.markupContent)
881885
val info = new lsp4j.ParameterInformation(param.show)
882886
documentation.foreach(info.setDocumentation(_))
883887
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)