@@ -63,8 +63,6 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
63
63
val partial = findPartialIdentifier(file, cursor)
64
64
LOG .debug(" Looking for completions that match '{}'" , partial)
65
65
66
- // TODO: Filter out already imported completions (i.e. those for which elementCompletions were already found)
67
- // from the index completion items.
68
66
val (elementItems, isExhaustive) = elementCompletionItems(file, cursor, config, partial)
69
67
val elementItemList = elementItems.take(MAX_COMPLETION_ITEMS ).toList()
70
68
val items = (elementItemList.asSequence() + if (isExhaustive) emptySequence() else indexCompletionItems(file.parse, index, partial))
@@ -79,27 +77,32 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
79
77
}
80
78
81
79
/* * Finds completions in the global symbol index, for potentially unimported symbols. */
82
- private fun indexCompletionItems (parsedFile : KtFile , index : SymbolIndex , partial : String ): Sequence <CompletionItem > = index
83
- .query(partial, limit = MAX_COMPLETION_ITEMS )
84
- .asSequence()
85
- .filter { it.kind != Symbol .Kind .MODULE } // Ignore global module/package name completions for now, since they cannot be 'imported'
86
- .map { CompletionItem ().apply {
87
- label = it.fqName.shortName().toString()
88
- kind = when (it.kind) {
89
- Symbol .Kind .CLASS -> CompletionItemKind .Class
90
- Symbol .Kind .INTERFACE -> CompletionItemKind .Interface
91
- Symbol .Kind .FUNCTION -> CompletionItemKind .Function
92
- Symbol .Kind .VARIABLE -> CompletionItemKind .Variable
93
- Symbol .Kind .MODULE -> CompletionItemKind .Module
94
- Symbol .Kind .ENUM -> CompletionItemKind .Enum
95
- Symbol .Kind .ENUM_MEMBER -> CompletionItemKind .EnumMember
96
- Symbol .Kind .CONSTRUCTOR -> CompletionItemKind .Constructor
97
- Symbol .Kind .FIELD -> CompletionItemKind .Field
98
- }
99
- detail = " (import from ${it.fqName.parent()} )"
100
- val pos = findImportInsertionPosition(parsedFile, it.fqName)
101
- additionalTextEdits = listOf (TextEdit (Range (pos, pos), " \n import ${it.fqName} " )) // TODO: CRLF?
102
- } }
80
+ private fun indexCompletionItems (parsedFile : KtFile , index : SymbolIndex , partial : String ): Sequence <CompletionItem > {
81
+ val importedFqNames = parsedFile.importDirectives.mapNotNull { it.importedFqName }.toSet()
82
+
83
+ return index
84
+ .query(partial, limit = MAX_COMPLETION_ITEMS )
85
+ .asSequence()
86
+ .filter { it.kind != Symbol .Kind .MODULE } // Ignore global module/package name completions for now, since they cannot be 'imported'
87
+ .filter { it.fqName !in importedFqNames }
88
+ .map { CompletionItem ().apply {
89
+ label = it.fqName.shortName().toString()
90
+ kind = when (it.kind) {
91
+ Symbol .Kind .CLASS -> CompletionItemKind .Class
92
+ Symbol .Kind .INTERFACE -> CompletionItemKind .Interface
93
+ Symbol .Kind .FUNCTION -> CompletionItemKind .Function
94
+ Symbol .Kind .VARIABLE -> CompletionItemKind .Variable
95
+ Symbol .Kind .MODULE -> CompletionItemKind .Module
96
+ Symbol .Kind .ENUM -> CompletionItemKind .Enum
97
+ Symbol .Kind .ENUM_MEMBER -> CompletionItemKind .EnumMember
98
+ Symbol .Kind .CONSTRUCTOR -> CompletionItemKind .Constructor
99
+ Symbol .Kind .FIELD -> CompletionItemKind .Field
100
+ }
101
+ detail = " (import from ${it.fqName.parent()} )"
102
+ val pos = findImportInsertionPosition(parsedFile, it.fqName)
103
+ additionalTextEdits = listOf (TextEdit (Range (pos, pos), " \n import ${it.fqName} " )) // TODO: CRLF?
104
+ } }
105
+ }
103
106
104
107
/* * Finds a good insertion position for a new import of the given fully-qualified name. */
105
108
private fun findImportInsertionPosition (parsedFile : KtFile , fqName : FqName ): Position =
0 commit comments