Skip to content

Commit ea6e3c8

Browse files
committed
Join declaration & assignment: treat assignment with this. correctly #KT-16000 Fixed
1 parent 84f324e commit ea6e3c8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte
104104
val assignments = mutableListOf<KtBinaryExpression>()
105105
fun process(binaryExpr: KtBinaryExpression) {
106106
if (binaryExpr.operationToken != KtTokens.EQ) return
107-
val leftReference = binaryExpr.left as? KtNameReferenceExpression ?: return
107+
val left = binaryExpr.left
108+
val leftReference = when (left) {
109+
is KtNameReferenceExpression ->
110+
left
111+
is KtDotQualifiedExpression ->
112+
if (left.receiverExpression is KtThisExpression) left.selectorExpression as? KtNameReferenceExpression else null
113+
else ->
114+
null
115+
} ?: return
108116
if (leftReference.getReferencedName() != property.name) return
109117
assignments += binaryExpr
110118
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// IS_APPLICABLE: false
2+
3+
class My {
4+
val x: Int<caret>
5+
6+
constructor(x: Int) {
7+
this.x = x
8+
}
9+
10+
constructor() {
11+
x = 42
12+
}
13+
}

idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8916,6 +8916,12 @@ public void testMultipleConstructors() throws Exception {
89168916
doTest(fileName);
89178917
}
89188918

8919+
@TestMetadata("multipleConstructorsWithThis.kt")
8920+
public void testMultipleConstructorsWithThis() throws Exception {
8921+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt");
8922+
doTest(fileName);
8923+
}
8924+
89198925
@TestMetadata("propertyReassignment.kt")
89208926
public void testPropertyReassignment() throws Exception {
89218927
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/propertyReassignment.kt");

0 commit comments

Comments
 (0)