Add intention to replace "Map.getOrDefault"
#KT-21503 Fixed
This commit is contained in:
committed by
Ilya Kirillov
parent
e09ae645b3
commit
f5c0a30c51
@@ -1654,6 +1654,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ReplaceMapGetOrDefaultIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.RemoveUnderscoresFromNumericLiteralIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun test(map: Map<Int, String>) {
|
||||
map<spot>[1] ?: "foo"</spot>
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test(map: Map<Int, String>) {
|
||||
map<spot>.getOrDefault(1, "foo")</spot>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention replaces <b>map.getOrDefault(key, defaultValue)</b> function calls with the indexing and elvis operator (<b>map[key] ?: defaultValue</b>).
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.isNullable
|
||||
|
||||
class ReplaceMapGetOrDefaultIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(
|
||||
KtDotQualifiedExpression::class.java, "Replace with indexing and elvis operator"
|
||||
) {
|
||||
companion object {
|
||||
private val getOrDefaultFqName = FqName("kotlin.collections.Map.getOrDefault")
|
||||
}
|
||||
|
||||
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
||||
val callExpression = element.callExpression ?: return null
|
||||
val calleeExpression = callExpression.calleeExpression ?: return null
|
||||
if (calleeExpression.text != getOrDefaultFqName.shortName().asString()) return null
|
||||
val (firstArg, secondArg) = callExpression.arguments() ?: return null
|
||||
val context = element.analyze(BodyResolveMode.PARTIAL)
|
||||
if (callExpression.getResolvedCall(context)?.isCalling(getOrDefaultFqName) != true) return null
|
||||
if (element.receiverExpression.getType(context)?.arguments?.lastOrNull()?.type?.isNullable() == true) return null
|
||||
text = "Replace with ${element.receiverExpression.text}[${firstArg.text}] ?: ${secondArg.text}"
|
||||
return calleeExpression.textRange
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
||||
val callExpression = element.callExpression ?: return
|
||||
val (firstArg, secondArg) = callExpression.arguments() ?: return
|
||||
val replaced = element.replaced(
|
||||
KtPsiFactory(element).createExpressionByPattern("$0[$1] ?: $2", element.receiverExpression, firstArg, secondArg)
|
||||
)
|
||||
replaced.findDescendantOfType<KtArrayAccessExpression>()?.leftBracket?.startOffset?.let {
|
||||
editor?.caretModel?.moveToOffset(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtCallExpression.arguments(): Pair<KtExpression, KtExpression>? {
|
||||
val args = valueArguments
|
||||
if (args.size != 2) return null
|
||||
val first = args[0].getArgumentExpression() ?: return null
|
||||
val second = args[1].getArgumentExpression() ?: return null
|
||||
return first to second
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ReplaceMapGetOrDefaultIntention
|
||||
@@ -0,0 +1,4 @@
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
fun test(map: Map<Int, String>) {
|
||||
map.<caret>getOrDefault(1, "bar") + "baz"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
fun test(map: Map<Int, String>) {
|
||||
(map[1] ?: "bar") + "baz"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// IS_APPLICABLE: false
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
fun main() {
|
||||
val map = mapOf(1 to "", 2 to null)
|
||||
val b = map.getOrDefault<caret>(2, "bar") == null
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
fun test(map: Map<Int, String>) {
|
||||
map.getOrDefault<caret>(1, "bar")
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
fun test(map: Map<Int, String>) {
|
||||
map<caret>[1] ?: "bar"
|
||||
}
|
||||
@@ -14177,6 +14177,34 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceMapGetOrDefault")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceMapGetOrDefault extends AbstractIntentionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceMapGetOrDefault() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceMapGetOrDefault"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("inExpression.kt")
|
||||
public void testInExpression() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceMapGetOrDefault/inExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullableValue.kt")
|
||||
public void testNullableValue() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceMapGetOrDefault/nullableValue.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/intentions/replaceMapGetOrDefault/simple.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceSizeCheckWithIsNotEmpty")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -220,7 +220,8 @@ private val inspectionLikePostProcessingGroup =
|
||||
generalInspectionBasedProcessing(LiftReturnOrAssignmentInspection(skipLongExpressions = false)),
|
||||
intentionBasedProcessing(RemoveEmptyPrimaryConstructorIntention()),
|
||||
generalInspectionBasedProcessing(MayBeConstantInspection()),
|
||||
RemoveForExpressionLoopParameterTypeProcessing()
|
||||
RemoveForExpressionLoopParameterTypeProcessing(),
|
||||
intentionBasedProcessing(ReplaceMapGetOrDefaultIntention())
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// RUNTIME_WITH_FULL_JDK
|
||||
import java.util.Map;
|
||||
|
||||
class C {
|
||||
String foo(Map<Integer, String> map) {
|
||||
return map.getOrDefault(1, "bar");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
internal class C {
|
||||
fun foo(map: Map<Int, String>): String {
|
||||
return map[1] ?: "bar"
|
||||
}
|
||||
}
|
||||
+5
@@ -4102,6 +4102,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
|
||||
runTest("nj2k/testData/newJ2k/postProcessing/java8MapForEachWithFullJdk.java");
|
||||
}
|
||||
|
||||
@TestMetadata("MapGetOrDefault.java")
|
||||
public void testMapGetOrDefault() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/postProcessing/MapGetOrDefault.java");
|
||||
}
|
||||
|
||||
@TestMetadata("NotIs.java")
|
||||
public void testNotIs() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/postProcessing/NotIs.java");
|
||||
|
||||
Reference in New Issue
Block a user