Move out of Companion Object: Warn if traget class already contains function with the same signature
#KT-13877 Fixed
This commit is contained in:
+14
@@ -20,7 +20,12 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import com.intellij.refactoring.util.RefactoringUIUtil
|
||||
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.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.idea.refactoring.checkConflictsInteractively
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.*
|
||||
import org.jetbrains.kotlin.idea.refactoring.runSynchronouslyWithProgress
|
||||
@@ -28,6 +33,7 @@ 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.isAncestor
|
||||
import org.jetbrains.kotlin.util.findCallableMemberBySignature
|
||||
|
||||
class MoveMemberOutOfCompanionObjectIntention : SelfTargetingRangeIntention<KtNamedDeclaration>(KtNamedDeclaration::class.java,
|
||||
"Move out of companion object") {
|
||||
@@ -73,6 +79,14 @@ class MoveMemberOutOfCompanionObjectIntention : SelfTargetingRangeIntention<KtNa
|
||||
conflicts.putValue(refElement, "Class instance required: ${refElement.text}")
|
||||
}
|
||||
|
||||
val targetClassDescriptor = targetClass.resolveToDescriptor() as ClassDescriptor
|
||||
val callableDescriptor = element.resolveToDescriptor() as CallableMemberDescriptor
|
||||
targetClassDescriptor.findCallableMemberBySignature(callableDescriptor)?.let {
|
||||
DescriptorToSourceUtilsIde.getAnyDeclaration(project, it)
|
||||
}?.let {
|
||||
conflicts.putValue(it, "Class '${targetClass.name}' already contains ${RefactoringUIUtil.getDescription(it, false)}")
|
||||
}
|
||||
|
||||
project.checkConflictsInteractively(conflicts) {
|
||||
runWriteAction {
|
||||
Mover.Default(element, targetClass)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// SHOULD_FAIL_WITH: Class 'Test' already contains function f1(Int)
|
||||
class Test {
|
||||
fun f1(n: Int){}
|
||||
companion object{
|
||||
fun <caret>f1(n: Int){}
|
||||
}
|
||||
}
|
||||
@@ -9306,6 +9306,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/moveOutOfCompanion/notCompanion2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("redeclarationConflict.kt")
|
||||
public void testRedeclarationConflict() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/moveOutOfCompanion/redeclarationConflict.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/moveToCompanion")
|
||||
|
||||
Reference in New Issue
Block a user