Skip to content

Commit 3eb183e

Browse files
committed
Pass receiver to index completions (WIP)
1 parent f0b497c commit 3eb183e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

server/src/main/kotlin/org/javacs/kt/completion/Completions.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
6363
val partial = findPartialIdentifier(file, cursor)
6464
LOG.debug("Looking for completions that match '{}'", partial)
6565

66-
val (elementItems, isExhaustive) = elementCompletionItems(file, cursor, config, partial)
66+
val (elementItems, isExhaustive, receiver) = elementCompletionItems(file, cursor, config, partial)
6767
val elementItemList = elementItems.toList()
6868
val elementItemLabels = elementItemList.mapNotNull { it.label }.toSet()
6969
val items = (
7070
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())
7272
+ (if (elementItemList.isEmpty()) keywordCompletionItems(partial) else emptySequence())
7373
)
7474
val itemList = items
@@ -81,7 +81,7 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
8181
}
8282

8383
/** 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> {
8585
val imports = parsedFile.importDirectives
8686
// TODO: Deal with alias imports
8787
val wildcardPackages = imports
@@ -154,20 +154,24 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
154154
kind = CompletionItemKind.Keyword
155155
} }
156156

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)
158158

159159
/** Finds completions based on the element around the user's cursor. */
160160
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): ElementCompletionItems {
161161
val surroundingElement = completableElement(file, cursor) ?: return ElementCompletionItems(emptySequence(), isExhaustive = true)
162-
val isExhaustive = surroundingElement !is KtNameReferenceExpression && surroundingElement !is KtTypeElement
163162
val completions = elementCompletions(file, cursor, surroundingElement)
164163

165164
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false) }
166165
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }
167166
?: matchesName.sortedBy { if (name(it).startsWith(partial)) 0 else 1 }
168167
val visible = sorted.filter(isVisible(file, cursor))
169168

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)
171175
}
172176

173177
private val callPattern = Regex("(.*)\\((?:\\$\\d+)?\\)(?:\\$0)?")

0 commit comments

Comments
 (0)