diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt index 75d45b23226..a8a5e76e506 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt @@ -34,6 +34,20 @@ import org.jetbrains.jet.lang.psi.JetFile public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention("replace.with.infix.function.call.intention", javaClass()) { override fun isApplicableTo(element: JetCallExpression): Boolean { + throw IllegalStateException("isApplicableTo(JetExpressionImpl, Editor) should be called instead") + } + + override fun isApplicableTo(element: JetCallExpression, editor: Editor): Boolean { + val caretLocation = editor.getCaretModel().getOffset() + + val calleeExpr = element.getCalleeExpression() + if (calleeExpr == null) return false + + val textRange = calleeExpr.getTextRange() + if (textRange == null) return false + + if (caretLocation !in textRange) return false + val parent = element.getParent() if (parent is JetDotQualifiedExpression) { diff --git a/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt new file mode 100644 index 00000000000..7fc217bef6b --- /dev/null +++ b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt @@ -0,0 +1,3 @@ +fun foo(x: Int) { + x.times(1) +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt.after b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt.after new file mode 100644 index 00000000000..1650135ae7b --- /dev/null +++ b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt.after @@ -0,0 +1,3 @@ +fun foo(x: Int) { + x times 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideReceiverExpr.kt b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideReceiverExpr.kt new file mode 100644 index 00000000000..e1f7d36d272 --- /dev/null +++ b/idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideReceiverExpr.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +fun foo(num: Int) { + num.times(1) +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceWithInfixFunctionCall/inapplicableCaretPosition.kt b/idea/testData/intentions/replaceWithInfixFunctionCall/inapplicableCaretPosition.kt new file mode 100644 index 00000000000..06bc10f47e4 --- /dev/null +++ b/idea/testData/intentions/replaceWithInfixFunctionCall/inapplicableCaretPosition.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +fun foo(x: Int) { + x.times(1) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 234432a2138..daeab59cba7 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -1751,6 +1751,16 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/binaryExpressionArgument.kt"); } + @TestMetadata("caretInsideCalleeExpr.kt") + public void testCaretInsideCalleeExpr() throws Exception { + doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideCalleeExpr.kt"); + } + + @TestMetadata("caretInsideReceiverExpr.kt") + public void testCaretInsideReceiverExpr() throws Exception { + doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/caretInsideReceiverExpr.kt"); + } + @TestMetadata("doubleFunctionCall.kt") public void testDoubleFunctionCall() throws Exception { doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/doubleFunctionCall.kt"); @@ -1776,6 +1786,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/functionSafeCall.kt"); } + @TestMetadata("inapplicableCaretPosition.kt") + public void testInapplicableCaretPosition() throws Exception { + doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/inapplicableCaretPosition.kt"); + } + @TestMetadata("multipleArguments.kt") public void testMultipleArguments() throws Exception { doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/multipleArguments.kt");