Intention to replace add/addAll on a mutable collection with += (#1143)
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a <spot>+=</spot> 4
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a<spot>.add</spot>(4)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention replaces <code>add()</code> and <code>addAll()</code> calls in mutable collection with <code>+=</code>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1603,6 +1603,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ReplaceAddWithPlusAssignIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||
displayName="Object literal can be converted to lambda"
|
||||
groupPath="Kotlin"
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||
import org.jetbrains.kotlin.resolve.BindingContextUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getExplicitReceiverValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isSubclassOf
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class ReplaceAddWithPlusAssignIntention : SelfTargetingOffsetIndependentIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, "Replace with '+='") {
|
||||
override fun isApplicableTo(element: KtDotQualifiedExpression): Boolean {
|
||||
if (element.callExpression?.valueArguments?.size != 1) return false
|
||||
|
||||
when (element.calleeName) {
|
||||
"add" -> text = "Replace 'add()' with '+='"
|
||||
"addAll" -> text = "Replace 'addAll()' with '+='"
|
||||
else -> return false
|
||||
}
|
||||
|
||||
val context = element.analyze(BodyResolveMode.PARTIAL)
|
||||
BindingContextUtils.extractVariableDescriptorFromReference(context, element.receiverExpression)?.let {
|
||||
if (it.isVar) return false
|
||||
} ?: return false
|
||||
|
||||
val resolvedCall = element.getResolvedCall(context) ?: return false
|
||||
val receiverClass = DescriptorUtils.getClassDescriptorForType(resolvedCall.getExplicitReceiverValue()?.type ?: return false)
|
||||
return receiverClass.isSubclassOf(DefaultBuiltIns.Instance.mutableCollection)
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0 += $1", element.receiverExpression,
|
||||
element.callExpression?.valueArguments?.get(0)?.getArgumentExpression() ?: return))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ReplaceAddWithPlusAssignIntention
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace 'add()' with '+='
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>add(4)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace 'add()' with '+='
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a += 4
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace 'addAll()' with '+='
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>addAll(arrayOf(4, 5, 6))
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: Replace 'addAll()' with '+='
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a += arrayOf(4, 5, 6)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>addAll(1, arrayListOf(4))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
var a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>addAll(arrayListOf(4))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
val a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>add(1, 4)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
var a = arrayListOf<Int>(1, 2, 3)
|
||||
a.<caret>add(4)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class A {
|
||||
fun add(x: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
A().<caret>add(1)
|
||||
}
|
||||
@@ -13524,6 +13524,57 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceAddWithPlusAssign")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceAddWithPlusAssign extends AbstractIntentionTest {
|
||||
@TestMetadata("add.kt")
|
||||
public void testAdd() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/add.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addAll.kt")
|
||||
public void testAddAll() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/addAll.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addAllAtIndex.kt")
|
||||
public void testAddAllAtIndex() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/addAllAtIndex.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addAllToVar.kt")
|
||||
public void testAddAllToVar() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/addAllToVar.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addAtIndex.kt")
|
||||
public void testAddAtIndex() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/addAtIndex.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("addToVar.kt")
|
||||
public void testAddToVar() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/addToVar.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceAddWithPlusAssign() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceAddWithPlusAssign"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("nonCollection.kt")
|
||||
public void testNonCollection() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceAddWithPlusAssign/nonCollection.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceArrayEqualityOpWithArraysEquals")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user