Skip to content

Commit 8fb9a9c

Browse files
committed
Only suggest unimported index completions
1 parent 2164f14 commit 8fb9a9c

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ fun completions(file: CompiledFile, cursor: Int, index: SymbolIndex, config: Com
6363
val partial = findPartialIdentifier(file, cursor)
6464
LOG.debug("Looking for completions that match '{}'", partial)
6565

66-
// TODO: Filter out already imported completions (i.e. those for which elementCompletions were already found)
67-
// from the index completion items.
6866
val (elementItems, isExhaustive) = elementCompletionItems(file, cursor, config, partial)
6967
val elementItemList = elementItems.take(MAX_COMPLETION_ITEMS).toList()
7068
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
7977
}
8078

8179
/** 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), "\nimport ${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), "\nimport ${it.fqName}")) // TODO: CRLF?
104+
} }
105+
}
103106

104107
/** Finds a good insertion position for a new import of the given fully-qualified name. */
105108
private fun findImportInsertionPosition(parsedFile: KtFile, fqName: FqName): Position =

0 commit comments

Comments
 (0)