Avoid creating a separate synthetic file for each default import
This commit is contained in:
@@ -51,4 +51,9 @@ public class KtImportsFactory {
|
||||
public Collection<KtImportDirective> createImportDirectives(@NotNull Collection<ImportPath> importPaths) {
|
||||
return CollectionsKt.map(importPaths, this::createImportDirective);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Collection<KtImportDirective> createImportDirectivesNotCached(@NotNull Collection<ImportPath> importPaths) {
|
||||
return KtPsiFactoryKt.KtPsiFactory(project, false).createImportDirectives(importPaths);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,16 +410,34 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
|
||||
throw IllegalArgumentException("import path must not be empty")
|
||||
}
|
||||
|
||||
val importDirectiveBuilder = StringBuilder("import ")
|
||||
importDirectiveBuilder.append(importPath.pathStr)
|
||||
val file = createFile(buildString { appendImport (importPath) })
|
||||
return file.importDirectives.first()
|
||||
}
|
||||
|
||||
private fun StringBuilder.appendImport(importPath: ImportPath) {
|
||||
if (importPath.fqName.isRoot) {
|
||||
throw IllegalArgumentException("import path must not be empty")
|
||||
}
|
||||
|
||||
append("import ")
|
||||
append(importPath.pathStr)
|
||||
|
||||
val alias = importPath.alias
|
||||
if (alias != null) {
|
||||
importDirectiveBuilder.append(" as ").append(alias.asString())
|
||||
append(" as ").append(alias.asString())
|
||||
}
|
||||
}
|
||||
|
||||
fun createImportDirectives(paths: Collection<ImportPath>): List<KtImportDirective> {
|
||||
val fileContent = buildString {
|
||||
for (path in paths) {
|
||||
appendImport(path)
|
||||
append('\n')
|
||||
}
|
||||
}
|
||||
|
||||
val file = createFile(importDirectiveBuilder.toString())
|
||||
return file.importDirectives.first()
|
||||
val file = createFile(fileContent)
|
||||
return file.importDirectives
|
||||
}
|
||||
|
||||
fun createPrimaryConstructor(): KtPrimaryConstructor {
|
||||
|
||||
@@ -50,7 +50,7 @@ class FileScopeFactory(
|
||||
) {
|
||||
/* avoid constructing psi for default imports prematurely (time consuming in some scenarios) */
|
||||
private val defaultImports by storageManager.createLazyValue {
|
||||
ktImportsFactory.createImportDirectives(defaultImportProvider.defaultImports)
|
||||
ktImportsFactory.createImportDirectivesNotCached(defaultImportProvider.defaultImports)
|
||||
}
|
||||
|
||||
fun createScopesForFile(file: KtFile, existingImports: ImportingScope? = null): FileScopes {
|
||||
|
||||
Reference in New Issue
Block a user