From 5290553184e21b89912ff47b341e8d2012d454b9 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 26 Jul 2017 23:40:18 +0900 Subject: [PATCH] Fix Math.min with coerceAtMost intention inside qualified expression So #KT-19232 Fixed --- ...MethodsWithKotlinNativeMethodsIntention.kt | 11 +++---- .../KT-19232-1.kt | 7 +++++ .../KT-19232-1.kt.after | 7 +++++ .../KT-19232-2.kt | 5 ++++ .../KT-19232-2.kt.after | 5 ++++ .../maxInMax.kt | 6 ++++ .../maxInMax.kt.after | 6 ++++ .../KT-19232-1.kt | 7 +++++ .../KT-19232-1.kt.after | 7 +++++ .../KT-19232-2.kt | 5 ++++ .../KT-19232-2.kt.after | 5 ++++ .../intentions/IntentionTestGenerated.java | 30 +++++++++++++++++++ 12 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt.after create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt.after create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt create mode 100644 idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt.after create mode 100644 idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt create mode 100644 idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt.after create mode 100644 idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt create mode 100644 idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceMathMethodsWithKotlinNativeMethodsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceMathMethodsWithKotlinNativeMethodsIntention.kt index a0ef2cb9847..06bcfe2abf8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceMathMethodsWithKotlinNativeMethodsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceMathMethodsWithKotlinNativeMethodsIntention.kt @@ -18,18 +18,15 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.core.replaced -import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.psi.KtDotQualifiedExpression -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.createExpressionByPattern -import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis abstract class ReplaceMathMethodsWithKotlinNativeMethodsIntention( - text: String, val replacedMethodName: String, val mathMethodName: String + text: String, private val replacedMethodName: String, private val mathMethodName: String ) : SelfTargetingOffsetIndependentIntention(KtCallExpression::class.java, text) { override fun applyTo(element: KtCallExpression, editor: Editor?) { - val target = element.getStrictParentOfType() ?: element + val target = element.getQualifiedExpressionForSelectorOrThis() val valueArguments = element.valueArguments val methodName = replacedMethodName val newExpression = KtPsiFactory(element).createExpressionByPattern("$0.$methodName($1)", diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt new file mode 100644 index 00000000000..4ceaf89d9b0 --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +import java.lang.Math.max + +fun foo() { + Pair(max(1, 3), max(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt.after b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt.after new file mode 100644 index 00000000000..8b7c22210f0 --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +import java.lang.Math.max + +fun foo() { + Pair(1.coerceAtLeast(3), max(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt new file mode 100644 index 00000000000..0e68703dda0 --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + Pair(Math.max(1, 3), Math.max(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt.after b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt.after new file mode 100644 index 00000000000..27db48db8a6 --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + Pair(1.coerceAtLeast(3), Math.max(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt new file mode 100644 index 00000000000..1fa5b85bb6a --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.lang.Math.max + +fun foo() { + Math.max(max(1, 3), max(2, 4)) +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt.after b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt.after new file mode 100644 index 00000000000..3892aa9b404 --- /dev/null +++ b/idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.lang.Math.max + +fun foo() { + Math.max(1.coerceAtLeast(3), max(2, 4)) +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt new file mode 100644 index 00000000000..48de805348a --- /dev/null +++ b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +import java.lang.Math.min + +fun foo() { + Pair(min(1, 3), min(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt.after b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt.after new file mode 100644 index 00000000000..fbd5356a253 --- /dev/null +++ b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +import java.lang.Math.min + +fun foo() { + Pair(1.coerceAtMost(3), min(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt new file mode 100644 index 00000000000..33548d645a6 --- /dev/null +++ b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + Pair(Math.min(1, 3), Math.min(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt.after b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt.after new file mode 100644 index 00000000000..462b4d5d5f0 --- /dev/null +++ b/idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + Pair(1.coerceAtMost(3), Math.min(2, 4)).let { println(it) } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 258da7a339b..7be5762bdd6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13454,6 +13454,24 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("KT-19232-1.kt") + public void testKT_19232_1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt"); + doTest(fileName); + } + + @TestMetadata("KT-19232-2.kt") + public void testKT_19232_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt"); + doTest(fileName); + } + + @TestMetadata("maxInMax.kt") + public void testMaxInMax() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt"); + doTest(fileName); + } + @TestMetadata("moreThan2ValueArg.kt") public void testMoreThan2ValueArg() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/moreThan2ValueArg.kt"); @@ -13499,6 +13517,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("KT-19232-1.kt") + public void testKT_19232_1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt"); + doTest(fileName); + } + + @TestMetadata("KT-19232-2.kt") + public void testKT_19232_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt"); + doTest(fileName); + } + @TestMetadata("moreThan2ValueArg.kt") public void testMoreThan2ValueArg() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/moreThan2ValueArg.kt");