@@ -20,6 +20,17 @@ import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}
20
20
21
21
import scala .collection .mutable
22
22
23
+ /**
24
+ * One of the results of a completion query.
25
+ *
26
+ * @param label The label of this completion result, or the text that this completion result
27
+ * should insert in the scope where the completion request happened.
28
+ * @param description The description of this completion result: the fully qualified name for
29
+ * types, or the type for terms.
30
+ * @param symbols The symbols that are matched by this completion result.
31
+ */
32
+ case class Completion (label : String , description : String , symbols : List [Symbol ])
33
+
23
34
object Completion {
24
35
25
36
import dotty .tools .dotc .ast .tpd ._
@@ -28,7 +39,7 @@ object Completion {
28
39
*
29
40
* @return offset and list of symbols for possible completions
30
41
*/
31
- def completions (pos : SourcePosition )(implicit ctx : Context ): (Int , List [Symbol ]) = {
42
+ def completions (pos : SourcePosition )(implicit ctx : Context ): (Int , List [Completion ]) = {
32
43
val path = Interactive .pathTo(ctx.compilationUnit.tpdTree, pos.pos)
33
44
computeCompletions(pos, path)(Interactive .contextOfPath(path))
34
45
}
@@ -100,7 +111,7 @@ object Completion {
100
111
new CompletionBuffer (mode, prefix, pos)
101
112
}
102
113
103
- private def computeCompletions (pos : SourcePosition , path : List [Tree ])(implicit ctx : Context ): (Int , List [Symbol ]) = {
114
+ private def computeCompletions (pos : SourcePosition , path : List [Tree ])(implicit ctx : Context ): (Int , List [Completion ]) = {
104
115
105
116
val offset = completionOffset(path)
106
117
val buffer = completionBuffer(path, pos)
@@ -131,15 +142,27 @@ object Completion {
131
142
/**
132
143
* Return the list of symbols that shoudl be included in completion results.
133
144
*
134
- * If the mode is `Import` and several symbols share the same name, the type symbols are
135
- * preferred over term symbols.
145
+ * If several symbols share the same name, the type symbols appear before term symbols inside
146
+ * the same `Completion`.
147
+ */
148
+ def getCompletions (implicit ctx : Context ): List [Completion ] = {
149
+ val groupedSymbols = completions.toList.groupBy(_.name.stripModuleClassSuffix.toSimpleName).toList
150
+ groupedSymbols.map { case (name, symbols) =>
151
+ val typesFirst = symbols.sortWith((s, _) => s.isType)
152
+ // Use distinct to remove duplicates with class, module class, etc.
153
+ val descriptions = typesFirst.map(description).distinct.mkString(" , " )
154
+ Completion (name.toString, descriptions, typesFirst)
155
+ }
156
+ }
157
+
158
+ /**
159
+ * A description for `sym`.
160
+ *
161
+ * For types, show the symbol's full name, or its type for term symbols.
136
162
*/
137
- def getCompletions (implicit ctx : Context ): List [Symbol ] = {
138
- // Show only the type symbols when there are multiple options with the same name
139
- completions.toList.groupBy(_.name.stripModuleClassSuffix.toSimpleName).mapValues {
140
- case sym :: Nil => sym :: Nil
141
- case syms => syms.filter(_.isType)
142
- }.values.flatten.toList
163
+ private def description (sym : Symbol )(implicit ctx : Context ): String = {
164
+ if (sym.isType) sym.showFullName
165
+ else sym.info.widenTermRefExpr.show
143
166
}
144
167
145
168
/**
0 commit comments