Implement Unwrap/Remove for lambdas
This commit is contained in:
@@ -378,7 +378,8 @@ public class GenerateTests {
|
|||||||
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapThen", "doTestThenUnwrapper"),
|
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapThen", "doTestThenUnwrapper"),
|
||||||
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapElse", "doTestElseUnwrapper"),
|
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapElse", "doTestElseUnwrapper"),
|
||||||
testModel("idea/testData/codeInsight/unwrapAndRemove/removeElse", "doTestElseRemover"),
|
testModel("idea/testData/codeInsight/unwrapAndRemove/removeElse", "doTestElseRemover"),
|
||||||
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapLoop", "doTestLoopUnwrapper")
|
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapLoop", "doTestLoopUnwrapper"),
|
||||||
|
testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda", "doTestLambdaUnwrapper")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.codeInsight.unwrap;
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement;
|
||||||
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
|
import com.intellij.util.IncorrectOperationException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
|
||||||
|
public class KotlinLambdaUnwrapper extends KotlinUnwrapRemoveBase {
|
||||||
|
public KotlinLambdaUnwrapper(String key) {
|
||||||
|
super(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JetElement getLambdaEnclosingElement(@NotNull JetFunctionLiteralExpression lambda) {
|
||||||
|
PsiElement parent = lambda.getParent();
|
||||||
|
|
||||||
|
if (parent instanceof JetValueArgument) {
|
||||||
|
return PsiTreeUtil.getParentOfType(parent, JetCallExpression.class, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent instanceof JetCallExpression) {
|
||||||
|
return (JetElement) parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent instanceof JetProperty && ((JetProperty) parent).isLocal()) {
|
||||||
|
return (JetElement) parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lambda;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isApplicableTo(PsiElement e) {
|
||||||
|
if (!(e instanceof JetFunctionLiteralExpression)) return false;
|
||||||
|
|
||||||
|
JetFunctionLiteralExpression lambda = (JetFunctionLiteralExpression) e;
|
||||||
|
JetBlockExpression body = lambda.getBodyExpression();
|
||||||
|
JetElement enclosingElement = getLambdaEnclosingElement((JetFunctionLiteralExpression) e);
|
||||||
|
|
||||||
|
if (body == null || enclosingElement == null) return false;
|
||||||
|
|
||||||
|
return canExtractExpression(body, (JetElement)enclosingElement.getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
|
||||||
|
JetFunctionLiteralExpression lambda = (JetFunctionLiteralExpression) element;
|
||||||
|
JetBlockExpression body = lambda.getBodyExpression();
|
||||||
|
JetElement enclosingExpression = getLambdaEnclosingElement(lambda);
|
||||||
|
|
||||||
|
context.extractFromBlock(body, enclosingExpression);
|
||||||
|
context.delete(enclosingExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ public class KotlinUnwrapDescriptor extends UnwrapDescriptorBase {
|
|||||||
new KoitlinUnwrappers.KotlinElseRemover("remove.else"),
|
new KoitlinUnwrappers.KotlinElseRemover("remove.else"),
|
||||||
new KoitlinUnwrappers.KotlinElseUnwrapper("unwrap.else"),
|
new KoitlinUnwrappers.KotlinElseUnwrapper("unwrap.else"),
|
||||||
new KoitlinUnwrappers.KotlinLoopUnwrapper("unwrap.expression"),
|
new KoitlinUnwrappers.KotlinLoopUnwrapper("unwrap.expression"),
|
||||||
|
new KotlinLambdaUnwrapper("unwrap.expression"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
run(1, 2) <caret>{
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
fun foo() {
|
||||||
|
return run(1, 2) <caret>{
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
run(1, 2) <caret>{
|
||||||
|
println("lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
println("lambda")<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
run(<caret>{
|
||||||
|
println("lambda")
|
||||||
|
}, 1)
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
println("lambda")<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
return run(1, 2) <caret>{
|
||||||
|
println("lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
return println("lambda")<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// OPTION: 1
|
||||||
|
fun foo() {
|
||||||
|
<caret>{
|
||||||
|
println("lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 1
|
||||||
|
fun foo() {
|
||||||
|
<caret>println("lambda")
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
val x = <caret>{
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
val x = <caret>{
|
||||||
|
println("lambda")
|
||||||
|
}
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
val x = <caret>println("lambda")
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
val x = <caret>{
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
println("lambda")
|
||||||
|
println("another lambda")<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
val x = <caret>{
|
||||||
|
println("lambda")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// OPTION: 0
|
||||||
|
fun foo() {
|
||||||
|
println("lambda")<caret>
|
||||||
|
}
|
||||||
@@ -46,6 +46,10 @@ public abstract class AbstractUnwrapRemoveTest extends LightCodeInsightTestCase
|
|||||||
doTest(path, KoitlinUnwrappers.KotlinLoopUnwrapper.class);
|
doTest(path, KoitlinUnwrappers.KotlinLoopUnwrapper.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void doTestLambdaUnwrapper(@NotNull String path) throws Exception {
|
||||||
|
doTest(path, KotlinLambdaUnwrapper.class);
|
||||||
|
}
|
||||||
|
|
||||||
private void doTest(@NotNull String path, final Class<? extends Unwrapper> unwrapperClass) throws Exception {
|
private void doTest(@NotNull String path, final Class<? extends Unwrapper> unwrapperClass) throws Exception {
|
||||||
configureByFile(path);
|
configureByFile(path);
|
||||||
|
|
||||||
|
|||||||
+60
-1
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.codeInsight.unwrap.AbstractUnwrapRemoveTest;
|
|||||||
|
|
||||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@InnerTestClasses({UnwrapRemoveTestGenerated.UnwrapThen.class, UnwrapRemoveTestGenerated.UnwrapElse.class, UnwrapRemoveTestGenerated.RemoveElse.class, UnwrapRemoveTestGenerated.UnwrapLoop.class})
|
@InnerTestClasses({UnwrapRemoveTestGenerated.UnwrapThen.class, UnwrapRemoveTestGenerated.UnwrapElse.class, UnwrapRemoveTestGenerated.RemoveElse.class, UnwrapRemoveTestGenerated.UnwrapLoop.class, UnwrapRemoveTestGenerated.UnwrapLambda.class})
|
||||||
public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest {
|
public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest {
|
||||||
@TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapThen")
|
@TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapThen")
|
||||||
public static class UnwrapThen extends AbstractUnwrapRemoveTest {
|
public static class UnwrapThen extends AbstractUnwrapRemoveTest {
|
||||||
@@ -114,12 +114,71 @@ public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda")
|
||||||
|
public static class UnwrapLambda extends AbstractUnwrapRemoveTest {
|
||||||
|
public void testAllFilesPresentInUnwrapLambda() throws Exception {
|
||||||
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaCallCompoundInBlock.kt")
|
||||||
|
public void testLambdaCallCompoundInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaCallCompoundInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaCallCompoundInReturn.kt")
|
||||||
|
public void testLambdaCallCompoundInReturn() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaCallCompoundInReturn.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaCallInBlock.kt")
|
||||||
|
public void testLambdaCallInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaCallInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaCallInBlock2.kt")
|
||||||
|
public void testLambdaCallInBlock2() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaCallInBlock2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaCallSimpleInReturn.kt")
|
||||||
|
public void testLambdaCallSimpleInReturn() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaCallSimpleInReturn.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaInBlock.kt")
|
||||||
|
public void testLambdaInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaNonLocalPropertyCompoundInBlock.kt")
|
||||||
|
public void testLambdaNonLocalPropertyCompoundInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaNonLocalPropertyCompoundInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaNonLocalPropertyInBlock.kt")
|
||||||
|
public void testLambdaNonLocalPropertyInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaNonLocalPropertyInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaPropertyCompoundInBlock.kt")
|
||||||
|
public void testLambdaPropertyCompoundInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaPropertyCompoundInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaPropertyInBlock.kt")
|
||||||
|
public void testLambdaPropertyInBlock() throws Exception {
|
||||||
|
doTestLambdaUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda/lambdaPropertyInBlock.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite("UnwrapRemoveTestGenerated");
|
TestSuite suite = new TestSuite("UnwrapRemoveTestGenerated");
|
||||||
suite.addTestSuite(UnwrapThen.class);
|
suite.addTestSuite(UnwrapThen.class);
|
||||||
suite.addTestSuite(UnwrapElse.class);
|
suite.addTestSuite(UnwrapElse.class);
|
||||||
suite.addTestSuite(RemoveElse.class);
|
suite.addTestSuite(RemoveElse.class);
|
||||||
suite.addTestSuite(UnwrapLoop.class);
|
suite.addTestSuite(UnwrapLoop.class);
|
||||||
|
suite.addTestSuite(UnwrapLambda.class);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user