From ea6e3c828d694a6db3631576e4227ee29939727a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 1 Feb 2017 18:07:43 +0300 Subject: [PATCH] Join declaration & assignment: treat assignment with `this.` correctly #KT-16000 Fixed --- .../JoinDeclarationAndAssignmentIntention.kt | 10 +++++++++- .../multipleConstructorsWithThis.kt | 13 +++++++++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt index 1032778c014..26340633f00 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt @@ -104,7 +104,15 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte val assignments = mutableListOf() fun process(binaryExpr: KtBinaryExpression) { if (binaryExpr.operationToken != KtTokens.EQ) return - val leftReference = binaryExpr.left as? KtNameReferenceExpression ?: return + val left = binaryExpr.left + val leftReference = when (left) { + is KtNameReferenceExpression -> + left + is KtDotQualifiedExpression -> + if (left.receiverExpression is KtThisExpression) left.selectorExpression as? KtNameReferenceExpression else null + else -> + null + } ?: return if (leftReference.getReferencedName() != property.name) return assignments += binaryExpr } diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt b/idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt new file mode 100644 index 00000000000..6dbbe878d1c --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false + +class My { + val x: Int + + constructor(x: Int) { + this.x = x + } + + constructor() { + x = 42 + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 0d48aa18e32..a74901f0632 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8916,6 +8916,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("multipleConstructorsWithThis.kt") + public void testMultipleConstructorsWithThis() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt"); + doTest(fileName); + } + @TestMetadata("propertyReassignment.kt") public void testPropertyReassignment() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/propertyReassignment.kt");