@@ -44,19 +44,24 @@ import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
44
44
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
45
45
import java.util.concurrent.TimeUnit
46
46
47
+ // The maxmimum number of completion items
47
48
private const val MAX_COMPLETION_ITEMS = 50
48
49
50
+ // The minimum length after which completion lists are sorted
51
+ private const val MIN_SORT_LENGTH = 4
52
+
49
53
/* * Finds completions at the specified position. */
50
54
fun completions (file : CompiledFile , cursor : Int , config : CompletionConfiguration ): CompletionList {
51
55
val partial = findPartialIdentifier(file, cursor)
52
56
LOG .debug(" Looking for completions that match '{}'" , partial)
53
57
54
- val items = elementCompletionItems(file, cursor, config, partial).ifEmpty { keywordCompletionItems(partial) }
58
+ var isIncomplete = false
59
+ val items = elementCompletionItems(file, cursor, config, partial).ifEmpty { keywordCompletionItems(partial).also { isIncomplete = true } }
55
60
val itemList = items
56
61
.take(MAX_COMPLETION_ITEMS )
57
62
.toList()
58
63
.onEachIndexed { i, item -> item.sortText = i.toString().padStart(2 , ' 0' ) }
59
- val isIncomplete = (itemList.size == MAX_COMPLETION_ITEMS )
64
+ isIncomplete = isIncomplete || (itemList.size == MAX_COMPLETION_ITEMS )
60
65
61
66
return CompletionList (isIncomplete, itemList)
62
67
}
@@ -74,13 +79,17 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
74
79
/* * Finds completions based on the element around the user's cursor. */
75
80
private fun elementCompletionItems (file : CompiledFile , cursor : Int , config : CompletionConfiguration , partial : String ): Sequence <CompletionItem > {
76
81
val surroundingElement = completableElement(file, cursor) ? : return emptySequence()
77
- val completions = elementCompletions(file, cursor, surroundingElement)
82
+ val completions = elementCompletions(file, cursor, surroundingElement).toList() // DEBUG
83
+
84
+ println (" Before: " + completions.map { name(it) }) // DEBUG
78
85
79
- val matchesName = completions.filter { containsCharactersInOrder( name(it), partial, false ) }
80
- val sorted = matchesName.takeIf { partial.isNotEmpty() }?.sortedBy { stringDistance(name(it), partial) } ? : matchesName
86
+ val matchesName = completions.filter { name(it).startsWith( partial) }
87
+ val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) } ? : matchesName
81
88
val visible = sorted.filter(isVisible(file, cursor))
82
89
83
- return visible.map { completionItem(it, surroundingElement, file, config) }
90
+ println (" After: " + visible.map { name(it) }) // DEBUG
91
+
92
+ return visible.map { completionItem(it, surroundingElement, file, config) }.asSequence() // DEBUG
84
93
}
85
94
86
95
private val callPattern = Regex (" (.*)\\ ((?:\\ $\\ d+)?\\ )(?:\\ $0)?" )
0 commit comments