Minor: refactoring KotlinAddImportAction
This commit is contained in:
@@ -54,10 +54,10 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
|
||||
internal fun createSingleImportAction(
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
fqNames: Collection<FqName>
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
fqNames: Collection<FqName>
|
||||
): KotlinAddImportAction {
|
||||
val file = element.containingKtFile
|
||||
val prioritizer = Prioritizer(element.containingKtFile)
|
||||
@@ -71,17 +71,17 @@ internal fun createSingleImportAction(
|
||||
}
|
||||
|
||||
internal fun createSingleImportActionForConstructor(
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
fqNames: Collection<FqName>
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
fqNames: Collection<FqName>
|
||||
): KotlinAddImportAction {
|
||||
val file = element.containingKtFile
|
||||
val prioritizer = Prioritizer(element.containingKtFile)
|
||||
val variants = fqNames.mapNotNull { fqName ->
|
||||
val sameFqNameDescriptors = file.resolveImportReference(fqName.parent())
|
||||
.filterIsInstance<ClassDescriptor>()
|
||||
.flatMap { it.constructors }
|
||||
.filterIsInstance<ClassDescriptor>()
|
||||
.flatMap { it.constructors }
|
||||
|
||||
val priority = sameFqNameDescriptors.asSequence().map { prioritizer.priority(it) }.min() ?: return@mapNotNull null
|
||||
Prioritizer.VariantWithPriority(SingleImportVariant(fqName, sameFqNameDescriptors), priority)
|
||||
@@ -90,34 +90,33 @@ internal fun createSingleImportActionForConstructor(
|
||||
}
|
||||
|
||||
internal fun createGroupedImportsAction(
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
autoImportDescription: String,
|
||||
fqNames: Collection<FqName>
|
||||
project: Project,
|
||||
editor: Editor,
|
||||
element: KtElement,
|
||||
autoImportDescription: String,
|
||||
fqNames: Collection<FqName>
|
||||
): KotlinAddImportAction {
|
||||
val prioritizer = DescriptorGroupPrioritizer(element.containingKtFile)
|
||||
|
||||
val file = element.containingKtFile
|
||||
val variants = fqNames
|
||||
.groupBy { it.parentOrNull() ?: FqName.ROOT }
|
||||
.map {
|
||||
val samePackageFqNames = it.value
|
||||
val descriptors = samePackageFqNames.flatMap { file.resolveImportReference(it) }
|
||||
val variant = if (samePackageFqNames.size > 1) {
|
||||
GroupedImportVariant(autoImportDescription, descriptors)
|
||||
}
|
||||
else {
|
||||
SingleImportVariant(samePackageFqNames.first(), descriptors)
|
||||
}
|
||||
.groupBy { it.parentOrNull() ?: FqName.ROOT }
|
||||
.map {
|
||||
val samePackageFqNames = it.value
|
||||
val descriptors = samePackageFqNames.flatMap { fqName -> file.resolveImportReference(fqName) }
|
||||
val variant = if (samePackageFqNames.size > 1) {
|
||||
GroupedImportVariant(autoImportDescription, descriptors)
|
||||
} else {
|
||||
SingleImportVariant(samePackageFqNames.first(), descriptors)
|
||||
}
|
||||
|
||||
val priority = prioritizer.priority(descriptors)
|
||||
DescriptorGroupPrioritizer.VariantWithPriority(variant, priority)
|
||||
}
|
||||
.sortedBy {
|
||||
it.priority
|
||||
}
|
||||
.map { it.variant }
|
||||
val priority = prioritizer.priority(descriptors)
|
||||
DescriptorGroupPrioritizer.VariantWithPriority(variant, priority)
|
||||
}
|
||||
.sortedBy {
|
||||
it.priority
|
||||
}
|
||||
.map { it.variant }
|
||||
|
||||
return KotlinAddImportAction(project, editor, element, variants)
|
||||
}
|
||||
@@ -127,10 +126,11 @@ internal fun createGroupedImportsAction(
|
||||
* Based on {@link AddImportAction}
|
||||
*/
|
||||
class KotlinAddImportAction internal constructor(
|
||||
private val project: Project,
|
||||
private val editor: Editor,
|
||||
private val element: KtElement,
|
||||
private val variants: List<AutoImportVariant>) : QuestionAction {
|
||||
private val project: Project,
|
||||
private val editor: Editor,
|
||||
private val element: KtElement,
|
||||
private val variants: List<AutoImportVariant>
|
||||
) : QuestionAction {
|
||||
fun showHint(): Boolean {
|
||||
if (variants.isEmpty()) return false
|
||||
|
||||
@@ -212,7 +212,10 @@ class KotlinAddImportAction internal constructor(
|
||||
// insert partly qualified name
|
||||
if (descriptor is ClassDescriptor || descriptor is PackageViewDescriptor) {
|
||||
if (element is KtSimpleNameExpression) {
|
||||
element.mainReference.bindToFqName(descriptor.importableFqName!!, KtSimpleNameReference.ShorteningMode.FORCED_SHORTENING)
|
||||
element.mainReference.bindToFqName(
|
||||
descriptor.importableFqName!!,
|
||||
KtSimpleNameReference.ShorteningMode.FORCED_SHORTENING
|
||||
)
|
||||
}
|
||||
} else {
|
||||
ImportInsertHelper.getInstance(project).importDescriptor(file, descriptor)
|
||||
@@ -283,20 +286,22 @@ internal interface AutoImportVariant {
|
||||
fun icon(project: Project) = KotlinDescriptorIconProvider.getIcon(descriptorsToImport.first(), declarationToImport(project), 0)
|
||||
|
||||
fun declarationToImport(project: Project): PsiElement? =
|
||||
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 descriptorsToImport: Collection<DeclarationDescriptor> get() = descriptors
|
||||
override val hint: String get() = "$autoImportDescription from $excludeFqNameCheck"
|
||||
}
|
||||
|
||||
private class SingleImportVariant(
|
||||
override val excludeFqNameCheck: FqName,
|
||||
val descriptors: Collection<DeclarationDescriptor>
|
||||
override val excludeFqNameCheck: FqName,
|
||||
val descriptors: Collection<DeclarationDescriptor>
|
||||
) : 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())
|
||||
|
||||
override val hint: String get() = excludeFqNameCheck.asString()
|
||||
|
||||
Reference in New Issue
Block a user