Extract Function: Do not extract into lambda

This commit is contained in:
Alexey Sedunov
2014-07-04 18:21:13 +04:00
parent 0c49d48a48
commit 30192b4c40
32 changed files with 249 additions and 12 deletions
@@ -180,8 +180,9 @@ fun selectElements(
fun getContainers(element: PsiElement, strict: Boolean): List<JetElement> {
if (allContainersEnabled) return element.getAllExtractionContainers(strict)
val declaration = element.getParentByType(javaClass<JetDeclaration>(), strict)
if (declaration == null) return Collections.emptyList()
val declaration = element.getParentByType(javaClass<JetDeclaration>(), strict)?.let { declaration ->
stream(declaration) { it.getParentByType(javaClass<JetDeclaration>(), true) }.firstOrNull { it !is JetFunctionLiteral }
} ?: return Collections.emptyList()
val parent = declaration.getParent()?.let {
when (it) {
@@ -1,3 +1,5 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A {
fun foo(a: Int, b: Int): Int {
return {
@@ -1,11 +1,13 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A {
fun foo(a: Int, b: Int): Int {
fun i(): Int {
return a + b - 1
}
return {
i()
i(a, b)
}.invoke()
}
fun i(a: Int, b: Int): Int {
return a + b - 1
}
}
@@ -0,0 +1,9 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A {
fun foo(a: Int, b: Int): Int {
return {
{ <selection>a + b - 1</selection> }.invoke()
}.invoke()
}
}
@@ -0,0 +1,13 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A {
fun foo(a: Int, b: Int): Int {
return {
{ i(a, b) }.invoke()
}.invoke()
}
fun i(a: Int, b: Int): Int {
return a + b - 1
}
}
@@ -0,0 +1,3 @@
class A(val a: Int, val b: Int) {
val foo: Int get() = { <selection>a + b</selection> - 1 }.invoke()
}
@@ -0,0 +1,7 @@
class A(val a: Int, val b: Int) {
val foo: Int get() = { i() - 1 }.invoke()
fun i(): Int {
return a + b
}
}
@@ -0,0 +1,3 @@
val a = 1
val b = 1
val foo: Int get() = { <selection>a + b</selection> - 1 }.invoke()
@@ -0,0 +1,7 @@
val a = 1
val b = 1
val foo: Int get() = { i() - 1 }.invoke()
fun i(): Int {
return a + b
}
@@ -0,0 +1,6 @@
// PARAM_TYPES: kotlin.Int
class A(val a: Int, b: Int) {
{
println({ <selection>a + b</selection> - 1 }.invoke())
}
}
@@ -0,0 +1,10 @@
// PARAM_TYPES: kotlin.Int
class A(val a: Int, b: Int) {
{
println({ i(b) - 1 }.invoke())
}
fun i(b: Int): Int {
return a + b
}
}
@@ -0,0 +1,3 @@
// PARAM_TYPES: kotlin.Int
val n = 1
class A(val a: Int, val b: Int = { <selection>a + n</selection> }.invoke())
@@ -0,0 +1,7 @@
// PARAM_TYPES: kotlin.Int
val n = 1
class A(val a: Int, val b: Int = { i(a) }.invoke())
fun i(a: Int): Int {
return a + n
}
@@ -0,0 +1,4 @@
// PARAM_TYPES: kotlin.Int
fun bar(n: Int) {
fun foo(a: Int, b: Int = { <selection>a + n</selection> }.invoke()) = a + b - n - 1
}
@@ -0,0 +1 @@
Cannot extract method since following types are not denotable in the target scope: [ERROR : ]
@@ -0,0 +1,5 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
fun bar(n: Int) {
fun foo(a: Int, b: Int) = { <selection>a + b - n</selection> - 1 }.invoke()
}
@@ -0,0 +1,9 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
fun bar(n: Int) {
fun i(a: Int, b: Int): Int {
return a + b - n
}
fun foo(a: Int, b: Int) = { i(a, b) - 1 }.invoke()
}
@@ -0,0 +1,4 @@
// PARAM_TYPES: kotlin.Int
class A(val n: Int) {
fun foo(a: Int, b: Int = { <selection>a + n</selection> }.invoke()) = a + b - n - 1
}
@@ -0,0 +1,8 @@
// PARAM_TYPES: kotlin.Int
class A(val n: Int) {
fun foo(a: Int, b: Int = { i(a) }.invoke()) = a + b - n - 1
fun i(a: Int): Int {
return a + n
}
}
@@ -0,0 +1,5 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A(val n: Int) {
fun foo(a: Int, b: Int) = { <selection>a + b - n</selection> - 1 }.invoke()
}
@@ -0,0 +1,9 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
class A(val n: Int) {
fun foo(a: Int, b: Int) = { i(a, b) - 1 }.invoke()
fun i(a: Int, b: Int): Int {
return a + b - n
}
}
@@ -0,0 +1,3 @@
// PARAM_TYPES: kotlin.Int
val n = 1
fun foo(a: Int, b: Int = { <selection>a + n</selection> }) = a + b - 1
@@ -0,0 +1,7 @@
// PARAM_TYPES: kotlin.Int
val n = 1
fun foo(a: Int, b: Int = { i(a) }) = a + b - 1
fun i(a: Int): Int {
return a + n
}
@@ -0,0 +1,3 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
fun foo(a: Int, b: Int) = { <selection>a + b</selection> - 1 }.invoke()
@@ -0,0 +1,7 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
fun foo(a: Int, b: Int) = { i(a, b) - 1 }.invoke()
fun i(a: Int, b: Int): Int {
return a + b
}
@@ -0,0 +1,3 @@
fun bar(a: Int, b: Int) {
val foo = { <selection>a + b</selection> - 1 }.invoke()
}
@@ -0,0 +1,7 @@
fun bar(a: Int, b: Int) {
fun i(): Int {
return a + b
}
val foo = { i() - 1 }.invoke()
}
@@ -0,0 +1,4 @@
// PARAM_TYPES: kotlin.Int
class A(val a: Int, b: Int) {
val foo = { <selection>a + b</selection> - 1 }.invoke()
}
@@ -0,0 +1,8 @@
// PARAM_TYPES: kotlin.Int
class A(val a: Int, b: Int) {
val foo = { i(b) - 1 }.invoke()
fun i(b: Int): Int {
return a + b
}
}
@@ -0,0 +1,3 @@
val a = 1
val b = 1
val foo = { <selection>a + b</selection> - 1 }.invoke()
@@ -0,0 +1,7 @@
val a = 1
val b = 1
val foo = { i() - 1 }.invoke()
fun i(): Int {
return a + b
}
@@ -16,17 +16,14 @@
package org.jetbrains.jet.plugin.refactoring.introduce.introduceVariable;
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.refactoring.introduce.introduceVariable.AbstractJetExtractionTest;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@@ -678,6 +675,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/defaultContainer/localFunction.kt");
}
@TestMetadata("nestedLambda.kt")
public void testNestedLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/defaultContainer/nestedLambda.kt");
}
@TestMetadata("topLevelFunction.kt")
public void testTopLevelFunction() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/defaultContainer/topLevelFunction.kt");
@@ -703,11 +705,21 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/accessors/memberProperty.kt");
}
@TestMetadata("memberPropertyWithLambda.kt")
public void testMemberPropertyWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/accessors/memberPropertyWithLambda.kt");
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/accessors/topLevelProperty.kt");
}
@TestMetadata("topLevelPropertyWithLambda.kt")
public void testTopLevelPropertyWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/accessors/topLevelPropertyWithLambda.kt");
}
}
@TestMetadata("idea/testData/refactoring/extractFunction/initializers/classes")
@@ -721,11 +733,21 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/classes/classInitializer.kt");
}
@TestMetadata("classInitializerWithLambda.kt")
public void testClassInitializerWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/classes/classInitializerWithLambda.kt");
}
@TestMetadata("classParameters.kt")
public void testClassParameters() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/classes/classParameters.kt");
}
@TestMetadata("classParametersWithLambda.kt")
public void testClassParametersWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/classes/classParametersWithLambda.kt");
}
}
@TestMetadata("idea/testData/refactoring/extractFunction/initializers/functions")
@@ -744,6 +766,16 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/localFunctionParameters.kt");
}
@TestMetadata("localFunctionParametersWithLambda.kt")
public void testLocalFunctionParametersWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/localFunctionParametersWithLambda.kt");
}
@TestMetadata("localFunctionWithLambda.kt")
public void testLocalFunctionWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/localFunctionWithLambda.kt");
}
@TestMetadata("memberFunction.kt")
public void testMemberFunction() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/memberFunction.kt");
@@ -754,6 +786,16 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/memberFunctionParameters.kt");
}
@TestMetadata("memberFunctionParametersWithLambda.kt")
public void testMemberFunctionParametersWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/memberFunctionParametersWithLambda.kt");
}
@TestMetadata("memberFunctionWithLambda.kt")
public void testMemberFunctionWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/memberFunctionWithLambda.kt");
}
@TestMetadata("topLevelFunction.kt")
public void testTopLevelFunction() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/topLevelFunction.kt");
@@ -764,6 +806,16 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/topLevelFunctionParameters.kt");
}
@TestMetadata("topLevelFunctionParametersWithLambda.kt")
public void testTopLevelFunctionParametersWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/topLevelFunctionParametersWithLambda.kt");
}
@TestMetadata("topLevelFunctionWithLambda.kt")
public void testTopLevelFunctionWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/functions/topLevelFunctionWithLambda.kt");
}
}
@TestMetadata("idea/testData/refactoring/extractFunction/initializers/properties")
@@ -777,16 +829,31 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/localProperty.kt");
}
@TestMetadata("localPropertyWithLambda.kt")
public void testLocalPropertyWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/localPropertyWithLambda.kt");
}
@TestMetadata("memberProperty.kt")
public void testMemberProperty() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/memberProperty.kt");
}
@TestMetadata("memberPropertyWithLambda.kt")
public void testMemberPropertyWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/memberPropertyWithLambda.kt");
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/topLevelProperty.kt");
}
@TestMetadata("topLevelPropertyWithLambda.kt")
public void testTopLevelPropertyWithLambda() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/initializers/properties/topLevelPropertyWithLambda.kt");
}
}
public static Test innerSuite() {