Introduce Parameter: Do not suggest containers of object and non-inner classes
This commit is contained in:
+20
-1
@@ -135,6 +135,9 @@ fun IntroduceParameterDescriptor.performRefactoring() {
|
|||||||
public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase() {
|
public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase() {
|
||||||
open fun configure(descriptor: IntroduceParameterDescriptor): IntroduceParameterDescriptor = descriptor
|
open fun configure(descriptor: IntroduceParameterDescriptor): IntroduceParameterDescriptor = descriptor
|
||||||
|
|
||||||
|
private fun isObjectOrNonInnerClass(e: PsiElement): Boolean =
|
||||||
|
e is JetObjectDeclaration || (e is JetClass && !e.isInner())
|
||||||
|
|
||||||
fun invoke(project: Project, editor: Editor, expression: JetExpression, targetParent: JetNamedDeclaration) {
|
fun invoke(project: Project, editor: Editor, expression: JetExpression, targetParent: JetNamedDeclaration) {
|
||||||
val psiFactory = JetPsiFactory(project)
|
val psiFactory = JetPsiFactory(project)
|
||||||
|
|
||||||
@@ -170,8 +173,19 @@ public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase()
|
|||||||
.filter { it.second.isNotEmpty() }
|
.filter { it.second.isNotEmpty() }
|
||||||
.toMap()
|
.toMap()
|
||||||
|
|
||||||
|
val forbiddenRanges =
|
||||||
|
if (targetParent is JetClass) {
|
||||||
|
targetParent.getDeclarations().filter { isObjectOrNonInnerClass(it) }.map { it.getTextRange() }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Collections.emptyList()
|
||||||
|
}
|
||||||
val occurrencesToReplace = expression.toRange()
|
val occurrencesToReplace = expression.toRange()
|
||||||
.match(body, JetPsiUnifier.DEFAULT)
|
.match(body, JetPsiUnifier.DEFAULT)
|
||||||
|
.filterNot {
|
||||||
|
val textRange = it.range.getTextRange()
|
||||||
|
forbiddenRanges.any { it.intersects(textRange) }
|
||||||
|
}
|
||||||
.map {
|
.map {
|
||||||
val matchedElement = it.range.elements.singleOrNull()
|
val matchedElement = it.range.elements.singleOrNull()
|
||||||
when (matchedElement) {
|
when (matchedElement) {
|
||||||
@@ -241,7 +255,12 @@ public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase()
|
|||||||
editor = editor,
|
editor = editor,
|
||||||
file = file,
|
file = file,
|
||||||
getContainers = { elements, parent ->
|
getContainers = { elements, parent ->
|
||||||
parent.parents(withItself = false)
|
val parents = parent.parents(withItself = false)
|
||||||
|
val stopAt = (parent.parents(withItself = false) zip parent.parents(withItself = false).drop(1))
|
||||||
|
.firstOrNull { isObjectOrNonInnerClass(it.first) }
|
||||||
|
?.second
|
||||||
|
|
||||||
|
(if (stopAt != null) parent.parents(withItself = false).takeWhile { it != stopAt } else parents)
|
||||||
.filter {
|
.filter {
|
||||||
((it is JetClass && !it.isTrait() && it !is JetEnumEntry) || it is JetNamedFunction || it is JetSecondaryConstructor) &&
|
((it is JetClass && !it.isTrait() && it !is JetEnumEntry) || it is JetNamedFunction || it is JetSecondaryConstructor) &&
|
||||||
((it as JetNamedDeclaration).getValueParameterList() != null || it.getNameIdentifier() != null)
|
((it as JetNamedDeclaration).getValueParameterList() != null || it.getNameIdentifier() != null)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// TARGET:
|
||||||
|
class A {
|
||||||
|
inner class B {
|
||||||
|
fun foo() = <selection>1</selection>
|
||||||
|
}
|
||||||
|
|
||||||
|
object O {
|
||||||
|
fun foo() = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// TARGET:
|
||||||
|
class A(val i: Int = 1) {
|
||||||
|
inner class B {
|
||||||
|
fun foo() = i
|
||||||
|
}
|
||||||
|
|
||||||
|
object O {
|
||||||
|
fun foo() = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -2269,6 +2269,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doIntroduceParameterTest(fileName);
|
doIntroduceParameterTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("forbiddenUsages.kt")
|
||||||
|
public void testForbiddenUsages() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/forbiddenUsages.kt");
|
||||||
|
doIntroduceParameterTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("functionMultipleUnusedParameters.kt")
|
@TestMetadata("functionMultipleUnusedParameters.kt")
|
||||||
public void testFunctionMultipleUnusedParameters() throws Exception {
|
public void testFunctionMultipleUnusedParameters() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/functionMultipleUnusedParameters.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/functionMultipleUnusedParameters.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user