From e601057e9334a50f2d24371f234baa81c6b3dab4 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 12 May 2015 14:07:58 +0300 Subject: [PATCH] Smaller ranges for intentions --- .../ReplaceCallWithBinaryOperatorIntention.kt | 21 +++++++++--------- .../ReplaceCallWithUnaryOperatorIntention.kt | 17 +++++++------- .../ReplaceContainsIntention.kt | 22 ++++++++++--------- .../ReplaceGetIntention.kt | 20 ++++++++--------- .../ReplaceInvokeIntention.kt | 11 +++++----- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt index 8e3536d392b..ad63f4f8539 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt @@ -17,7 +17,8 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.toResolvedCall @@ -26,16 +27,16 @@ import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch -public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace call with binary operator") { - override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { - val operation = operation(element.functionName) ?: return false - val resolvedCall = element.toResolvedCall() ?: return false - if (!resolvedCall.getStatus().isSuccess()) return false - if (resolvedCall.getCall().getTypeArgumentList() != null) return false - val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return false - if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return false +public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace call with binary operator") { + override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { + val operation = operation(element.functionName) ?: return null + val resolvedCall = element.toResolvedCall() ?: return null + if (!resolvedCall.getStatus().isSuccess()) return null + if (resolvedCall.getCall().getTypeArgumentList() != null) return null + val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null + if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return null setText("Replace with '$operation' operator") - return true + return element.callExpression!!.getCalleeExpression()!!.getTextRange() } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt index 002c3d8e5c4..3a5d8921587 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt @@ -17,21 +17,22 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern -public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace call with unary operator") { - override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { - val operation = operation(element.functionName) ?: return false - val call = element.callExpression ?: return false - if (call.getTypeArgumentList() != null) return false - if (!call.getValueArguments().isEmpty()) return false +public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace call with unary operator") { + override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { + val operation = operation(element.functionName) ?: return null + val call = element.callExpression ?: return null + if (call.getTypeArgumentList() != null) return null + if (!call.getValueArguments().isEmpty()) return null setText("Replace with '$operation' operator") - return true + return call.getCalleeExpression()!!.getTextRange() } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt index 8b7fd5371c2..d99e414ce54 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt @@ -17,7 +17,8 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.toResolvedCall @@ -27,17 +28,18 @@ import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.types.expressions.OperatorConventions -public class ReplaceContainsIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'contains' call with 'in' operator") { - override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { - if (element.functionName != OperatorConventions.CONTAINS.asString()) return false - val resolvedCall = element.toResolvedCall() ?: return false - if (!resolvedCall.getStatus().isSuccess()) return false - val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return false - if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return false +public class ReplaceContainsIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'contains' call with 'in' operator") { + override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { + if (element.functionName != OperatorConventions.CONTAINS.asString()) return null + val resolvedCall = element.toResolvedCall() ?: return null + if (!resolvedCall.getStatus().isSuccess()) return null + val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null + if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return null val target = resolvedCall.getResultingDescriptor() - val returnType = target.getReturnType() ?: return false - return target.builtIns.isBooleanOrSubtype(returnType) + val returnType = target.getReturnType() ?: return null + if (!target.builtIns.isBooleanOrSubtype(returnType)) return null + return element.callExpression!!.getCalleeExpression()!!.getTextRange() } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt index 1de67312b27..0c7379b8aa3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt @@ -17,23 +17,23 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.buildExpression -import org.jetbrains.kotlin.psi.createExpressionByPattern -public class ReplaceGetIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'get' call with index operator") { - override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { - if (element.functionName != "get") return false - val call = element.callExpression ?: return false - if (call.getTypeArgumentList() != null) return false +public class ReplaceGetIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'get' call with index operator") { + override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { + if (element.functionName != "get") return null + val call = element.callExpression ?: return null + if (call.getTypeArgumentList() != null) return null val arguments = call.getValueArguments() - if (arguments.isEmpty()) return false - if (arguments.any { it.isNamed() }) return false - return true + if (arguments.isEmpty()) return null + if (arguments.any { it.isNamed() }) return null + return call.getCalleeExpression()!!.getTextRange() } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt index 0fdc9df1da4..3e087787c73 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt @@ -17,16 +17,17 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.openapi.editor.Editor -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.intentions.functionName -import org.jetbrains.kotlin.psi.JetCallExpression import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.types.expressions.OperatorConventions -public class ReplaceInvokeIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'invoke' with direct call") { - override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { - return element.functionName == OperatorConventions.INVOKE.asString() +public class ReplaceInvokeIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'invoke' with direct call") { + override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { + if (element.functionName != OperatorConventions.INVOKE.asString()) return null + return element.callExpression!!.getCalleeExpression()!!.getTextRange() } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {