Minor: refactoring KotlinAddImportAction
This commit is contained in:
@@ -103,11 +103,10 @@ internal fun createGroupedImportsAction(
|
|||||||
.groupBy { it.parentOrNull() ?: FqName.ROOT }
|
.groupBy { it.parentOrNull() ?: FqName.ROOT }
|
||||||
.map {
|
.map {
|
||||||
val samePackageFqNames = it.value
|
val samePackageFqNames = it.value
|
||||||
val descriptors = samePackageFqNames.flatMap { file.resolveImportReference(it) }
|
val descriptors = samePackageFqNames.flatMap { fqName -> file.resolveImportReference(fqName) }
|
||||||
val variant = if (samePackageFqNames.size > 1) {
|
val variant = if (samePackageFqNames.size > 1) {
|
||||||
GroupedImportVariant(autoImportDescription, descriptors)
|
GroupedImportVariant(autoImportDescription, descriptors)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
SingleImportVariant(samePackageFqNames.first(), descriptors)
|
SingleImportVariant(samePackageFqNames.first(), descriptors)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +129,8 @@ class KotlinAddImportAction internal constructor(
|
|||||||
private val project: Project,
|
private val project: Project,
|
||||||
private val editor: Editor,
|
private val editor: Editor,
|
||||||
private val element: KtElement,
|
private val element: KtElement,
|
||||||
private val variants: List<AutoImportVariant>) : QuestionAction {
|
private val variants: List<AutoImportVariant>
|
||||||
|
) : QuestionAction {
|
||||||
fun showHint(): Boolean {
|
fun showHint(): Boolean {
|
||||||
if (variants.isEmpty()) return false
|
if (variants.isEmpty()) return false
|
||||||
|
|
||||||
@@ -212,7 +212,10 @@ class KotlinAddImportAction internal constructor(
|
|||||||
// insert partly qualified name
|
// insert partly qualified name
|
||||||
if (descriptor is ClassDescriptor || descriptor is PackageViewDescriptor) {
|
if (descriptor is ClassDescriptor || descriptor is PackageViewDescriptor) {
|
||||||
if (element is KtSimpleNameExpression) {
|
if (element is KtSimpleNameExpression) {
|
||||||
element.mainReference.bindToFqName(descriptor.importableFqName!!, KtSimpleNameReference.ShorteningMode.FORCED_SHORTENING)
|
element.mainReference.bindToFqName(
|
||||||
|
descriptor.importableFqName!!,
|
||||||
|
KtSimpleNameReference.ShorteningMode.FORCED_SHORTENING
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ImportInsertHelper.getInstance(project).importDescriptor(file, descriptor)
|
ImportInsertHelper.getInstance(project).importDescriptor(file, descriptor)
|
||||||
@@ -286,7 +289,8 @@ internal interface AutoImportVariant {
|
|||||||
DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptorsToImport.first())
|
DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptorsToImport.first())
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GroupedImportVariant(val autoImportDescription: String, val descriptors: Collection<DeclarationDescriptor>) : AutoImportVariant {
|
private class GroupedImportVariant(val autoImportDescription: String, val descriptors: Collection<DeclarationDescriptor>) :
|
||||||
|
AutoImportVariant {
|
||||||
override val excludeFqNameCheck: FqName = descriptors.first().importableFqName!!.parent()
|
override val excludeFqNameCheck: FqName = descriptors.first().importableFqName!!.parent()
|
||||||
override val descriptorsToImport: Collection<DeclarationDescriptor> get() = descriptors
|
override val descriptorsToImport: Collection<DeclarationDescriptor> get() = descriptors
|
||||||
override val hint: String get() = "$autoImportDescription from $excludeFqNameCheck"
|
override val hint: String get() = "$autoImportDescription from $excludeFqNameCheck"
|
||||||
@@ -296,7 +300,8 @@ private class SingleImportVariant(
|
|||||||
override val excludeFqNameCheck: FqName,
|
override val excludeFqNameCheck: FqName,
|
||||||
val descriptors: Collection<DeclarationDescriptor>
|
val descriptors: Collection<DeclarationDescriptor>
|
||||||
) : AutoImportVariant {
|
) : AutoImportVariant {
|
||||||
override val descriptorsToImport: Collection<DeclarationDescriptor> get() =
|
override val descriptorsToImport: Collection<DeclarationDescriptor>
|
||||||
|
get() =
|
||||||
listOf(descriptors.singleOrNull() ?: descriptors.sortedBy { if (it is ClassDescriptor) 0 else 1 }.first())
|
listOf(descriptors.singleOrNull() ?: descriptors.sortedBy { if (it is ClassDescriptor) 0 else 1 }.first())
|
||||||
|
|
||||||
override val hint: String get() = excludeFqNameCheck.asString()
|
override val hint: String get() = excludeFqNameCheck.asString()
|
||||||
|
|||||||
Reference in New Issue
Block a user