Skip to content

Commit c53b05a

Browse files
committed
Re-introduce string containment filter in completions
Checking whether the candidate contains the partial string improves performance since less candidates have to be sorted.
1 parent bc5b797 commit c53b05a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
4545
import java.util.concurrent.TimeUnit
4646

4747
private const val MAX_COMPLETION_ITEMS = 50
48-
private const val MAX_STRING_DISTANCE = 10
4948

5049
/** Finds completions at the specified position. */
5150
fun completions(file: CompiledFile, cursor: Int, config: CompletionConfiguration): CompletionList {
@@ -77,7 +76,9 @@ private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: Comp
7776
val surroundingElement = completableElement(file, cursor) ?: return emptySequence()
7877
val completions = elementCompletions(file, cursor, surroundingElement)
7978

80-
val matchesName = completions.sortedBy { stringDistance(name(it), partial).takeIf { it < MAX_STRING_DISTANCE || partial.isEmpty() } }
79+
val matchesName = completions
80+
.filter { containsCharactersInOrder(name(it), partial, false) }
81+
.sortedBy { stringDistance(name(it), partial) }
8182
val visible = matchesName.filter(isVisible(file, cursor))
8283

8384
return visible.map { completionItem(it, surroundingElement, file, config) }

shared/src/main/kotlin/org/javacs/kt/util/StringUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ fun stringDistance(candidate: CharSequence, pattern: CharSequence, maxOffset: In
3737
val iMax = Math.max(iCandidate, iPattern)
3838
iCandidate = iMax
3939
iPattern = iMax
40+
if (iMax >= Math.min(candidateLength, patternLength)) {
41+
break
42+
}
4043
}
4144

4245
searchWindow@

0 commit comments

Comments
 (0)