Add intention to replace Math.max/min with coerceAtLeast/coerceAtMost #KT-13945 Fixed

This commit is contained in:
shiraji
2016-09-23 13:33:58 +09:00
committed by Mikhail Glukhikh
parent 4522d2c7da
commit 3aedf0d79f
33 changed files with 339 additions and 6 deletions
@@ -0,0 +1 @@
<spot>1.coerceAtLeast(2)</spot>
@@ -0,0 +1 @@
<spot>Math.max(1, 2)</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention replaces 'Math.max' calls with 'coerceAtLeast'
</body>
</html>
@@ -0,0 +1 @@
<spot>1.coerceAtMost(2)</spot>
@@ -0,0 +1 @@
<spot>Math.min(1, 2)</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention replaces 'Math.min' calls with 'coerceAtMost'
</body>
</html>
+10
View File
@@ -1354,6 +1354,16 @@
<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>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,20 @@
/*
* 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")
@@ -0,0 +1,44 @@
/*
* 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.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
abstract class ReplaceMathMethodsWithKotlinNativeMethodsIntention(
text: String, val replacedMethodName: String, val mathMethodName: String
) : SelfTargetingOffsetIndependentIntention<KtCallExpression>(KtCallExpression::class.java, text) {
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val target = element.getStrictParentOfType<KtDotQualifiedExpression>() ?: element
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}")
}
@@ -0,0 +1,20 @@
/*
* 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")
@@ -90,12 +90,7 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
}
private fun isLetMethod(element: KtCallExpression) =
element.calleeExpression?.text == "let" && isMethodCall(element, "kotlin.let")
private fun isMethodCall(expression: KtExpression, fqMethodName: String): Boolean {
val resolvedCall = expression.getResolvedCall(expression.analyze()) ?: return false
return resolvedCall.resultingDescriptor.fqNameUnsafe.asString() == fqMethodName
}
element.calleeExpression?.text == "let" && element.isMethodCall("kotlin.let")
private fun KtLambdaExpression.getParameterName(): String? {
val parameters = valueParameters
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.psi.typeRefHelpers.setReceiverTypeReference
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isFlexible
@@ -72,6 +73,11 @@ fun KtContainerNode.description(): String? {
return null
}
fun KtCallExpression.isMethodCall(fqMethodName: String): Boolean {
val resolvedCall = this.getResolvedCall(this.analyze()) ?: return false
return resolvedCall.resultingDescriptor.fqNameUnsafe.asString() == fqMethodName
}
fun isAutoCreatedItUsage(expression: KtNameReferenceExpression): Boolean {
if (expression.getReferencedName() != "it") return false
val context = expression.analyze(BodyResolveMode.PARTIAL)
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ReplaceMathMaxWithCoerceAtLeastIntention
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo() {
Math.max(1, 2)<caret>
}
object Math {
fun max(a: Int, b: Int) = 0
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
max(1.1, 1.2)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
1.1.coerceAtLeast(1.2)
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun max(p0: Double, p1: Double): Double defined in java.lang.Math<br>public open fun max(p0: Float, p1: Float): Float defined in java.lang.Math<br>public open fun max(p0: Int, p1: Int): Int defined in java.lang.Math<br>public open fun max(p0: Long, p1: Long): Long defined in java.lang.Math
import java.lang.Math.max
fun foo() {
max(1.1, 1.2, 1.3)<caret>
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
Math.max(2, 1)<caret>
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
2.coerceAtLeast(1)
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun max(p0: Double, p1: Double): Double defined in java.lang.Math<br>public open fun max(p0: Float, p1: Float): Float defined in java.lang.Math<br>public open fun max(p0: Int, p1: Int): Int defined in java.lang.Math<br>public open fun max(p0: Long, p1: Long): Long defined in java.lang.Math
import java.lang.Math.max
fun foo() {
max(1.1)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
max(1, 2)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.max
fun foo() {
1.coerceAtLeast(2)
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ReplaceMathMinWithCoerceAtMostIntention
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo() {
Math.min(1, 2)<caret>
}
object Math {
fun min(a: Int, b: Int) = 0
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
min(1.1, 1.2)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
1.1.coerceAtMost(1.2)
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun min(p0: Double, p1: Double): Double defined in java.lang.Math<br>public open fun min(p0: Float, p1: Float): Float defined in java.lang.Math<br>public open fun min(p0: Int, p1: Int): Int defined in java.lang.Math<br>public open fun min(p0: Long, p1: Long): Long defined in java.lang.Math
import java.lang.Math.min
fun foo() {
min(1.1, 1.2, 1.3)<caret>
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
Math.min(2, 1)<caret>
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
2.coerceAtMost(1)
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public open fun min(p0: Double, p1: Double): Double defined in java.lang.Math<br>public open fun min(p0: Float, p1: Float): Float defined in java.lang.Math<br>public open fun min(p0: Int, p1: Int): Int defined in java.lang.Math<br>public open fun min(p0: Long, p1: Long): Long defined in java.lang.Math
import java.lang.Math.min
fun foo() {
min(1.1)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
min(1, 2)<caret>
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.lang.Math.min
fun foo() {
1.coerceAtMost(2)
}
@@ -10526,6 +10526,96 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceMathMaxWithCoerceAtLeast extends AbstractIntentionTest {
public void testAllFilesPresentInReplaceMathMaxWithCoerceAtLeast() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("customMaxMethod.kt")
public void testCustomMaxMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/customMaxMethod.kt");
doTest(fileName);
}
@TestMetadata("doubles.kt")
public void testDoubles() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/doubles.kt");
doTest(fileName);
}
@TestMetadata("moreThan2ValueArg.kt")
public void testMoreThan2ValueArg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/moreThan2ValueArg.kt");
doTest(fileName);
}
@TestMetadata("noImport.kt")
public void testNoImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/noImport.kt");
doTest(fileName);
}
@TestMetadata("oneValueArg.kt")
public void testOneValueArg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/oneValueArg.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMaxWithCoerceAtLeast/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceMathMinWithCoerceAtMost extends AbstractIntentionTest {
public void testAllFilesPresentInReplaceMathMinWithCoerceAtMost() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceMathMinWithCoerceAtMost"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("customMinMethod.kt")
public void testCustomMinMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/customMinMethod.kt");
doTest(fileName);
}
@TestMetadata("doubles.kt")
public void testDoubles() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/doubles.kt");
doTest(fileName);
}
@TestMetadata("moreThan2ValueArg.kt")
public void testMoreThan2ValueArg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/moreThan2ValueArg.kt");
doTest(fileName);
}
@TestMetadata("noImport.kt")
public void testNoImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/noImport.kt");
doTest(fileName);
}
@TestMetadata("oneValueArg.kt")
public void testOneValueArg() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/oneValueArg.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceMathMinWithCoerceAtMost/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/replaceRangeToWithUntil")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)