diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateFunctionFromCallActionFactory.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateFunctionFromCallActionFactory.kt index d4fd95c71b2..8f28e63a209 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateFunctionFromCallActionFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/createFunction/CreateFunctionFromCallActionFactory.kt @@ -15,10 +15,25 @@ import org.jetbrains.jet.lang.resolve.calls.callUtil.getCall import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue import org.jetbrains.jet.lang.resolve.scopes.receivers.Qualifier import org.jetbrains.jet.plugin.quickfix.createFromUsage.callableBuilder.* +import org.jetbrains.jet.lang.diagnostics.Errors +import org.jetbrains.jet.lang.psi.psiUtil.getParentByType +import org.jetbrains.jet.lang.psi.psiUtil.getParentByTypeAndBranch object CreateFunctionFromCallActionFactory : JetSingleIntentionActionFactory() { override fun createAction(diagnostic: Diagnostic): IntentionAction? { - val callExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass()) ?: return null + val diagElement = diagnostic.getPsiElement() + val callExpr = when (diagnostic.getFactory()) { + in Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS -> { + val parent = diagElement.getParent() + if (parent is JetCallExpression && parent.getCalleeExpression() == diagElement) parent else diagElement + } + + Errors.NO_VALUE_FOR_PARAMETER, + Errors.TOO_MANY_ARGUMENTS -> diagElement.getParentByType(javaClass()) + + else -> throw AssertionError("Unexpected diagnostic: ${diagnostic.getFactory()}") + } + if (callExpr !is JetCallExpression) return null val calleeExpr = callExpr.getCalleeExpression() as? JetSimpleNameExpression ?: return null if (calleeExpr.getReferencedNameElementType() != JetTokens.IDENTIFIER) return null diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/beforePropertyOnUserType.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/beforePropertyOnUserType.kt new file mode 100644 index 00000000000..d0da68abc2f --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/beforePropertyOnUserType.kt @@ -0,0 +1,14 @@ +// "Create function 'foo' from usage" "false" +// ACTION: Convert to expression body +// ACTION: Replace with infix function call +// ERROR: Unresolved reference: x + +class A(val n: T) { + fun foo(p: Int) { + + } +} + +fun test() { + A(1).foo(x) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index b98b84080d1..ef94dc28b42 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -826,6 +826,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("beforePropertyOnUserType.kt") + public void testPropertyOnUserType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforePropertyOnUserType.kt"); + doTest(fileName); + } + @TestMetadata("beforeThisInClass.kt") public void testThisInClass() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeThisInClass.kt");