Intention "Convert to expression body"

This commit is contained in:
Valentin Kipyatkov
2014-01-22 19:52:58 +04:00
parent d01ff28212
commit 020ea99220
32 changed files with 391 additions and 7 deletions
@@ -11,4 +11,12 @@
<val name="value" val="&quot;fun invoke(project: Project, editor: Editor, file: PsiFile): Unit&quot;"/>
</annotation>
</item>
<item
name='com.intellij.codeInsight.intention.PsiElementBaseIntentionAction boolean isAvailable(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiElement) 1'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item
name='com.intellij.codeInsight.intention.PsiElementBaseIntentionAction void invoke(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiElement) 1'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -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;");
@@ -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<String>) {
System.setProperty("java.awt.headless", "true")
@@ -256,6 +257,10 @@ fun main(args: Array<String>) {
model("quickfix", pattern = "^before(\\w+)\\.kt$")
}
testClass(javaClass<AbstractIntentionTest>(), "ConvertToExpressionBodyTestGenerated") {
model("intentions/convertToExpressionBody", pattern = "^before(\\w+)\\.kt$")
}
testClass(javaClass<AbstractJSBasicCompletionTest>()) {
model("completion/basic/common")
model("completion/basic/js")
+5
View File
@@ -371,6 +371,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.ktSignature.KotlinSignatureAnnotationIntention</className>
<category>Kotlin</category>
@@ -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"
@@ -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<JetDeclarationWithBody>())
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
}
}
@@ -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"
}
}
@@ -0,0 +1,2 @@
// "Convert to expression body" "true"
fun foo(): Unit = throw UnsupportedOperationException()
@@ -0,0 +1,2 @@
// "Convert to expression body" "true"
fun foo(): Nothing = throw UnsupportedOperationException()
@@ -0,0 +1,2 @@
// "Convert to expression body" "true"
fun foo(): String = "abc"
@@ -0,0 +1,4 @@
// "Convert to expression body" "true"
fun foo() = bar()
fun bar() { }
@@ -0,0 +1,2 @@
// "Convert to expression body" "true"
fun foo(): Unit = throw UnsupportedOperationException()
@@ -0,0 +1,3 @@
// "Convert to expression body" "true"
val foo: String
get() = "abc"
@@ -0,0 +1,12 @@
// "Convert to expression body" "true"
trait I {
fun foo(): String
}
fun bar(): I {
<caret>return object: I {
override fun foo(): String {
return "a"
}
}
}
@@ -0,0 +1,8 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
var a = 1
var b = 2
fun foo() {
<caret>a = b
}
@@ -0,0 +1,5 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo() {
<caret>val v = 1
}
@@ -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
@@ -0,0 +1,10 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo(p: Boolean): String {
if (p) {
<caret>return "abc"
}
else {
return "def"
}
}
@@ -0,0 +1,4 @@
// "Convert to expression body" "true"
fun foo() {
<caret>throw UnsupportedOperationException()
}
@@ -0,0 +1,3 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo() = <caret>"abc"
@@ -0,0 +1,4 @@
// "Convert to expression body" "true"
fun foo(): Nothing {
<caret>throw UnsupportedOperationException()
}
@@ -0,0 +1,4 @@
// "Convert to expression body" "true"
fun <caret>foo(): String {
return "abc"
}
@@ -0,0 +1,6 @@
// "Convert to expression body" "true"
fun foo() {
<caret>bar()
}
fun bar() { }
@@ -0,0 +1,7 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo() {
<caret>bar()
}
fun bar(): String = "abc"
@@ -0,0 +1,4 @@
// "Convert to expression body" "true"
fun foo(): Unit {
<caret>throw UnsupportedOperationException()
}
@@ -0,0 +1,9 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo(handler: () -> Unit) { }
fun bar() {
foo { <caret>zoo() }
}
fun zoo(){}
@@ -0,0 +1,5 @@
// "Convert to expression body" "true"
val foo: String
<caret>get() {
return "abc"
}
@@ -0,0 +1,6 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo(): String {
val v = 1
<caret>return "abc"
}
@@ -0,0 +1,5 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo() {
<caret>return
}
@@ -0,0 +1,5 @@
// "class org.jetbrains.jet.plugin.intentions.ConvertToExpressionBodyAction" "false"
fun foo(p: Boolean): String {
<caret>while(true) { }
}
@@ -0,0 +1,6 @@
package org.jetbrains.jet.plugin.intentions;
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
public abstract class AbstractIntentionTest extends AbstractQuickFixTest{
}
@@ -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");
}
}