Skip to content

Commit e63b5e8

Browse files
committed
WIP: First sort completions after n characters
1 parent 0c16bb2 commit e63b5e8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,24 @@ import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
4444
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
4545
import java.util.concurrent.TimeUnit
4646

47+
// The maxmimum number of completion items
4748
private const val MAX_COMPLETION_ITEMS = 50
4849

50+
// The minimum length after which completion lists are sorted
51+
private const val MIN_SORT_LENGTH = 4
52+
4953
/** Finds completions at the specified position. */
5054
fun completions(file: CompiledFile, cursor: Int, config: CompletionConfiguration): CompletionList {
5155
val partial = findPartialIdentifier(file, cursor)
5256
LOG.debug("Looking for completions that match '{}'", partial)
5357

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 } }
5560
val itemList = items
5661
.take(MAX_COMPLETION_ITEMS)
5762
.toList()
5863
.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)
6065

6166
return CompletionList(isIncomplete, itemList)
6267
}
@@ -74,13 +79,17 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
7479
/** Finds completions based on the element around the user's cursor. */
7580
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
7681
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
7885

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
8188
val visible = sorted.filter(isVisible(file, cursor))
8289

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
8493
}
8594

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

0 commit comments

Comments
 (0)