Minor: refactoring KotlinAddImportAction
This commit is contained in:
@@ -54,10 +54,10 @@ import org.jetbrains.kotlin.psi.KtFile
|
|||||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||||
|
|
||||||
internal fun createSingleImportAction(
|
internal fun createSingleImportAction(
|
||||||
project: Project,
|
project: Project,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
element: KtElement,
|
element: KtElement,
|
||||||
fqNames: Collection<FqName>
|
fqNames: Collection<FqName>
|
||||||
): KotlinAddImportAction {
|
): KotlinAddImportAction {
|
||||||
val file = element.containingKtFile
|
val file = element.containingKtFile
|
||||||
val prioritizer = Prioritizer(element.containingKtFile)
|
val prioritizer = Prioritizer(element.containingKtFile)
|
||||||
@@ -71,17 +71,17 @@ internal fun createSingleImportAction(
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun createSingleImportActionForConstructor(
|
internal fun createSingleImportActionForConstructor(
|
||||||
project: Project,
|
project: Project,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
element: KtElement,
|
element: KtElement,
|
||||||
fqNames: Collection<FqName>
|
fqNames: Collection<FqName>
|
||||||
): KotlinAddImportAction {
|
): KotlinAddImportAction {
|
||||||
val file = element.containingKtFile
|
val file = element.containingKtFile
|
||||||
val prioritizer = Prioritizer(element.containingKtFile)
|
val prioritizer = Prioritizer(element.containingKtFile)
|
||||||
val variants = fqNames.mapNotNull { fqName ->
|
val variants = fqNames.mapNotNull { fqName ->
|
||||||
val sameFqNameDescriptors = file.resolveImportReference(fqName.parent())
|
val sameFqNameDescriptors = file.resolveImportReference(fqName.parent())
|
||||||
.filterIsInstance<ClassDescriptor>()
|
.filterIsInstance<ClassDescriptor>()
|
||||||
.flatMap { it.constructors }
|
.flatMap { it.constructors }
|
||||||
|
|
||||||
val priority = sameFqNameDescriptors.asSequence().map { prioritizer.priority(it) }.min() ?: return@mapNotNull null
|
val priority = sameFqNameDescriptors.asSequence().map { prioritizer.priority(it) }.min() ?: return@mapNotNull null
|
||||||
Prioritizer.VariantWithPriority(SingleImportVariant(fqName, sameFqNameDescriptors), priority)
|
Prioritizer.VariantWithPriority(SingleImportVariant(fqName, sameFqNameDescriptors), priority)
|
||||||
@@ -90,34 +90,33 @@ internal fun createSingleImportActionForConstructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun createGroupedImportsAction(
|
internal fun createGroupedImportsAction(
|
||||||
project: Project,
|
project: Project,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
element: KtElement,
|
element: KtElement,
|
||||||
autoImportDescription: String,
|
autoImportDescription: String,
|
||||||
fqNames: Collection<FqName>
|
fqNames: Collection<FqName>
|
||||||
): KotlinAddImportAction {
|
): KotlinAddImportAction {
|
||||||
val prioritizer = DescriptorGroupPrioritizer(element.containingKtFile)
|
val prioritizer = DescriptorGroupPrioritizer(element.containingKtFile)
|
||||||
|
|
||||||
val file = element.containingKtFile
|
val file = element.containingKtFile
|
||||||
val variants = fqNames
|
val variants = fqNames
|
||||||
.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)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val priority = prioritizer.priority(descriptors)
|
val priority = prioritizer.priority(descriptors)
|
||||||
DescriptorGroupPrioritizer.VariantWithPriority(variant, priority)
|
DescriptorGroupPrioritizer.VariantWithPriority(variant, priority)
|
||||||
}
|
}
|
||||||
.sortedBy {
|
.sortedBy {
|
||||||
it.priority
|
it.priority
|
||||||
}
|
}
|
||||||
.map { it.variant }
|
.map { it.variant }
|
||||||
|
|
||||||
return KotlinAddImportAction(project, editor, element, variants)
|
return KotlinAddImportAction(project, editor, element, variants)
|
||||||
}
|
}
|
||||||
@@ -127,10 +126,11 @@ internal fun createGroupedImportsAction(
|
|||||||
* Based on {@link AddImportAction}
|
* Based on {@link AddImportAction}
|
||||||
*/
|
*/
|
||||||
class KotlinAddImportAction internal constructor(
|
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)
|
||||||
@@ -283,20 +286,22 @@ internal interface AutoImportVariant {
|
|||||||
fun icon(project: Project) = KotlinDescriptorIconProvider.getIcon(descriptorsToImport.first(), declarationToImport(project), 0)
|
fun icon(project: Project) = KotlinDescriptorIconProvider.getIcon(descriptorsToImport.first(), declarationToImport(project), 0)
|
||||||
|
|
||||||
fun declarationToImport(project: Project): PsiElement? =
|
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 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"
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SingleImportVariant(
|
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