diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 49c3e205664..dc639cfd5bd 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -276,6 +276,8 @@ replace.with.dot.qualified.method.call.intention=Replace with simple method call replace.with.dot.qualified.method.call.intention.family=Replace with simple method call replace.with.infix.function.call.intention=Replace with infix function call replace.with.infix.function.call.intention.family=Replace with infix function call +replace.with.infix.function.call.intention.error.resolution.failed=The element cannot be resolved +replace.with.infix.function.call.intention.error.package.call=Cannot be applied with a package as the receiver replace.explicit.function.literal.param.with.it=Replace explicit parameter ''{0}'' with ''it'' replace.explicit.function.literal.param.with.it.family=Replace Explicit Parameter With 'it' move.lambda.inside.parentheses=Move lambda function into parentheses diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt index bd70e67a1c2..d68f59361f1 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/ReplaceWithInfixFunctionCallIntention.kt @@ -19,14 +19,18 @@ package org.jetbrains.jet.plugin.intentions import org.jetbrains.jet.lang.psi.JetCallExpression import com.intellij.openapi.editor.Editor import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression -import org.jetbrains.jet.lang.psi.JetValueArgument -import org.jetbrains.jet.lang.psi.JetPsiFactory -import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetFile import org.jetbrains.jet.plugin.caches.resolve.getBindingContext +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.plugin.JetBundle +import com.intellij.openapi.ui.popup.JBPopupFactory +import org.jetbrains.jet.lang.psi.JetValueArgument +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.analyzer.computeTypeInfoInContext +import org.jetbrains.jet.lang.types.PackageType +import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils +import org.jetbrains.jet.lang.psi.JetPsiFactory -public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention("replace.with.infix.function.call.intention", javaClass()) { +public open 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") } @@ -54,11 +58,12 @@ public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention() val functionLiteralArguments = element.getFunctionLiteralArguments() + val bindingContext = AnalyzerFacadeWithCache.getContextForElement(parent) + val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, parent] + + when { + scope == null -> { + intentionFailed(editor, "resolution.failed") + return + } + else -> + when (receiver.computeTypeInfoInContext(scope).getType()) { + is PackageType -> { + intentionFailed(editor, "package.call") + return + } + } + } rightHandTextStringBuilder.append( if (valueArguments.size() > 0) @@ -92,5 +118,4 @@ public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntentionprintln("") +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceWithInfixFunctionCall/singlePackageFunctionCall.kt b/idea/testData/intentions/replaceWithInfixFunctionCall/singlePackageFunctionCall.kt new file mode 100644 index 00000000000..5b63ec8b0f9 --- /dev/null +++ b/idea/testData/intentions/replaceWithInfixFunctionCall/singlePackageFunctionCall.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// IS_APPLICABLE: false +package demo + +fun foo(str: String) = kotlin.io.println(str) + +fun main() { + demo.foo("") +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index 3c807bcdf24..d293dbdcd07 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -179,7 +179,7 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes } public void doTestReplaceWithInfixFunctionCall(@NotNull String path) throws Exception { - doTestIntention(path, new ReplaceWithInfixFunctionCallIntention()); + doTestIntention(path, new TestableReplaceWithInfixFunctionCallIntention()); } public void doTestReplaceExplicitFunctionLiteralParamWithIt(@NotNull String path) throws Exception { diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 1d993fa0111..d14071fa84d 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -2067,6 +2067,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/nullAssertedReceiver.kt"); } + @TestMetadata("packageFunctionCall.kt") + public void testPackageFunctionCall() throws Exception { + doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/packageFunctionCall.kt"); + } + @TestMetadata("propertyAccess.kt") public void testPropertyAccess() throws Exception { doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/propertyAccess.kt"); @@ -2087,6 +2092,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/simpleMethodCall.kt"); } + @TestMetadata("singlePackageFunctionCall.kt") + public void testSinglePackageFunctionCall() throws Exception { + doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/singlePackageFunctionCall.kt"); + } + @TestMetadata("zeroArguments.kt") public void testZeroArguments() throws Exception { doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/zeroArguments.kt"); diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/TestableReplaceWithInfixFunctionCallIntention.kt b/idea/tests/org/jetbrains/jet/plugin/intentions/TestableReplaceWithInfixFunctionCallIntention.kt new file mode 100644 index 00000000000..f9987e22b61 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/TestableReplaceWithInfixFunctionCallIntention.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2014 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.jet.plugin.intentions + +import com.intellij.openapi.editor.Editor + +public class TestableReplaceWithInfixFunctionCallIntention : ReplaceWithInfixFunctionCallIntention() { + override fun intentionFailed(editor: Editor, messageID: String) { + throw IntentionTestException(messageID) + } +} \ No newline at end of file