@@ -63,12 +63,12 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
63
63
val partial = findPartialIdentifier(file, cursor)
64
64
LOG .debug(" Looking for completions that match '{}'" , partial)
65
65
66
- val (elementItems, isExhaustive) = elementCompletionItems(file, cursor, config, partial)
66
+ val (elementItems, isExhaustive, receiver ) = elementCompletionItems(file, cursor, config, partial)
67
67
val elementItemList = elementItems.toList()
68
68
val elementItemLabels = elementItemList.mapNotNull { it.label }.toSet()
69
69
val items = (
70
70
elementItemList.asSequence()
71
- + (if (! isExhaustive) indexCompletionItems(file.parse, index, partial).filter { it.label !in elementItemLabels } else emptySequence())
71
+ + (if (! isExhaustive) indexCompletionItems(file.parse, receiver, index, partial).filter { it.label !in elementItemLabels } else emptySequence())
72
72
+ (if (elementItemList.isEmpty()) keywordCompletionItems(partial) else emptySequence())
73
73
)
74
74
val itemList = items
@@ -81,7 +81,7 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
81
81
}
82
82
83
83
/* * Finds completions in the global symbol index, for potentially unimported symbols. */
84
- private fun indexCompletionItems (parsedFile : KtFile , index : SymbolIndex , partial : String ): Sequence <CompletionItem > {
84
+ private fun indexCompletionItems (parsedFile : KtFile , receiver : KtExpression ? , index : SymbolIndex , partial : String ): Sequence <CompletionItem > {
85
85
val imports = parsedFile.importDirectives
86
86
// TODO: Deal with alias imports
87
87
val wildcardPackages = imports
@@ -154,20 +154,24 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
154
154
kind = CompletionItemKind .Keyword
155
155
} }
156
156
157
- data class ElementCompletionItems (val items : Sequence <CompletionItem >, val isExhaustive : Boolean )
157
+ data class ElementCompletionItems (val items : Sequence <CompletionItem >, val isExhaustive : Boolean , val receiver : KtExpression ? = null )
158
158
159
159
/* * Finds completions based on the element around the user's cursor. */
160
160
private fun elementCompletionItems (file : CompiledFile , cursor : Int , config : CompletionConfiguration , partial : String ): ElementCompletionItems {
161
161
val surroundingElement = completableElement(file, cursor) ? : return ElementCompletionItems (emptySequence(), isExhaustive = true )
162
- val isExhaustive = surroundingElement !is KtNameReferenceExpression && surroundingElement !is KtTypeElement
163
162
val completions = elementCompletions(file, cursor, surroundingElement)
164
163
165
164
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false ) }
166
165
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }
167
166
? : matchesName.sortedBy { if (name(it).startsWith(partial)) 0 else 1 }
168
167
val visible = sorted.filter(isVisible(file, cursor))
169
168
170
- return ElementCompletionItems (visible.map { completionItem(it, surroundingElement, file, config) }, isExhaustive)
169
+ val isExhaustive = surroundingElement !is KtNameReferenceExpression
170
+ && surroundingElement !is KtTypeElement
171
+ && surroundingElement !is KtQualifiedExpression
172
+ val receiver = (surroundingElement as ? KtQualifiedExpression )?.receiverExpression
173
+
174
+ return ElementCompletionItems (visible.map { completionItem(it, surroundingElement, file, config) }, isExhaustive, receiver)
171
175
}
172
176
173
177
private val callPattern = Regex (" (.*)\\ ((?:\\ $\\ d+)?\\ )(?:\\ $0)?" )
0 commit comments