Merge ReplaceMathMaxWithCoerceAtLeastIntention & ReplaceMathMinWithCoerceAtMostIntention with ReplaceJavaStaticMethodWithKotlinAnalogInspection

Relates to #KT-31502
This commit is contained in:
Dmitry Gridin
2019-05-27 18:01:53 +07:00
parent 26478b1374
commit d3339cae46
51 changed files with 36 additions and 412 deletions
-10
View File
@@ -1408,16 +1408,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceMathMaxWithCoerceAtLeastIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceMathMinWithCoerceAtMostIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveConstructorKeywordIntention</className>
<category>Kotlin</category>
@@ -1 +0,0 @@
<spot>1.coerceAtLeast(2)</spot>
@@ -1 +0,0 @@
<spot>Math.max(1, 2)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces <b>Math.max</b> calls with <b>coerceAtLeast</b> calls.
</body>
</html>
@@ -1 +0,0 @@
<spot>1.coerceAtMost(2)</spot>
@@ -1 +0,0 @@
<spot>Math.min(1, 2)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces <b>Math.min</b> calls with <b>coerceAtMost</b> calls.
</body>
</html>
@@ -148,7 +148,9 @@ class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspecti
Replacement("java.lang.Math.log1p", "kotlin.math.ln1p"),
Replacement("java.lang.Math.log10", "kotlin.math.log10"),
Replacement("java.lang.Math.max", "kotlin.math.max"),
Replacement("java.lang.Math.max", "kotlin.ranges.coerceAtLeast", toExtensionFunction = true),
Replacement("java.lang.Math.min", "kotlin.math.min"),
Replacement("java.lang.Math.min", "kotlin.ranges.coerceAtMost", toExtensionFunction = true),
Replacement("java.lang.Math.nextDown", "kotlin.math.nextDown", toExtensionFunction = true),
Replacement("java.lang.Math.nextAfter", "kotlin.math.nextTowards", toExtensionFunction = true),
Replacement("java.lang.Math.nextUp", "kotlin.math.nextUp", toExtensionFunction = true),
@@ -1,20 +0,0 @@
/*
* Copyright 2010-2016 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
class ReplaceMathMaxWithCoerceAtLeastIntention :
ReplaceMathMethodsWithKotlinNativeMethodsIntention("Replace Math.max with coerceAtLeast", "coerceAtLeast", "max")
@@ -1,43 +0,0 @@
/*
* Copyright 2010-2016 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.idea.core.replaced
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
abstract class ReplaceMathMethodsWithKotlinNativeMethodsIntention(
text: String, private val replacedMethodName: String, private val mathMethodName: String
) : SelfTargetingOffsetIndependentIntention<KtCallExpression>(KtCallExpression::class.java, text) {
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val target = element.getQualifiedExpressionForSelectorOrThis()
val valueArguments = element.valueArguments
val methodName = replacedMethodName
val newExpression = KtPsiFactory(element).createExpressionByPattern("$0.$methodName($1)",
valueArguments[0].text, valueArguments[1].text)
target.replaced(newExpression)
}
override fun isApplicableTo(element: KtCallExpression) =
element.calleeExpression?.text == mathMethodName &&
element.valueArguments.size == 2 &&
element.isMethodCall("java.lang.Math.$mathMethodName")
}
@@ -1,20 +0,0 @@
/*
* Copyright 2010-2016 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
class ReplaceMathMinWithCoerceAtMostIntention :
ReplaceMathMethodsWithKotlinNativeMethodsIntention("Replace Math.min with coerceAtMost", "coerceAtMost", "min")
@@ -0,0 +1,5 @@
// FIX: Replace with `coerceAtLeast` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
<caret>Math.max(x, y)
}
@@ -0,0 +1,5 @@
// FIX: Replace with `coerceAtLeast` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
x.coerceAtLeast(y)
}
@@ -0,0 +1,5 @@
// FIX: Replace with `coerceAtMost` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
<caret>Math.min(x, y)
}
@@ -0,0 +1,5 @@
// FIX: Replace with `coerceAtMost` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
x.coerceAtMost(y)
}
@@ -1,3 +1,4 @@
// FIX: Replace with `max` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
<caret>Math.max(x, y)
@@ -1,5 +1,6 @@
import kotlin.math.max
// FIX: Replace with `max` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
max(x, y)
@@ -1,3 +1,4 @@
// FIX: Replace with `min` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
<caret>Math.min(x, y)
@@ -1,5 +1,6 @@
import kotlin.math.min
// FIX: Replace with `min` function
// WITH_RUNTIME
fun test(x: Double, y: Double) {
min(x, y)
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceMathMaxWithCoerceAtLeastIntention
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
Pair(<caret>max(1, 3), max(2, 4)).let { println(it) }
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
Pair(<caret>1.coerceAtLeast(3), max(2, 4)).let { println(it) }
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Pair(Math.max(1, 3)<caret>, Math.max(2, 4)).let { println(it) }
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Pair(1.coerceAtLeast(3)<caret>, Math.max(2, 4)).let { println(it) }
}
@@ -1,10 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo() {
Math.max(1, 2)<caret>
}
object Math {
fun max(a: Int, b: Int) = 0
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
max(1.1, 1.2)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
1.1.coerceAtLeast(1.2)
}
@@ -1,6 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
Math.max(max(1, 3)<caret>, max(2, 4))
}
@@ -1,6 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
Math.max(1.coerceAtLeast(3), max(2, 4))
}
@@ -1,9 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun max(a: Double, b: Double): Double defined in java.lang.Math<br>public open fun max(a: Float, b: Float): Float defined in java.lang.Math<br>public open fun max(a: Int, b: Int): Int defined in java.lang.Math<br>public open fun max(a: Long, b: Long): Long defined in java.lang.Math
import java.lang.Math.max
fun foo() {
max(1.1, 1.2, 1.3)<caret>
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Math.max(2, 1)<caret>
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
2.coerceAtLeast(1)
}
@@ -1,9 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun max(a: Double, b: Double): Double defined in java.lang.Math<br>public open fun max(a: Float, b: Float): Float defined in java.lang.Math<br>public open fun max(a: Int, b: Int): Int defined in java.lang.Math<br>public open fun max(a: Long, b: Long): Long defined in java.lang.Math
import java.lang.Math.max
fun foo() {
max(1.1)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
max(1, 2)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
1.coerceAtLeast(2)
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceMathMinWithCoerceAtMostIntention
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
Pair(<caret>min(1, 3), min(2, 4)).let { println(it) }
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
Pair(<caret>1.coerceAtMost(3), min(2, 4)).let { println(it) }
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Pair(Math.min(1, 3)<caret>, Math.min(2, 4)).let { println(it) }
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Pair(1.coerceAtMost(3)<caret>, Math.min(2, 4)).let { println(it) }
}
@@ -1,10 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo() {
Math.min(1, 2)<caret>
}
object Math {
fun min(a: Int, b: Int) = 0
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
min(1.1, 1.2)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
1.1.coerceAtMost(1.2)
}
@@ -1,9 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun min(a: Double, b: Double): Double defined in java.lang.Math<br>public open fun min(a: Float, b: Float): Float defined in java.lang.Math<br>public open fun min(a: Int, b: Int): Int defined in java.lang.Math<br>public open fun min(a: Long, b: Long): Long defined in java.lang.Math
import java.lang.Math.min
fun foo() {
min(1.1, 1.2, 1.3)<caret>
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
Math.min(2, 1)<caret>
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo() {
2.coerceAtMost(1)
}
@@ -1,9 +0,0 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun min(a: Double, b: Double): Double defined in java.lang.Math<br>public open fun min(a: Float, b: Float): Float defined in java.lang.Math<br>public open fun min(a: Int, b: Int): Int defined in java.lang.Math<br>public open fun min(a: Long, b: Long): Long defined in java.lang.Math
import java.lang.Math.min
fun foo() {
min(1.1)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
min(1, 2)<caret>
}
@@ -1,7 +0,0 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
1.coerceAtMost(2)
}
@@ -7529,6 +7529,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/ceil.kt");
}
@TestMetadata("coerceAtLeast.kt")
public void testCoerceAtLeast() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/coerceAtLeast.kt");
}
@TestMetadata("coerceAtMost.kt")
public void testCoerceAtMost() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/coerceAtMost.kt");
}
@TestMetadata("copySign.kt")
public void testCopySign() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/math/copySign.kt");
@@ -15000,117 +15000,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceMathMaxWithCoerceAtLeast extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceMathMaxWithCoerceAtLeast() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("customMaxMethod.kt")
public void testCustomMaxMethod() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/customMaxMethod.kt");
}
@TestMetadata("doubles.kt")
public void testDoubles() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/doubles.kt");
}
@TestMetadata("KT-19232-1.kt")
public void testKT_19232_1() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-1.kt");
}
@TestMetadata("KT-19232-2.kt")
public void testKT_19232_2() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/KT-19232-2.kt");
}
@TestMetadata("maxInMax.kt")
public void testMaxInMax() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/maxInMax.kt");
}
@TestMetadata("moreThan2ValueArg.kt")
public void testMoreThan2ValueArg() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/moreThan2ValueArg.kt");
}
@TestMetadata("noImport.kt")
public void testNoImport() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/noImport.kt");
}
@TestMetadata("oneValueArg.kt")
public void testOneValueArg() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/oneValueArg.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/simple.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceMathMinWithCoerceAtMost extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceMathMinWithCoerceAtMost() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceMathMinWithCoerceAtMost"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("customMinMethod.kt")
public void testCustomMinMethod() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/customMinMethod.kt");
}
@TestMetadata("doubles.kt")
public void testDoubles() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/doubles.kt");
}
@TestMetadata("KT-19232-1.kt")
public void testKT_19232_1() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-1.kt");
}
@TestMetadata("KT-19232-2.kt")
public void testKT_19232_2() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/KT-19232-2.kt");
}
@TestMetadata("moreThan2ValueArg.kt")
public void testMoreThan2ValueArg() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/moreThan2ValueArg.kt");
}
@TestMetadata("noImport.kt")
public void testNoImport() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/noImport.kt");
}
@TestMetadata("oneValueArg.kt")
public void testOneValueArg() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/oneValueArg.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/replaceMathMinWithCoerceAtMost/simple.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceSingleLineLetIntention")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)