From d4a9b922e51fb96f639309c4961e04c996be2c59 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 20 Oct 2014 15:01:11 +0400 Subject: [PATCH] Create From Usage: Fix template insertion when target container is not empty --- .../callableBuilder/CallableBuilder.kt | 9 +++++++-- .../call/afterFunOnUserTypeWithDeclarations.kt | 14 ++++++++++++++ .../call/beforeFunOnUserTypeWithDeclarations.kt | 10 ++++++++++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 8 +++++++- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/afterFunOnUserTypeWithDeclarations.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithDeclarations.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt index d29e1b05292..00f9f33cada 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt @@ -61,6 +61,8 @@ import org.jetbrains.kotlin.util.printAndReturn import org.jetbrains.jet.lang.types.checker.JetTypeChecker import org.jetbrains.jet.plugin.intentions.declarations.DeclarationUtils import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction +import org.jetbrains.jet.lang.psi.psiUtil.getParentByType +import com.intellij.psi.SmartPointerManager private val TYPE_PARAMETER_LIST_VARIABLE_NAME = "typeParameterList" private val TEMPLATE_FROM_USAGE_FUNCTION_BODY = "New Kotlin Function Body.kt" @@ -541,8 +543,9 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { } fun buildAndRunTemplate() { - val declaration = createDeclarationSkeleton() - val project = declaration.getProject() + val declarationSkeleton = createDeclarationSkeleton() + val project = declarationSkeleton.getProject() + val declarationPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(declarationSkeleton) // build templates PsiDocumentManager.getInstance(project).commitAllDocuments() @@ -551,6 +554,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { val caretModel = containingFileEditor.getCaretModel() caretModel.moveToOffset(containingFile.getNode().getStartOffset()) + val declaration = declarationPointer.getElement() + val builder = TemplateBuilderImpl(containingFile) if (declaration is JetProperty) { setupValVarTemplate(builder, declaration) diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/afterFunOnUserTypeWithDeclarations.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/afterFunOnUserTypeWithDeclarations.kt new file mode 100644 index 00000000000..ec7a79f5d14 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/afterFunOnUserTypeWithDeclarations.kt @@ -0,0 +1,14 @@ +// "Create function 'foo' from usage" "true" +class F { + fun bar() { + + } + + fun foo(i: Int, s: String): Int { + throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. + } +} + +class X { + val f: Int = F().foo(1, "2") +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithDeclarations.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithDeclarations.kt new file mode 100644 index 00000000000..22bcacfbaa5 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithDeclarations.kt @@ -0,0 +1,10 @@ +// "Create function 'foo' from usage" "true" +class F { + fun bar() { + + } +} + +class X { + val f: Int = F().foo(1, "2") +} \ 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 82d02b5cd8d..a3864576148 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -795,7 +795,13 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserType.kt"); doTest(fileName); } - + + @TestMetadata("beforeFunOnUserTypeWithDeclarations.kt") + public void testFunOnUserTypeWithDeclarations() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithDeclarations.kt"); + doTest(fileName); + } + @TestMetadata("beforeFunOnUserTypeWithTypeParams.kt") public void testFunOnUserTypeWithTypeParams() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserTypeWithTypeParams.kt");