Create from usage: Do not generate function body if it's declared as a trait member

#KT-6078 Fixed
This commit is contained in:
Alexey Sedunov
2015-02-18 14:16:01 +03:00
parent 60d02dab0f
commit 0ae69e0d3e
6 changed files with 56 additions and 3 deletions
@@ -438,7 +438,13 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
else ""
val declaration : JetNamedDeclaration = when (callableInfo.kind) {
CallableKind.FUNCTION -> psiFactory.createFunction("${modifiers}fun<> $header {}")
CallableKind.FUNCTION -> {
val body = when {
containingElement is JetClass && containingElement.isTrait() && !config.isExtension -> ""
else -> "{}"
}
psiFactory.createFunction("${modifiers}fun<> $header $body")
}
CallableKind.CONSTRUCTOR -> {
with((callableInfo as ConstructorInfo).classInfo) {
val classBody = when (kind) {
@@ -620,6 +626,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
}
private fun setupFunctionBody(func: JetNamedFunction) {
val oldBody = func.getBodyExpression() ?: return
val fileTemplate = FileTemplateManager.getInstance()!!.getCodeTemplate(TEMPLATE_FROM_USAGE_FUNCTION_BODY)
val properties = Properties()
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, if (skipReturnType) "Unit" else func.getTypeReference()!!.getText())
@@ -641,8 +649,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
throw IncorrectOperationException("Failed to parse file template", e)
}
val newBodyExpression = JetPsiFactory(func).createFunctionBody(bodyText)
func.getBodyExpression()!!.replace(newBodyExpression)
oldBody.replace(JetPsiFactory(func).createFunctionBody(bodyText))
}
private fun setupCallTypeArguments(callElement: JetCallElement, typeParameters: List<TypeParameterDescriptor>) {
@@ -0,0 +1,11 @@
// "Create extension function 'foo'" "true"
trait T
fun test(t: T) {
val b: Boolean = t.foo("1", 2)
}
fun T.foo(s: String, i: Int): Boolean {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
@@ -0,0 +1,9 @@
// "Create function 'foo'" "true"
trait T {
fun foo(s: String, i: Int): Boolean
}
fun test(t: T) {
val b: Boolean = t.foo("1", 2)
}
@@ -0,0 +1,7 @@
// "Create extension function 'foo'" "true"
trait T
fun test(t: T) {
val b: Boolean = t.<caret>foo("1", 2)
}
@@ -0,0 +1,7 @@
// "Create function 'foo'" "true"
trait T
fun test(t: T) {
val b: Boolean = t.<caret>foo("1", 2)
}
@@ -1534,6 +1534,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeExtensionFunOnTrait.kt")
public void testExtensionFunOnTrait() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeExtensionFunOnTrait.kt");
doTest(fileName);
}
@TestMetadata("beforeFunExtraArgs.kt")
public void testFunExtraArgs() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunExtraArgs.kt");
@@ -1570,6 +1576,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeFunOnTrait.kt")
public void testFunOnTrait() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnTrait.kt");
doTest(fileName);
}
@TestMetadata("beforeFunOnUserObject.kt")
public void testFunOnUserObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserObject.kt");