Smaller ranges for intentions

This commit is contained in:
Valentin Kipyatkov
2015-05-12 14:07:58 +03:00
parent 11812a9ece
commit e601057e93
5 changed files with 48 additions and 43 deletions
@@ -17,7 +17,8 @@
package org.jetbrains.kotlin.idea.intentions.conventionNameCalls package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor 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.callExpression
import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.functionName
import org.jetbrains.kotlin.idea.intentions.toResolvedCall 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.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingOffsetIndependentIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with binary operator") { public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with binary operator") {
override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
val operation = operation(element.functionName) ?: return false val operation = operation(element.functionName) ?: return null
val resolvedCall = element.toResolvedCall() ?: return false val resolvedCall = element.toResolvedCall() ?: return null
if (!resolvedCall.getStatus().isSuccess()) return false if (!resolvedCall.getStatus().isSuccess()) return null
if (resolvedCall.getCall().getTypeArgumentList() != null) return false if (resolvedCall.getCall().getTypeArgumentList() != null) return null
val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return false val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null
if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return false if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return null
setText("Replace with '$operation' operator") setText("Replace with '$operation' operator")
return true return element.callExpression!!.getCalleeExpression()!!.getTextRange()
} }
override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {
@@ -17,21 +17,22 @@
package org.jetbrains.kotlin.idea.intentions.conventionNameCalls package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor 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.callExpression
import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.functionName
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern import org.jetbrains.kotlin.psi.createExpressionByPattern
public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingOffsetIndependentIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with unary operator") { public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with unary operator") {
override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
val operation = operation(element.functionName) ?: return false val operation = operation(element.functionName) ?: return null
val call = element.callExpression ?: return false val call = element.callExpression ?: return null
if (call.getTypeArgumentList() != null) return false if (call.getTypeArgumentList() != null) return null
if (!call.getValueArguments().isEmpty()) return false if (!call.getValueArguments().isEmpty()) return null
setText("Replace with '$operation' operator") setText("Replace with '$operation' operator")
return true return call.getCalleeExpression()!!.getTextRange()
} }
override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {
@@ -17,7 +17,8 @@
package org.jetbrains.kotlin.idea.intentions.conventionNameCalls package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor 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.callExpression
import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.functionName
import org.jetbrains.kotlin.idea.intentions.toResolvedCall 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.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.expressions.OperatorConventions import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class ReplaceContainsIntention : JetSelfTargetingOffsetIndependentIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'contains' call with 'in' operator") { public class ReplaceContainsIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'contains' call with 'in' operator") {
override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
if (element.functionName != OperatorConventions.CONTAINS.asString()) return false if (element.functionName != OperatorConventions.CONTAINS.asString()) return null
val resolvedCall = element.toResolvedCall() ?: return false val resolvedCall = element.toResolvedCall() ?: return null
if (!resolvedCall.getStatus().isSuccess()) return false if (!resolvedCall.getStatus().isSuccess()) return null
val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return false val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null
if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return false if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return null
val target = resolvedCall.getResultingDescriptor() val target = resolvedCall.getResultingDescriptor()
val returnType = target.getReturnType() ?: return false val returnType = target.getReturnType() ?: return null
return target.builtIns.isBooleanOrSubtype(returnType) if (!target.builtIns.isBooleanOrSubtype(returnType)) return null
return element.callExpression!!.getCalleeExpression()!!.getTextRange()
} }
override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {
@@ -17,23 +17,23 @@
package org.jetbrains.kotlin.idea.intentions.conventionNameCalls package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor 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.callExpression
import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.functionName
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.buildExpression import org.jetbrains.kotlin.psi.buildExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
public class ReplaceGetIntention : JetSelfTargetingOffsetIndependentIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'get' call with index operator") { public class ReplaceGetIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'get' call with index operator") {
override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
if (element.functionName != "get") return false if (element.functionName != "get") return null
val call = element.callExpression ?: return false val call = element.callExpression ?: return null
if (call.getTypeArgumentList() != null) return false if (call.getTypeArgumentList() != null) return null
val arguments = call.getValueArguments() val arguments = call.getValueArguments()
if (arguments.isEmpty()) return false if (arguments.isEmpty()) return null
if (arguments.any { it.isNamed() }) return false if (arguments.any { it.isNamed() }) return null
return true return call.getCalleeExpression()!!.getTextRange()
} }
override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {
@@ -17,16 +17,17 @@
package org.jetbrains.kotlin.idea.intentions.conventionNameCalls package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor 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.callExpression
import org.jetbrains.kotlin.idea.intentions.functionName import org.jetbrains.kotlin.idea.intentions.functionName
import org.jetbrains.kotlin.psi.JetCallExpression
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.types.expressions.OperatorConventions import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class ReplaceInvokeIntention : JetSelfTargetingOffsetIndependentIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'invoke' with direct call") { public class ReplaceInvokeIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'invoke' with direct call") {
override fun isApplicableTo(element: JetDotQualifiedExpression): Boolean { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
return element.functionName == OperatorConventions.INVOKE.asString() if (element.functionName != OperatorConventions.INVOKE.asString()) return null
return element.callExpression!!.getCalleeExpression()!!.getTextRange()
} }
override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) {