arrayListOf, mutableListOf, hashSetOf and mutableSetOf supported
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions.loopToCallChain
|
|||||||
import com.intellij.openapi.util.Key
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.openapi.util.UserDataHolderBase
|
import com.intellij.openapi.util.UserDataHolderBase
|
||||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
import org.jetbrains.kotlin.diagnostics.Severity
|
import org.jetbrains.kotlin.diagnostics.Severity
|
||||||
import org.jetbrains.kotlin.idea.analysis.analyzeInContext
|
import org.jetbrains.kotlin.idea.analysis.analyzeInContext
|
||||||
@@ -190,18 +191,34 @@ enum class CollectionKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun KtExpression.isSimpleCollectionInstantiation(): CollectionKind? {
|
fun KtExpression.isSimpleCollectionInstantiation(): CollectionKind? {
|
||||||
//TODO: support mutableListOf() etc
|
|
||||||
val callExpression = this as? KtCallExpression ?: return null //TODO: it can be qualified too
|
val callExpression = this as? KtCallExpression ?: return null //TODO: it can be qualified too
|
||||||
if (callExpression.valueArguments.isNotEmpty()) return null
|
if (callExpression.valueArguments.isNotEmpty()) return null
|
||||||
|
|
||||||
val bindingContext = callExpression.analyze(BodyResolveMode.PARTIAL)
|
val bindingContext = callExpression.analyze(BodyResolveMode.PARTIAL)
|
||||||
val resolvedCall = callExpression.getResolvedCall(bindingContext) ?: return null
|
val resolvedCall = callExpression.getResolvedCall(bindingContext) ?: return null
|
||||||
val constructorDescriptor = resolvedCall.resultingDescriptor as? ConstructorDescriptor ?: return null
|
val descriptor = resolvedCall.resultingDescriptor
|
||||||
val classDescriptor = constructorDescriptor.containingDeclaration
|
|
||||||
val classFqName = classDescriptor.importableFqName?.asString()
|
when (descriptor) {
|
||||||
return when (classFqName) {
|
is ConstructorDescriptor -> {
|
||||||
"java.util.ArrayList" -> CollectionKind.LIST
|
val classDescriptor = descriptor.containingDeclaration
|
||||||
"java.util.HashSet", "java.util.LinkedHashSet" -> CollectionKind.SET
|
val classFqName = classDescriptor.importableFqName?.asString()
|
||||||
else -> null
|
return when (classFqName) {
|
||||||
|
"java.util.ArrayList" -> CollectionKind.LIST
|
||||||
|
"java.util.HashSet", "java.util.LinkedHashSet" -> CollectionKind.SET
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is FunctionDescriptor -> {
|
||||||
|
val fqName = descriptor.importableFqName?.asString()
|
||||||
|
return when (fqName) {
|
||||||
|
"kotlin.collections.arrayListOf", "kotlin.collections.mutableListOf" -> CollectionKind.LIST
|
||||||
|
"kotlin.collections.hashSetOf", "kotlin.collections.mutableSetOf" -> CollectionKind.SET
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toList()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): List<String> {
|
||||||
|
val result = arrayListOf<String>()
|
||||||
|
<caret>for (s in map.values) {
|
||||||
|
result.add(s)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toList()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): List<String> {
|
||||||
|
val <caret>result = map.values.toList()
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toList()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): List<String> {
|
||||||
|
val result = mutableListOf<String>()
|
||||||
|
<caret>for (s in map.values) {
|
||||||
|
result.add(s)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toList()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): List<String> {
|
||||||
|
val <caret>result = map.values.toList()
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toMutableSet()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): MutableCollection<String> {
|
||||||
|
val result = hashSetOf<String>()
|
||||||
|
<caret>for (s in map.values) {
|
||||||
|
result.add(s)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toMutableSet()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): MutableCollection<String> {
|
||||||
|
val <caret>result = map.values.toMutableSet()
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toMutableSet()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): MutableCollection<String> {
|
||||||
|
val result = mutableSetOf<String>()
|
||||||
|
<caret>for (s in map.values) {
|
||||||
|
result.add(s)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'toMutableSet()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(map: Map<Int, String>): MutableCollection<String> {
|
||||||
|
val <caret>result = map.values.toMutableSet()
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -1055,12 +1055,36 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toList2.kt")
|
||||||
|
public void testToList2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toList2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toList3.kt")
|
||||||
|
public void testToList3() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toList3.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("toMutableSet.kt")
|
@TestMetadata("toMutableSet.kt")
|
||||||
public void testToMutableSet() throws Exception {
|
public void testToMutableSet() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toMutableSet2.kt")
|
||||||
|
public void testToMutableSet2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toMutableSet3.kt")
|
||||||
|
public void testToMutableSet3() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet3.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("toSet.kt")
|
@TestMetadata("toSet.kt")
|
||||||
public void testToSet() throws Exception {
|
public void testToSet() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toSet.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toSet.kt");
|
||||||
|
|||||||
@@ -8173,12 +8173,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toList2.kt")
|
||||||
|
public void testToList2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toList2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toList3.kt")
|
||||||
|
public void testToList3() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toList3.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("toMutableSet.kt")
|
@TestMetadata("toMutableSet.kt")
|
||||||
public void testToMutableSet() throws Exception {
|
public void testToMutableSet() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toMutableSet2.kt")
|
||||||
|
public void testToMutableSet2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toMutableSet3.kt")
|
||||||
|
public void testToMutableSet3() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toMutableSet3.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("toSet.kt")
|
@TestMetadata("toSet.kt")
|
||||||
public void testToSet() throws Exception {
|
public void testToSet() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toSet.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toSet.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user