Skip to content

Commit 60da1cf

Browse files
committed
Update completions to query symbol index db
1 parent 799690a commit 60da1cf

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.eclipse.lsp4j.CompletionList
88
import org.javacs.kt.CompiledFile
99
import org.javacs.kt.LOG
1010
import org.javacs.kt.CompletionConfiguration
11+
import org.javacs.kt.index.Symbol
1112
import org.javacs.kt.index.SymbolIndex
1213
import org.javacs.kt.util.containsCharactersInOrder
1314
import org.javacs.kt.util.findParent
@@ -59,7 +60,9 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
5960
LOG.debug("Looking for completions that match '{}'", partial)
6061

6162
var isIncomplete = false
62-
val items = elementCompletionItems(file, cursor, index, config, partial).ifEmpty { keywordCompletionItems(partial).also { isIncomplete = true } }
63+
// 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 } }
6366
val itemList = items
6467
.take(MAX_COMPLETION_ITEMS)
6568
.toList()
@@ -69,6 +72,25 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
6972
return CompletionList(isIncomplete, itemList)
7073
}
7174

75+
/** Finds completions in the symbol index. */
76+
private fun indexCompletionItems(index: SymbolIndex, partial: String): Sequence<CompletionItem> = index
77+
.query(partial)
78+
.map { CompletionItem().apply {
79+
label = it.fqName.shortName().toString()
80+
kind = when (it.kind) {
81+
Symbol.Kind.CLASS -> CompletionItemKind.Class
82+
Symbol.Kind.INTERFACE -> CompletionItemKind.Interface
83+
Symbol.Kind.FUNCTION -> CompletionItemKind.Function
84+
Symbol.Kind.VARIABLE -> CompletionItemKind.Variable
85+
Symbol.Kind.MODULE -> CompletionItemKind.Module
86+
Symbol.Kind.ENUM -> CompletionItemKind.Enum
87+
Symbol.Kind.ENUM_MEMBER -> CompletionItemKind.EnumMember
88+
Symbol.Kind.CONSTRUCTOR -> CompletionItemKind.Constructor
89+
Symbol.Kind.FIELD -> CompletionItemKind.Field
90+
}
91+
} }
92+
.asSequence()
93+
7294
/** Finds keyword completions starting with the given partial identifier. */
7395
private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
7496
(KtTokens.SOFT_KEYWORDS.getTypes() + KtTokens.KEYWORDS.getTypes()).asSequence()
@@ -80,9 +102,9 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
80102
} }
81103

82104
/** Finds completions based on the element around the user's cursor. */
83-
private fun elementCompletionItems(file: CompiledFile, cursor: Int, index: SymbolIndex, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
105+
private fun elementCompletionItems(file: CompiledFile, cursor: Int, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
84106
val surroundingElement = completableElement(file, cursor) ?: return emptySequence()
85-
val completions = elementCompletions(file, cursor, surroundingElement) + index.globalDescriptors.values // TODO: Filter non-imported (i.e. the elementCompletions already found) and auto-import these when selected by the user
107+
val completions = elementCompletions(file, cursor, surroundingElement)
86108

87109
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false) }
88110
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import org.jetbrains.kotlin.name.FqName
44

55
data class Symbol(
66
// TODO: Store location (e.g. using a URI)
7-
private val fqName: FqName,
8-
private val kind: Kind
7+
val fqName: FqName,
8+
val kind: Kind
99
) {
1010
enum class Kind(val rawValue: Int) {
1111
CLASS(0),

0 commit comments

Comments
 (0)