From 020ea99220ef444ff28456d3340009a05b01b4a2 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 22 Jan 2014 19:52:58 +0400 Subject: [PATCH] Intention "Convert to expression body" --- .../codeInsight/intention/annotations.xml | 8 ++ .../jetbrains/jet/lang/psi/JetPsiFactory.java | 14 +- .../jet/generators/tests/GenerateTests.kt | 5 + idea/src/META-INF/plugin.xml | 5 + .../jetbrains/jet/plugin/JetBundle.properties | 2 + .../ConvertToExpressionBodyAction.kt | 105 +++++++++++++++ .../afterAnonymousObjectExpression.kt | 10 ++ .../afterFunWithImplicitUnitTypeWithThrow.kt | 2 + .../afterFunWithNothingType.kt | 2 + .../afterFunWithReturn.kt | 2 + .../afterFunWithUnitType.kt | 4 + .../afterFunWithUnitTypeWithThrow.kt | 2 + .../afterGetWithReturn.kt | 3 + .../beforeAnonymousObjectExpression.kt | 12 ++ .../beforeAssignment.kt | 8 ++ .../beforeDeclaration.kt | 5 + .../beforeExpressionWithReturns1.kt | 7 + .../beforeExpressionWithReturns2.kt | 10 ++ .../beforeFunWithImplicitUnitTypeWithThrow.kt | 4 + .../beforeFunWithNoBlock.kt | 3 + .../beforeFunWithNothingType.kt | 4 + .../beforeFunWithReturn.kt | 4 + .../beforeFunWithUnitType.kt | 6 + .../beforeFunWithUnitType2.kt | 7 + .../beforeFunWithUnitTypeWithThrow.kt | 4 + .../beforeFunctionLiteral.kt | 9 ++ .../beforeGetWithReturn.kt | 5 + .../beforeMultipleStatements.kt | 6 + .../beforeReturnWithNoValue.kt | 5 + .../convertToExpressionBody/beforeWhile.kt | 5 + .../intentions/AbstractIntentionTest.java | 6 + .../ConvertToExpressionBodyTestGenerated.java | 124 ++++++++++++++++++ 32 files changed, 391 insertions(+), 7 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterAnonymousObjectExpression.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterFunWithImplicitUnitTypeWithThrow.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterFunWithNothingType.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterFunWithReturn.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterFunWithUnitType.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterFunWithUnitTypeWithThrow.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/afterGetWithReturn.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeAnonymousObjectExpression.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeAssignment.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeDeclaration.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns1.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns2.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithImplicitUnitTypeWithThrow.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithNoBlock.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithNothingType.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithReturn.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType2.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitTypeWithThrow.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeFunctionLiteral.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeGetWithReturn.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeMultipleStatements.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeReturnWithNoValue.kt create mode 100644 idea/testData/intentions/convertToExpressionBody/beforeWhile.kt create mode 100644 idea/tests/org/jetbrains/jet/plugin/intentions/AbstractIntentionTest.java create mode 100644 idea/tests/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyTestGenerated.java diff --git a/annotations/com/intellij/codeInsight/intention/annotations.xml b/annotations/com/intellij/codeInsight/intention/annotations.xml index 016e141fc58..38d7e6786b1 100644 --- a/annotations/com/intellij/codeInsight/intention/annotations.xml +++ b/annotations/com/intellij/codeInsight/intention/annotations.xml @@ -11,4 +11,12 @@ + + + + + + \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java index 9d5c1012139..5b24677e272 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java @@ -95,13 +95,6 @@ public class JetPsiFactory { return comma; } - @NotNull - public static PsiElement createEQ(Project project) { - PsiElement eq = createFunction(project, "fun foo() = foo").getEqualsToken(); - assert eq != null; - return eq; - } - @NotNull public static PsiElement createColon(Project project) { JetProperty property = createProperty(project, "val x: Int"); @@ -110,6 +103,13 @@ public class JetPsiFactory { return colon; } + @NotNull + public static PsiElement createEQ(Project project) { + PsiElement eq = createFunction(project, "fun foo() = foo").getEqualsToken(); + assert eq != null; + return eq; + } + @NotNull public static PsiElement createSemicolon(Project project) { JetProperty property = createProperty(project, "val x: Int;"); diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index 1b7de86cbc9..51caf66dfd9 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -88,6 +88,7 @@ import org.jetbrains.jet.resolve.AbstractAdditionalLazyResolveDescriptorRenderer import org.jetbrains.jet.resolve.AbstractReferenceResolveInLibrarySourcesTest import org.jetbrains.jet.completion.AbstractCompiledKotlinInJavaCompletionTest import org.jetbrains.jet.completion.AbstractKotlinSourceInJavaCompletionTest +import org.jetbrains.jet.plugin.intentions.AbstractIntentionTest fun main(args: Array) { System.setProperty("java.awt.headless", "true") @@ -256,6 +257,10 @@ fun main(args: Array) { model("quickfix", pattern = "^before(\\w+)\\.kt$") } + testClass(javaClass(), "ConvertToExpressionBodyTestGenerated") { + model("intentions/convertToExpressionBody", pattern = "^before(\\w+)\\.kt$") + } + testClass(javaClass()) { model("completion/basic/common") model("completion/basic/js") diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 8fc710488b9..eef103c449d 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -371,6 +371,11 @@ Kotlin + + org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction + Kotlin + + org.jetbrains.jet.plugin.ktSignature.KotlinSignatureAnnotationIntention Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index a2c7ac630b0..3c206016d38 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -53,6 +53,8 @@ specify.type.explicitly.action.family.name=Specify Type Explicitly specify.type.explicitly.add.return.type.action.name=Specify return type explicitly specify.type.explicitly.add.action.name=Specify type explicitly specify.type.explicitly.remove.action.name=Remove explicitly specified type +convert.to.expression.body.action.family.name=Convert to Expression Body +convert.to.expression.body.action.name=Convert to expression body rename.parameter.to.match.overridden.method=Rename parameter to match overridden method rename.family=Rename rename.kotlin.package.class.error="Can't rename kotlin package class" diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt b/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt new file mode 100644 index 00000000000..ec8536752c4 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt @@ -0,0 +1,105 @@ +package org.jetbrains.jet.plugin.intentions + +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction +import com.intellij.openapi.project.Project +import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.psi.JetBlockExpression +import org.jetbrains.jet.plugin.JetBundle +import org.jetbrains.jet.lang.psi.* +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.types.TypeUtils +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.lang.types.JetType + +public class ConvertToExpressionBodyAction : PsiElementBaseIntentionAction() { + override fun getFamilyName(): String = JetBundle.message("convert.to.expression.body.action.family.name") + + override fun isAvailable(project: Project, editor: Editor, element: PsiElement): Boolean { + setText(JetBundle.message("convert.to.expression.body.action.name")) + val data = calcData(element) + return data != null && !containsReturn(data.value) + } + + override fun invoke(project: Project, editor: Editor, element: PsiElement) { + val (declaration, value) = calcData(element)!! + + if (!declaration.hasDeclaredReturnType() && declaration is JetNamedFunction) { + val valueType = expressionType(value) + if (valueType == null || !KotlinBuiltIns.getInstance().isUnit(valueType)) { + specifyUnitTypeExplicitly(declaration) + } + } + + val body = declaration.getBodyExpression()!! + declaration.addBefore(JetPsiFactory.createEQ(project), body) + body.replace(value) + } + + private data class Data(val declaration: JetDeclarationWithBody, val value: JetExpression) + + private fun calcData(element: PsiElement): Data? { + val declaration = PsiTreeUtil.getParentOfType(element, javaClass()) + if (declaration == null || declaration is JetFunctionLiteral) return null + val body = declaration.getBodyExpression() + if (!declaration.hasBlockBody() || body !is JetBlockExpression) return null + + val statements = body.getStatements() + if (statements.size != 1) return null + val statement = statements[0] + return when(statement) { + is JetReturnExpression -> { + val value = statement.getReturnedExpression() + if (value != null) Data(declaration, value) else null + } + + //TODO: IMO this is not good code, there should be a way to detect that JetExpression does not have value + is JetDeclaration -> null // is JetExpression but does not have value + is JetLoopExpression -> null // is JetExpression but does not have value + + is JetExpression -> { + if (statement is JetBinaryExpression && statement.getOperationToken() == JetTokens.EQ) return null // assignment does not have value + + val expressionType = expressionType(statement) + if (expressionType != null && + (KotlinBuiltIns.getInstance().isUnit(expressionType) || KotlinBuiltIns.getInstance().isNothing(expressionType))) + Data(declaration, statement) + else + null + } + + else -> null + } + } + + private fun expressionType(expression: JetExpression): JetType? { + val resolveSession = AnalyzerFacadeWithCache.getLazyResolveSessionForFile(expression.getContainingFile() as JetFile) + val bindingContext = resolveSession.resolveToElement(expression) + return bindingContext.get(BindingContext.EXPRESSION_TYPE, expression) + } + + private fun specifyUnitTypeExplicitly(declaration: JetNamedFunction) { + val project = declaration.getProject() + val typeReference = JetPsiFactory.createType(project, "Unit") + val anchor = declaration.getValueParameterList() ?: return/*incomplete declaration*/ + declaration.addAfter(typeReference, anchor) + declaration.addAfter(JetPsiFactory.createColon(project), anchor) + } + + private fun containsReturn(element: PsiElement): Boolean { + if (element is JetReturnExpression) return true + //TODO: would be better to have some interface of declaration where return can be used + if (element is JetNamedFunction || element is JetPropertyAccessor) return false // can happen inside + + var child = element.getFirstChild() + while (child != null) { + if (containsReturn(child!!)) return true + child = child!!.getNextSibling() + } + + return false + } +} diff --git a/idea/testData/intentions/convertToExpressionBody/afterAnonymousObjectExpression.kt b/idea/testData/intentions/convertToExpressionBody/afterAnonymousObjectExpression.kt new file mode 100644 index 00000000000..61f5253b84c --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterAnonymousObjectExpression.kt @@ -0,0 +1,10 @@ +// "Convert to expression body" "true" +trait I { + fun foo(): String +} + +fun bar(): I = object: I { + override fun foo(): String { + return "a" + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/afterFunWithImplicitUnitTypeWithThrow.kt b/idea/testData/intentions/convertToExpressionBody/afterFunWithImplicitUnitTypeWithThrow.kt new file mode 100644 index 00000000000..979286879a2 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterFunWithImplicitUnitTypeWithThrow.kt @@ -0,0 +1,2 @@ +// "Convert to expression body" "true" +fun foo(): Unit = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/afterFunWithNothingType.kt b/idea/testData/intentions/convertToExpressionBody/afterFunWithNothingType.kt new file mode 100644 index 00000000000..c6631a2d179 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterFunWithNothingType.kt @@ -0,0 +1,2 @@ +// "Convert to expression body" "true" +fun foo(): Nothing = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/afterFunWithReturn.kt b/idea/testData/intentions/convertToExpressionBody/afterFunWithReturn.kt new file mode 100644 index 00000000000..0c237529970 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterFunWithReturn.kt @@ -0,0 +1,2 @@ +// "Convert to expression body" "true" +fun foo(): String = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitType.kt b/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitType.kt new file mode 100644 index 00000000000..451eacac56d --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitType.kt @@ -0,0 +1,4 @@ +// "Convert to expression body" "true" +fun foo() = bar() + +fun bar() { } \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitTypeWithThrow.kt b/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitTypeWithThrow.kt new file mode 100644 index 00000000000..979286879a2 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterFunWithUnitTypeWithThrow.kt @@ -0,0 +1,2 @@ +// "Convert to expression body" "true" +fun foo(): Unit = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/afterGetWithReturn.kt b/idea/testData/intentions/convertToExpressionBody/afterGetWithReturn.kt new file mode 100644 index 00000000000..8606345e795 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/afterGetWithReturn.kt @@ -0,0 +1,3 @@ +// "Convert to expression body" "true" +val foo: String + get() = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeAnonymousObjectExpression.kt b/idea/testData/intentions/convertToExpressionBody/beforeAnonymousObjectExpression.kt new file mode 100644 index 00000000000..e93c095665f --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeAnonymousObjectExpression.kt @@ -0,0 +1,12 @@ +// "Convert to expression body" "true" +trait I { + fun foo(): String +} + +fun bar(): I { + return object: I { + override fun foo(): String { + return "a" + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeAssignment.kt b/idea/testData/intentions/convertToExpressionBody/beforeAssignment.kt new file mode 100644 index 00000000000..60191c9e7ea --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeAssignment.kt @@ -0,0 +1,8 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +var a = 1 +var b = 2 + +fun foo() { + a = b +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeDeclaration.kt b/idea/testData/intentions/convertToExpressionBody/beforeDeclaration.kt new file mode 100644 index 00000000000..017b0d53f58 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeDeclaration.kt @@ -0,0 +1,5 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo() { + val v = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns1.kt b/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns1.kt new file mode 100644 index 00000000000..7026a9bfb12 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns1.kt @@ -0,0 +1,7 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo(p: Boolean): String { + return bar() ?: return "a" +} + +fun bar(): String? = null \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns2.kt b/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns2.kt new file mode 100644 index 00000000000..48357f1d5eb --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns2.kt @@ -0,0 +1,10 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo(p: Boolean): String { + if (p) { + return "abc" + } + else { + return "def" + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithImplicitUnitTypeWithThrow.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithImplicitUnitTypeWithThrow.kt new file mode 100644 index 00000000000..7bb3b998749 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithImplicitUnitTypeWithThrow.kt @@ -0,0 +1,4 @@ +// "Convert to expression body" "true" +fun foo() { + throw UnsupportedOperationException() +} diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithNoBlock.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithNoBlock.kt new file mode 100644 index 00000000000..e489d096904 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithNoBlock.kt @@ -0,0 +1,3 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo() = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithNothingType.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithNothingType.kt new file mode 100644 index 00000000000..fa1eae93a9e --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithNothingType.kt @@ -0,0 +1,4 @@ +// "Convert to expression body" "true" +fun foo(): Nothing { + throw UnsupportedOperationException() +} diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithReturn.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithReturn.kt new file mode 100644 index 00000000000..340ff584cc0 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithReturn.kt @@ -0,0 +1,4 @@ +// "Convert to expression body" "true" +fun foo(): String { + return "abc" +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType.kt new file mode 100644 index 00000000000..7cf6c2e0694 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType.kt @@ -0,0 +1,6 @@ +// "Convert to expression body" "true" +fun foo() { + bar() +} + +fun bar() { } \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType2.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType2.kt new file mode 100644 index 00000000000..3a3cd361eac --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType2.kt @@ -0,0 +1,7 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo() { + bar() +} + +fun bar(): String = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitTypeWithThrow.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitTypeWithThrow.kt new file mode 100644 index 00000000000..5f4a30c6f4a --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitTypeWithThrow.kt @@ -0,0 +1,4 @@ +// "Convert to expression body" "true" +fun foo(): Unit { + throw UnsupportedOperationException() +} diff --git a/idea/testData/intentions/convertToExpressionBody/beforeFunctionLiteral.kt b/idea/testData/intentions/convertToExpressionBody/beforeFunctionLiteral.kt new file mode 100644 index 00000000000..d46b2818f18 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeFunctionLiteral.kt @@ -0,0 +1,9 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo(handler: () -> Unit) { } + +fun bar() { + foo { zoo() } +} + +fun zoo(){} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeGetWithReturn.kt b/idea/testData/intentions/convertToExpressionBody/beforeGetWithReturn.kt new file mode 100644 index 00000000000..75f403b5c7d --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeGetWithReturn.kt @@ -0,0 +1,5 @@ +// "Convert to expression body" "true" +val foo: String + get() { + return "abc" + } \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeMultipleStatements.kt b/idea/testData/intentions/convertToExpressionBody/beforeMultipleStatements.kt new file mode 100644 index 00000000000..e4686a6269b --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeMultipleStatements.kt @@ -0,0 +1,6 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo(): String { + val v = 1 + return "abc" +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeReturnWithNoValue.kt b/idea/testData/intentions/convertToExpressionBody/beforeReturnWithNoValue.kt new file mode 100644 index 00000000000..cf3a5061950 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeReturnWithNoValue.kt @@ -0,0 +1,5 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo() { + return +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/beforeWhile.kt b/idea/testData/intentions/convertToExpressionBody/beforeWhile.kt new file mode 100644 index 00000000000..d76eece4ca3 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/beforeWhile.kt @@ -0,0 +1,5 @@ +// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false" + +fun foo(p: Boolean): String { + while(true) { } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractIntentionTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractIntentionTest.java new file mode 100644 index 00000000000..9ef3d342bda --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractIntentionTest.java @@ -0,0 +1,6 @@ +package org.jetbrains.jet.plugin.intentions; + +import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest; + +public abstract class AbstractIntentionTest extends AbstractQuickFixTest{ +} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyTestGenerated.java new file mode 100644 index 00000000000..db4cfcb113e --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyTestGenerated.java @@ -0,0 +1,124 @@ +/* + * Copyright 2010-2013 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 junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.plugin.intentions.AbstractIntentionTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/intentions/convertToExpressionBody") +public class ConvertToExpressionBodyTestGenerated extends AbstractIntentionTest { + public void testAllFilesPresentInConvertToExpressionBody() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/convertToExpressionBody"), Pattern.compile("^before(\\w+)\\.kt$"), true); + } + + @TestMetadata("beforeAnonymousObjectExpression.kt") + public void testAnonymousObjectExpression() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeAnonymousObjectExpression.kt"); + } + + @TestMetadata("beforeAssignment.kt") + public void testAssignment() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeAssignment.kt"); + } + + @TestMetadata("beforeDeclaration.kt") + public void testDeclaration() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeDeclaration.kt"); + } + + @TestMetadata("beforeExpressionWithReturns1.kt") + public void testExpressionWithReturns1() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns1.kt"); + } + + @TestMetadata("beforeExpressionWithReturns2.kt") + public void testExpressionWithReturns2() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeExpressionWithReturns2.kt"); + } + + @TestMetadata("beforeFunWithImplicitUnitTypeWithThrow.kt") + public void testFunWithImplicitUnitTypeWithThrow() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithImplicitUnitTypeWithThrow.kt"); + } + + @TestMetadata("beforeFunWithNoBlock.kt") + public void testFunWithNoBlock() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithNoBlock.kt"); + } + + @TestMetadata("beforeFunWithNothingType.kt") + public void testFunWithNothingType() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithNothingType.kt"); + } + + @TestMetadata("beforeFunWithReturn.kt") + public void testFunWithReturn() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithReturn.kt"); + } + + @TestMetadata("beforeFunWithUnitType.kt") + public void testFunWithUnitType() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType.kt"); + } + + @TestMetadata("beforeFunWithUnitType2.kt") + public void testFunWithUnitType2() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitType2.kt"); + } + + @TestMetadata("beforeFunWithUnitTypeWithThrow.kt") + public void testFunWithUnitTypeWithThrow() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunWithUnitTypeWithThrow.kt"); + } + + @TestMetadata("beforeFunctionLiteral.kt") + public void testFunctionLiteral() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeFunctionLiteral.kt"); + } + + @TestMetadata("beforeGetWithReturn.kt") + public void testGetWithReturn() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeGetWithReturn.kt"); + } + + @TestMetadata("beforeMultipleStatements.kt") + public void testMultipleStatements() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeMultipleStatements.kt"); + } + + @TestMetadata("beforeReturnWithNoValue.kt") + public void testReturnWithNoValue() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeReturnWithNoValue.kt"); + } + + @TestMetadata("beforeWhile.kt") + public void testWhile() throws Exception { + doTest("idea/testData/intentions/convertToExpressionBody/beforeWhile.kt"); + } + +}