@@ -8,6 +8,7 @@ import org.eclipse.lsp4j.CompletionList
8
8
import org.javacs.kt.CompiledFile
9
9
import org.javacs.kt.LOG
10
10
import org.javacs.kt.CompletionConfiguration
11
+ import org.javacs.kt.index.Symbol
11
12
import org.javacs.kt.index.SymbolIndex
12
13
import org.javacs.kt.util.containsCharactersInOrder
13
14
import org.javacs.kt.util.findParent
@@ -59,7 +60,9 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
59
60
LOG .debug(" Looking for completions that match '{}'" , partial)
60
61
61
62
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 } }
63
66
val itemList = items
64
67
.take(MAX_COMPLETION_ITEMS )
65
68
.toList()
@@ -69,6 +72,25 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
69
72
return CompletionList (isIncomplete, itemList)
70
73
}
71
74
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
+
72
94
/* * Finds keyword completions starting with the given partial identifier. */
73
95
private fun keywordCompletionItems (partial : String ): Sequence <CompletionItem > =
74
96
(KtTokens .SOFT_KEYWORDS .getTypes() + KtTokens .KEYWORDS .getTypes()).asSequence()
@@ -80,9 +102,9 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
80
102
} }
81
103
82
104
/* * 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 > {
84
106
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)
86
108
87
109
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false ) }
88
110
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }
0 commit comments