Replace with ordinary assignment: do not suggest when operator is augmented assignment operator function
#KT-34715 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
55d55446c8
commit
73e319ca7a
+8
-6
@@ -8,21 +8,23 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
class ReplaceWithOrdinaryAssignmentIntention : SelfTargetingIntention<KtBinaryExpression>(
|
||||
KtBinaryExpression::class.java,
|
||||
KotlinBundle.lazyMessage("replace.with.ordinary.assignment")
|
||||
), LowPriorityAction {
|
||||
override fun isApplicableTo(element: KtBinaryExpression, caretOffset: Int): Boolean {
|
||||
val operationReference = element.operationReference
|
||||
if (!operationReference.textRange.containsOffset(caretOffset)) return false
|
||||
if (element.operationToken !in KtTokens.AUGMENTED_ASSIGNMENTS) return false
|
||||
if (element.left !is KtNameReferenceExpression) return false
|
||||
val left = element.left ?: return false
|
||||
if ((left.safeAs<KtQualifiedExpression>()?.selectorExpression ?: left) !is KtNameReferenceExpression) return false
|
||||
if (element.right == null) return false
|
||||
return element.operationReference.textRange.containsOffset(caretOffset)
|
||||
return operationReference.mainReference.resolve().safeAs<KtFunction>()?.name?.endsWith("Assign") != true
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
operator fun Int.minusAssign(element: Int) {}
|
||||
|
||||
fun test() {
|
||||
val x = 1
|
||||
x <caret>-= 9
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val mutableList = mutableListOf(4, 5, 6, 7, 8)
|
||||
mutableList <caret>+= 9
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
operator fun Int.plusAssign(element: Int) {}
|
||||
|
||||
fun test() {
|
||||
val x = 1
|
||||
x <caret>+= 9
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
var x: Int = 1
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val foo = Foo()
|
||||
foo.x <caret>+= 1
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
var x: Int = 1
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val foo = Foo()
|
||||
foo.x = foo.x + 1
|
||||
}
|
||||
@@ -18,6 +18,7 @@ org.jetbrains.kotlin.idea.intentions.ConvertPropertyInitializerToGetterIntention
|
||||
org.jetbrains.kotlin.idea.intentions.CreateKotlinSubClassIntention
|
||||
org.jetbrains.kotlin.idea.intentions.MoveMemberToCompanionObjectIntention
|
||||
org.jetbrains.kotlin.idea.intentions.MovePropertyToConstructorIntention
|
||||
org.jetbrains.kotlin.idea.intentions.ReplaceWithOrdinaryAssignmentIntention
|
||||
org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
|
||||
org.jetbrains.kotlin.idea.quickfix.AddAnnotationTargetFix
|
||||
org.jetbrains.kotlin.idea.quickfix.AddExclExclCallFix
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// "Change 'list' to val" "false"
|
||||
// ACTION: Replace overloaded operator with function call
|
||||
// ACTION: Replace with 'plusAssign()' call
|
||||
// ACTION: Replace with ordinary assignment
|
||||
// ERROR: Assignment operators ambiguity: <br>public operator fun <T> Collection<Int>.plus(element: Int): List<Int> defined in kotlin.collections<br>public inline operator fun <T> MutableCollection<in Int>.plusAssign(element: Int): Unit defined in kotlin.collections
|
||||
// WITH_RUNTIME
|
||||
|
||||
|
||||
@@ -15607,6 +15607,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/complexRightExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("minusAssignOperatorFun.kt")
|
||||
public void testMinusAssignOperatorFun() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/minusAssignOperatorFun.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("mutableList.kt")
|
||||
public void testMutableList() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/mutableList.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonAssignmentExpression.kt")
|
||||
public void testNonAssignmentExpression() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/nonAssignmentExpression.kt");
|
||||
@@ -15617,6 +15627,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/nonAugmentedAssign.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("plusAssignOperatorFun.kt")
|
||||
public void testPlusAssignOperatorFun() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/plusAssignOperatorFun.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("qualifiedExpression.kt")
|
||||
public void testQualifiedExpression() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/qualifiedExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceWithOrdinaryAssignment/simple.kt");
|
||||
|
||||
Reference in New Issue
Block a user