Skip to content

Commit 4380c93

Browse files
committed
Check whether element completions are 'exhaustive'
1 parent 228071d commit 4380c93

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
6060
LOG.debug("Looking for completions that match '{}'", partial)
6161

6262
// TODO: Filter non-imported (i.e. the elementCompletions already found) and auto-import these when selected by the user
63-
val elementItems = elementCompletionItems(file, cursor, config, partial)
63+
val (elementItems, isExhaustive) = elementCompletionItems(file, cursor, config, partial)
6464
val elementItemList = elementItems.take(MAX_COMPLETION_ITEMS).toList()
65-
val items = (elementItemList.asSequence() + indexCompletionItems(index, partial)).ifEmpty { keywordCompletionItems(partial) }
65+
val items = (elementItemList.asSequence() + if (isExhaustive) emptySequence() else indexCompletionItems(index, partial))
66+
.ifEmpty { keywordCompletionItems(partial) }
6667
val itemList = items
6768
.take(MAX_COMPLETION_ITEMS)
6869
.toList()
@@ -101,17 +102,20 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
101102
kind = CompletionItemKind.Keyword
102103
} }
103104

105+
data class ElementCompletionItems(val items: Sequence<CompletionItem>, val isExhaustive: Boolean)
106+
104107
/** Finds completions based on the element around the user's cursor. */
105-
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
106-
val surroundingElement = completableElement(file, cursor) ?: return emptySequence()
108+
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): ElementCompletionItems {
109+
val surroundingElement = completableElement(file, cursor) ?: return ElementCompletionItems(emptySequence(), isExhaustive = false)
110+
val isExhaustive = surroundingElement !is KtNameReferenceExpression && surroundingElement !is KtTypeElement
107111
val completions = elementCompletions(file, cursor, surroundingElement)
108112

109113
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false) }
110114
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }
111115
?: matchesName.sortedBy { if (name(it).startsWith(partial)) 0 else 1 }
112116
val visible = sorted.filter(isVisible(file, cursor))
113117

114-
return visible.map { completionItem(it, surroundingElement, file, config) }
118+
return ElementCompletionItems(visible.map { completionItem(it, surroundingElement, file, config) }, isExhaustive)
115119
}
116120

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

0 commit comments

Comments
 (0)