Skip to content

Commit a2c0545

Browse files
committed
Apply a basic sorter for short partial identifiers
If the user enters an identifier that is shorter than a certain fixed length, the completions will not be sorted by string distance. Instead, the completions with a matching prefix will be placed before that other ones in the list.
1 parent e63b5e8 commit a2c0545

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,14 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
7979
/** Finds completions based on the element around the user's cursor. */
8080
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
8181
val surroundingElement = completableElement(file, cursor) ?: return emptySequence()
82-
val completions = elementCompletions(file, cursor, surroundingElement).toList() // DEBUG
82+
val completions = elementCompletions(file, cursor, surroundingElement)
8383

84-
println("Before: " + completions.map { name(it) }) // DEBUG
85-
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
84+
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false) }
85+
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }
86+
?: matchesName.sortedBy { if (name(it).startsWith(partial)) 0 else 1 }
8887
val visible = sorted.filter(isVisible(file, cursor))
8988

90-
println("After: " + visible.map { name(it) }) // DEBUG
91-
92-
return visible.map { completionItem(it, surroundingElement, file, config) }.asSequence() // DEBUG
89+
return visible.map { completionItem(it, surroundingElement, file, config) }
9390
}
9491

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

0 commit comments

Comments
 (0)