Create from usage: Do not generate function body if it's declared as a trait member
#KT-6078 Fixed
This commit is contained in:
+10
-3
@@ -438,7 +438,13 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
|||||||
else ""
|
else ""
|
||||||
|
|
||||||
val declaration : JetNamedDeclaration = when (callableInfo.kind) {
|
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 -> {
|
CallableKind.CONSTRUCTOR -> {
|
||||||
with((callableInfo as ConstructorInfo).classInfo) {
|
with((callableInfo as ConstructorInfo).classInfo) {
|
||||||
val classBody = when (kind) {
|
val classBody = when (kind) {
|
||||||
@@ -620,6 +626,8 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setupFunctionBody(func: JetNamedFunction) {
|
private fun setupFunctionBody(func: JetNamedFunction) {
|
||||||
|
val oldBody = func.getBodyExpression() ?: return
|
||||||
|
|
||||||
val fileTemplate = FileTemplateManager.getInstance()!!.getCodeTemplate(TEMPLATE_FROM_USAGE_FUNCTION_BODY)
|
val fileTemplate = FileTemplateManager.getInstance()!!.getCodeTemplate(TEMPLATE_FROM_USAGE_FUNCTION_BODY)
|
||||||
val properties = Properties()
|
val properties = Properties()
|
||||||
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, if (skipReturnType) "Unit" else func.getTypeReference()!!.getText())
|
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)
|
throw IncorrectOperationException("Failed to parse file template", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
val newBodyExpression = JetPsiFactory(func).createFunctionBody(bodyText)
|
oldBody.replace(JetPsiFactory(func).createFunctionBody(bodyText))
|
||||||
func.getBodyExpression()!!.replace(newBodyExpression)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupCallTypeArguments(callElement: JetCallElement, typeParameters: List<TypeParameterDescriptor>) {
|
private fun setupCallTypeArguments(callElement: JetCallElement, typeParameters: List<TypeParameterDescriptor>) {
|
||||||
|
|||||||
+11
@@ -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)
|
||||||
|
}
|
||||||
+7
@@ -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);
|
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")
|
@TestMetadata("beforeFunExtraArgs.kt")
|
||||||
public void testFunExtraArgs() throws Exception {
|
public void testFunExtraArgs() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunExtraArgs.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunExtraArgs.kt");
|
||||||
@@ -1570,6 +1576,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("beforeFunOnUserObject.kt")
|
||||||
public void testFunOnUserObject() throws Exception {
|
public void testFunOnUserObject() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserObject.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeFunOnUserObject.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user