Skip to content

Commit 228071d

Browse files
committed
Update completion list logic
1 parent 20674d3 commit 228071d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

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

62-
var isIncomplete = false
6362
// TODO: Filter non-imported (i.e. the elementCompletions already found) and auto-import these when selected by the user
64-
val items = (elementCompletionItems(file, cursor, config, partial) + indexCompletionItems(index, partial))
65-
.ifEmpty { keywordCompletionItems(partial).also { isIncomplete = true } }
63+
val elementItems = elementCompletionItems(file, cursor, config, partial)
64+
val elementItemList = elementItems.take(MAX_COMPLETION_ITEMS).toList()
65+
val items = (elementItemList.asSequence() + indexCompletionItems(index, partial)).ifEmpty { keywordCompletionItems(partial) }
6666
val itemList = items
6767
.take(MAX_COMPLETION_ITEMS)
6868
.toList()
6969
.onEachIndexed { i, item -> item.sortText = i.toString().padStart(2, '0') }
70-
isIncomplete = isIncomplete || (itemList.size == MAX_COMPLETION_ITEMS)
70+
val isIncomplete = itemList.size >= MAX_COMPLETION_ITEMS || elementItemList.isEmpty()
7171

7272
return CompletionList(isIncomplete, itemList)
7373
}
7474

7575
/** Finds completions in the symbol index. */
7676
private fun indexCompletionItems(index: SymbolIndex, partial: String): Sequence<CompletionItem> = index
77-
.query(partial)
77+
.query(partial, limit = MAX_COMPLETION_ITEMS)
7878
.map { CompletionItem().apply {
7979
label = it.fqName.shortName().toString()
8080
kind = when (it.kind) {

server/src/main/kotlin/org/javacs/kt/index/SymbolIndex.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
1010
import org.jetbrains.kotlin.name.FqName
1111
import org.javacs.kt.LOG
1212
import org.jetbrains.exposed.sql.Database
13+
import org.jetbrains.exposed.sql.SqlExpressionBuilder.like
1314
import org.jetbrains.exposed.sql.insert
1415

1516
private object Symbols : Table() {
@@ -23,6 +24,7 @@ private object Symbols : Table() {
2324
*/
2425
class SymbolIndex {
2526
private val db = Database.connect("jdbc:h2:mem:symbolindex;DB_CLOSE_DELAY=-1", "org.h2.Driver")
27+
private var initialized = false
2628

2729
init {
2830
transaction(db) {
@@ -31,6 +33,10 @@ class SymbolIndex {
3133
}
3234

3335
fun update(module: ModuleDescriptor) {
36+
// TODO: Remove this once a proper debounce mechanism (and ideally incremental updates) are in place.
37+
if (initialized) return
38+
initialized = true
39+
3440
val started = System.currentTimeMillis()
3541
LOG.info("Updating symbol index...")
3642

@@ -70,7 +76,7 @@ class SymbolIndex {
7076

7177
fun query(prefix: String, limit: Int = 20): List<Symbol> = transaction(db) {
7278
Symbols
73-
.selectAll()
79+
.select { Symbols.shortName.like("$prefix%") }
7480
.limit(limit)
7581
.map { Symbol(FqName(it[Symbols.fqName]), Symbol.Kind.fromRaw(it[Symbols.kind])) }
7682
}

0 commit comments

Comments
 (0)