From 7044e4675638b2e75e3fc214a9f6066566cc767f Mon Sep 17 00:00:00 2001 From: shiraji Date: Fri, 20 Oct 2017 15:49:33 +0300 Subject: [PATCH] Refactor common move member methods (cherry picked from commit 2dd8148) --- ...MoveMemberOutOfCompanionObjectIntention.kt | 67 ++++----------- .../MoveMemberOutOfObjectIntention.kt | 86 +++++++++++++++++++ 2 files changed, 103 insertions(+), 50 deletions(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberOutOfObjectIntention.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberOutOfCompanionObjectIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberOutOfCompanionObjectIntention.kt index dddba42a0fd..90b839e8871 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberOutOfCompanionObjectIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveMemberOutOfCompanionObjectIntention.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.idea.intentions -import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import com.intellij.psi.search.searches.ReferencesSearch @@ -25,15 +24,12 @@ import com.intellij.util.containers.MultiMap import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully -import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny +import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde -import org.jetbrains.kotlin.idea.refactoring.checkConflictsInteractively import org.jetbrains.kotlin.idea.refactoring.getUsageContext -import org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.* import org.jetbrains.kotlin.idea.runSynchronouslyWithProgress import org.jetbrains.kotlin.idea.util.application.runReadAction -import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject import org.jetbrains.kotlin.psi.psiUtil.parents @@ -42,47 +38,13 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isSubclassOf import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver import org.jetbrains.kotlin.util.findCallableMemberBySignature -class MoveMemberOutOfCompanionObjectIntention : SelfTargetingRangeIntention(KtNamedDeclaration::class.java, - "Move out of companion object") { - override fun startInWriteAction() = false - - override fun applicabilityRange(element: KtNamedDeclaration): TextRange? { - if (element !is KtNamedFunction && element !is KtProperty && element !is KtClassOrObject) return null - val container = element.containingClassOrObject - if (!(container is KtObjectDeclaration && container.isCompanion())) return null - if (container.containingClassOrObject == null) return null - return element.nameIdentifier?.textRange - } - - override fun applyTo(element: KtNamedDeclaration, editor: Editor?) { - val project = element.project - - val companionObject = element.containingClassOrObject!! - val targetClass = companionObject.containingClassOrObject!! - - fun deleteCompanionIfEmpty() { - if (companionObject.declarations.isEmpty()) { - companionObject.delete() - } - } - - if (element is KtClassOrObject) { - val moveDescriptor = MoveDeclarationsDescriptor(project, - listOf(element), - KotlinMoveTargetForExistingElement(targetClass), - MoveDeclarationsDelegate.NestedClass()) - runWriteAction { - MoveKotlinDeclarationsProcessor(moveDescriptor).run() - deleteCompanionIfEmpty() - } - return - } +class MoveMemberOutOfCompanionObjectIntention : MoveMemberOutOfObjectIntention("Move out of companion object") { + override fun addConflicts(element: KtNamedDeclaration, conflicts: MultiMap) { + val targetClass = element.containingClassOrObject!!.containingClassOrObject!! val targetClassDescriptor = runReadAction { targetClass.unsafeResolveToDescriptor() as ClassDescriptor } - val conflicts = MultiMap() - - val refsRequiringClassInstance = project.runSynchronouslyWithProgress("Searching for ${element.name}", true) { + val refsRequiringClassInstance = element.project.runSynchronouslyWithProgress("Searching for ${element.name}", true) { runReadAction { ReferencesSearch .search(element) @@ -113,17 +75,22 @@ class MoveMemberOutOfCompanionObjectIntention : SelfTargetingRangeIntention(KtNamedDeclaration::class.java, text) { + override fun startInWriteAction() = false + + abstract fun getDestination(element: KtNamedDeclaration) : KtElement + + abstract fun addConflicts(element: KtNamedDeclaration,conflicts: MultiMap) + + override fun applyTo(element: KtNamedDeclaration, editor: Editor?) { + val project = element.project + + val classOrObject = element.containingClassOrObject!! + val destination = getDestination(element) + + fun deleteClassOrObjectIfEmpty() { + if (classOrObject.declarations.isEmpty()) { + classOrObject.delete() + } + } + + if (element is KtClassOrObject) { + val moveDescriptor = MoveDeclarationsDescriptor(project, + listOf(element), + KotlinMoveTargetForExistingElement(destination), + MoveDeclarationsDelegate.NestedClass()) + runWriteAction { + MoveKotlinDeclarationsProcessor(moveDescriptor).run() + deleteClassOrObjectIfEmpty() + } + return + } + + val conflicts = MultiMap().apply { addConflicts(element, this) } + + project.checkConflictsInteractively(conflicts) { + runWriteAction { + Mover.Default(element, destination) + deleteClassOrObjectIfEmpty() + } + } + } +} \ No newline at end of file