Create Class from Usage: Filter out usages in super type entry list

#KT-22918 Fixed
This commit is contained in:
Alexey Sedunov
2018-05-24 21:45:09 +03:00
parent 35517a8253
commit 610cc3867b
4 changed files with 33 additions and 11 deletions
@@ -46,12 +46,8 @@ import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.util.projectStructure.module
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
import org.jetbrains.kotlin.utils.SmartList
import java.util.*
@@ -117,17 +113,20 @@ open class CreateClassFromUsageFix<E : KtElement> protected constructor (
if (editor == null) return
val applicableParents = SmartList<PsiElement>().also { parents ->
if (classInfo.kind == ClassKind.INTERFACE)
classInfo.applicableParents.filterTo(parents) { it != element?.containingClass() }
else
parents += classInfo.applicableParents
classInfo.applicableParents.filterNotTo(parents) {
it is KtClassOrObject && it.superTypeListEntries.any {
when (it) {
is KtDelegatedSuperTypeEntry, is KtSuperTypeEntry -> it.typeAsUserType == element
is KtSuperTypeCallEntry -> it == element
else -> false
}
}
}
if (classInfo.kind != ClassKind.ENUM_ENTRY) {
parents += SeparateFileWrapper(PsiManager.getInstance(project))
}
}
if (ApplicationManager.getApplication().isUnitTestMode) {
val targetParent = applicableParents.firstOrNull {
it.allChildren.any { it is PsiComment && it.text == "// TARGET_PARENT:" }
@@ -0,0 +1,7 @@
// "Create class 'A'" "true"
package p
// TARGET_PARENT:
class Foo: <caret>A() {
}
@@ -0,0 +1,11 @@
// "Create class 'A'" "true"
package p
// TARGET_PARENT:
class Foo: A() {
}
<caret>open class A {
}
@@ -2286,6 +2286,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/createFromUsage/createClass/delegationSpecifier/classDelegatorToSuperclass.kt");
}
@TestMetadata("createSuperclassInsideSubclass.kt")
public void testCreateSuperclassInsideSubclass() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createClass/delegationSpecifier/createSuperclassInsideSubclass.kt");
}
@TestMetadata("delegatorToSupercallNoReceiver.kt")
public void testDelegatorToSupercallNoReceiver() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createClass/delegationSpecifier/delegatorToSupercallNoReceiver.kt");