Fix KT-13962 with kotlin.collections constructors

This commit is contained in:
Dereck Bridie
2019-04-05 20:29:54 +02:00
committed by Mikhail Glukhikh
parent 10e3dfa980
commit 7da52b2df5
4 changed files with 21 additions and 6 deletions
@@ -9,6 +9,7 @@ import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertCollectionConstructorToFunction : SelfTargetingIntention<KtCallExpression>(
@@ -19,13 +20,13 @@ class ConvertCollectionConstructorToFunction : SelfTargetingIntention<KtCallExpr
"java.util.ArrayList.<init>" to "arrayListOf",
"kotlin.collections.ArrayList.<init>" to "arrayListOf",
"java.util.HashMap.<init>" to "hashMapOf",
"kotlin.collections.HashMap.<init>" to "arrayListOf",
"kotlin.collections.HashMap.<init>" to "hashMapOf",
"java.util.HashSet.<init>" to "hashSetOf",
"kotlin.collections.HashSet.<init>" to "arrayListOf",
"kotlin.collections.HashSet.<init>" to "hashSetOf",
"java.util.LinkedHashMap.<init>" to "linkedMapOf",
"kotlin.collections.LinkedHashMap.<init>" to "arrayListOf",
"kotlin.collections.LinkedHashMap.<init>" to "linkedMapOf",
"java.util.LinkedHashSet.<init>" to "linkedSetOf",
"kotlin.collections.LinkedHashSet.<init>" to "arrayListOf"
"kotlin.collections.LinkedHashSet.<init>" to "linkedSetOf"
)
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
@@ -36,7 +37,8 @@ class ConvertCollectionConstructorToFunction : SelfTargetingIntention<KtCallExpr
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val fq = element.resolveToCall()?.resultingDescriptor?.fqNameSafe?.asString() ?: return
val toCall = functionMap[fq] ?: return
val convertedCall = KtPsiFactory(element).createIdentifier(toCall)
element.calleeExpression?.replace(convertedCall)
val callee = element.calleeExpression ?: return
callee.replace(KtPsiFactory(element).createIdentifier(toCall))
element.getQualifiedExpressionForSelector()?.replace(element)
}
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list = java.util.<caret>ArrayList<Int>()
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo() {
var list = <caret>arrayListOf<Int>()
}
@@ -4935,6 +4935,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceArrayListCall.kt");
}
@TestMetadata("replaceArrayListCallQualified.kt")
public void testReplaceArrayListCallQualified() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceArrayListCallQualified.kt");
}
@TestMetadata("replaceArrayListCallWithType.kt")
public void testReplaceArrayListCallWithType() throws Exception {
runTest("idea/testData/intentions/convertCollectionConstructorToFunction/replaceArrayListCallWithType.kt");