Introduce Variable: Do not insert unused variable references

#KT-7227 Fixed
This commit is contained in:
Alexey Sedunov
2015-06-16 17:49:42 +03:00
parent 1506531e59
commit 683a8895df
8 changed files with 76 additions and 14 deletions
@@ -72,9 +72,9 @@ public abstract class AbstractKotlinInplaceIntroducer<D: JetNamedDeclaration>(
containingFile: PsiFile, containingFile: PsiFile,
declaration: D, declaration: D,
marker: RangeMarker, marker: RangeMarker,
exprText: String exprText: String?
): JetExpression? { ): JetExpression? {
if (!declaration.isValid()) return null if (exprText == null || !declaration.isValid()) return null
return containingFile return containingFile
.findElementAt(marker.getStartOffset()) .findElementAt(marker.getStartOffset())
@@ -180,8 +180,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
Pass<OccurrencesChooser.ReplaceChoice> callback = new Pass<OccurrencesChooser.ReplaceChoice>() { Pass<OccurrencesChooser.ReplaceChoice> callback = new Pass<OccurrencesChooser.ReplaceChoice>() {
@Override @Override
public void pass(OccurrencesChooser.ReplaceChoice replaceChoice) { public void pass(OccurrencesChooser.ReplaceChoice replaceChoice) {
boolean replaceOccurrence = boolean replaceOccurrence = shouldReplaceOccurrence(expression, bindingContext, container);
container != expression.getParent() || BindingContextUtilPackage.isUsedAsExpression(expression, bindingContext);
List<JetExpression> allReplaces; List<JetExpression> allReplaces;
if (OccurrencesChooser.ReplaceChoice.ALL == replaceChoice) { if (OccurrencesChooser.ReplaceChoice.ALL == replaceChoice) {
if (allOccurrences.size() > 1) replaceOccurrence = true; if (allOccurrences.size() > 1) replaceOccurrence = true;
@@ -415,17 +414,21 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
} }
} }
} }
for (JetExpression replace : allReplaces) { if (!needBraces) {
if (replaceOccurrence && !needBraces) { for (int i = 0; i < allReplaces.size(); i++) {
replaceExpression(replace); JetExpression replace = allReplaces.get(i);
}
else if (!needBraces) { if (i != 0 ? replaceOccurrence : shouldReplaceOccurrence(replace, bindingContext, commonContainer)) {
PsiElement sibling = PsiTreeUtil.skipSiblingsBackward(replace, PsiWhiteSpace.class); replaceExpression(replace);
if (sibling == property) {
replace.getParent().deleteChildRange(property.getNextSibling(), replace);
} }
else { else {
replace.delete(); PsiElement sibling = PsiTreeUtil.skipSiblingsBackward(replace, PsiWhiteSpace.class);
if (sibling == property) {
replace.getParent().deleteChildRange(property.getNextSibling(), replace);
}
else {
replace.delete();
}
} }
} }
} }
@@ -515,6 +518,14 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
); );
} }
private static boolean shouldReplaceOccurrence(
@NotNull JetExpression expression,
@NotNull BindingContext bindingContext,
@Nullable PsiElement container
) {
return BindingContextUtilPackage.isUsedAsExpression(expression, bindingContext) || container != expression.getParent();
}
@Nullable @Nullable
private static PsiElement getContainer(PsiElement place) { private static PsiElement getContainer(PsiElement place) {
if (place instanceof JetBlockExpression || place instanceof JetClassBody || if (place instanceof JetBlockExpression || place instanceof JetClassBody ||
@@ -1,6 +1,5 @@
fun a() { fun a() {
val i = 1 val i = 1
i i
i
if (true) i if (true) i
} }
@@ -0,0 +1,10 @@
class A {
fun test() = 1
}
fun main(args: Array<String>) {
val a = A()
<selection>a.test()</selection>
val b = a.test()
}
@@ -0,0 +1,10 @@
class A {
fun test() = 1
}
fun main(args: Array<String>) {
val a = A()
val i = a.test()
val b = i
}
@@ -0,0 +1,10 @@
class A {
fun test() = 1
}
fun main(args: Array<String>) {
val a = A()
a.test()
val b = <selection>a.test()</selection>
}
@@ -0,0 +1,10 @@
class A {
fun test() = 1
}
fun main(args: Array<String>) {
val a = A()
val i = a.test()
val b = i
}
@@ -271,6 +271,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest(fileName); doIntroduceVariableTest(fileName);
} }
@TestMetadata("UsedAndUnusedExpressions1.kt")
public void testUsedAndUnusedExpressions1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/UsedAndUnusedExpressions1.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("UsedAndUnusedExpressions2.kt")
public void testUsedAndUnusedExpressions2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/UsedAndUnusedExpressions2.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("WhenAddBlock.kt") @TestMetadata("WhenAddBlock.kt")
public void testWhenAddBlock() throws Exception { public void testWhenAddBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/WhenAddBlock.kt"); String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/WhenAddBlock.kt");