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,
declaration: D,
marker: RangeMarker,
exprText: String
exprText: String?
): JetExpression? {
if (!declaration.isValid()) return null
if (exprText == null || !declaration.isValid()) return null
return containingFile
.findElementAt(marker.getStartOffset())
@@ -180,8 +180,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
Pass<OccurrencesChooser.ReplaceChoice> callback = new Pass<OccurrencesChooser.ReplaceChoice>() {
@Override
public void pass(OccurrencesChooser.ReplaceChoice replaceChoice) {
boolean replaceOccurrence =
container != expression.getParent() || BindingContextUtilPackage.isUsedAsExpression(expression, bindingContext);
boolean replaceOccurrence = shouldReplaceOccurrence(expression, bindingContext, container);
List<JetExpression> allReplaces;
if (OccurrencesChooser.ReplaceChoice.ALL == replaceChoice) {
if (allOccurrences.size() > 1) replaceOccurrence = true;
@@ -415,17 +414,21 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
}
}
}
for (JetExpression replace : allReplaces) {
if (replaceOccurrence && !needBraces) {
replaceExpression(replace);
}
else if (!needBraces) {
PsiElement sibling = PsiTreeUtil.skipSiblingsBackward(replace, PsiWhiteSpace.class);
if (sibling == property) {
replace.getParent().deleteChildRange(property.getNextSibling(), replace);
if (!needBraces) {
for (int i = 0; i < allReplaces.size(); i++) {
JetExpression replace = allReplaces.get(i);
if (i != 0 ? replaceOccurrence : shouldReplaceOccurrence(replace, bindingContext, commonContainer)) {
replaceExpression(replace);
}
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
private static PsiElement getContainer(PsiElement place) {
if (place instanceof JetBlockExpression || place instanceof JetClassBody ||
@@ -1,6 +1,5 @@
fun a() {
val i = 1
i
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);
}
@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")
public void testWhenAddBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/WhenAddBlock.kt");