Introduce Variable: Do not delete original expression if its value is used in enclosing expression

This commit is contained in:
Alexey Sedunov
2014-12-22 17:22:42 +03:00
parent 6084352d37
commit e76792d4f2
7 changed files with 40 additions and 1 deletions
@@ -177,7 +177,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
@Override
public void pass(OccurrencesChooser.ReplaceChoice replaceChoice) {
boolean replaceOccurrence =
container != expression.getParent() || container instanceof JetNamedFunction;
container != expression.getParent() || BindingContextUtilPackage.isUsedAsExpression(expression, bindingContext);
List<JetExpression> allReplaces;
if (OccurrencesChooser.ReplaceChoice.ALL == replaceChoice) {
if (allOccurrences.size() > 1) replaceOccurrence = true;
@@ -0,0 +1,3 @@
fun a(x: Int) {
val t = if (x > 0) <selection>x + x</selection> - 1 else x * x + 1
}
@@ -0,0 +1,6 @@
fun a(x: Int) {
val t = if (x > 0) {
val i = x + x
i - 1
} else x * x + 1
}
@@ -3,5 +3,6 @@ fun b() {
a {it}
a {
val i = it
i
}
}
@@ -0,0 +1,7 @@
fun a(x: Int) {
val t = when {
x > 0 -> <selection>x * x</selection> + 1
x < 0 -> x + x
else -> 0
}
}
@@ -0,0 +1,10 @@
fun a(x: Int) {
val t = when {
x > 0 -> {
val i = x * x
i + 1
}
x < 0 -> x + x
else -> 0
}
}
@@ -129,6 +129,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest(fileName);
}
@TestMetadata("IfThenValuedAddBlock.kt")
public void testIfThenValuedAddBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/IfThenValuedAddBlock.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("It.kt")
public void testIt() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/It.kt");
@@ -249,6 +255,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest(fileName);
}
@TestMetadata("WhenValuedAddBlock.kt")
public void testWhenValuedAddBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/WhenValuedAddBlock.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("WhileAddBlock.kt")
public void testWhileAddBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/WhileAddBlock.kt");