Add intention to replace Math.max/min with coerceAtLeast/coerceAtMost #KT-13945 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
4522d2c7da
commit
3aedf0d79f
@@ -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"
|
||||
|
||||
+20
@@ -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")
|
||||
+44
@@ -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}")
|
||||
}
|
||||
+20
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user