diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java b/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java
index 3208e0be9c9..45b5009c20b 100644
--- a/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java
+++ b/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java
@@ -208,9 +208,70 @@ public class JetIntroduceVariableHandler extends JetIntroduceHandlerBase {
JetExpression emptyBody = JetPsiFactory.createEmptyBody(project);
PsiElement firstChild = emptyBody.getFirstChild();
emptyBody.addAfter(JetPsiFactory.createWhiteSpace(project, "\n"), firstChild);
- property = (JetProperty) emptyBody.addAfter(property, firstChild);
- emptyBody.addAfter(JetPsiFactory.createWhiteSpace(project, "\n"), firstChild);
- emptyBody = (JetExpression) anchor.replace(emptyBody);
+ if (replaceOccurrence && commonContainer != null) {
+ for (JetExpression replace : allReplaces) {
+ boolean isActualExpression = expression == replace;
+ JetExpression element = (JetExpression) replace.replace(JetPsiFactory.createExpression(project, suggestedNames[0]));
+ if (isActualExpression) reference.set(element);
+ }
+ PsiElement oldElement = commonContainer;
+ if (commonContainer instanceof JetWhenEntry) {
+ JetExpression body = ((JetWhenEntry) commonContainer).getExpression();
+ if (body != null) {
+ oldElement = body;
+ }
+ } else if (commonContainer instanceof JetNamedFunction) {
+ JetExpression body = ((JetNamedFunction) commonContainer).getBodyExpression();
+ if (body != null) {
+ oldElement = body;
+ }
+ } else if (commonContainer instanceof JetSecondaryConstructor) {
+ JetExpression body = ((JetSecondaryConstructor) commonContainer).getBodyExpression();
+ if (body != null) {
+ oldElement = body;
+ }
+ } else if (commonContainer instanceof JetContainerNode) {
+ JetContainerNode container = (JetContainerNode) commonContainer;
+ PsiElement[] children = container.getChildren();
+ for (PsiElement child : children) {
+ if (child instanceof JetExpression) {
+ oldElement = child;
+ }
+ }
+ }
+ //ugly logic to make sure we are working with right actual expression
+ JetExpression actualExpression = reference.get();
+ int diff = actualExpression.getTextRange().getStartOffset() - oldElement.getTextRange().getStartOffset();
+ String actualExpressionText = actualExpression.getText();
+ PsiElement newElement = emptyBody.addAfter(oldElement, firstChild);
+ PsiElement elem = newElement.findElementAt(diff);
+ while (elem != null && !(elem instanceof JetExpression &&
+ actualExpressionText.equals(elem.getText()))) {
+ elem = elem.getParent();
+ }
+ if (elem != null) {
+ reference.set((JetExpression) elem);
+ }
+ emptyBody.addAfter(JetPsiFactory.createWhiteSpace(project, "\n"), firstChild);
+ property = (JetProperty) emptyBody.addAfter(property, firstChild);
+ emptyBody.addAfter(JetPsiFactory.createWhiteSpace(project, "\n"), firstChild);
+ actualExpression = reference.get();
+ diff = actualExpression.getTextRange().getStartOffset() - emptyBody.getTextRange().getStartOffset();
+ actualExpressionText = actualExpression.getText();
+ emptyBody = (JetExpression) anchor.replace(emptyBody);
+ elem = emptyBody.findElementAt(diff);
+ while (elem != null && !(elem instanceof JetExpression &&
+ actualExpressionText.equals(elem.getText()))) {
+ elem = elem.getParent();
+ }
+ if (elem != null) {
+ reference.set((JetExpression) elem);
+ }
+ } else {
+ property = (JetProperty) emptyBody.addAfter(property, firstChild);
+ emptyBody.addAfter(JetPsiFactory.createWhiteSpace(project, "\n"), firstChild);
+ emptyBody = (JetExpression) anchor.replace(emptyBody);
+ }
for (PsiElement child : emptyBody.getChildren()) {
if (child instanceof JetProperty) {
property = (JetProperty) child;
@@ -239,7 +300,7 @@ public class JetIntroduceVariableHandler extends JetIntroduceHandlerBase {
}
}
for (JetExpression replace : allReplaces) {
- if (replaceOccurrence) {
+ if (replaceOccurrence && !needBraces) {
boolean isActualExpression = expression == replace;
JetExpression element = (JetExpression) replace.replace(JetPsiFactory.createExpression(project, suggestedNames[0]));
references.add(element);
diff --git a/idea/testData/refactoring/introduceVariable/DoWhileAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/DoWhileAddBlockInner.kt
new file mode 100644
index 00000000000..ec34a0f00fb
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/DoWhileAddBlockInner.kt
@@ -0,0 +1,15 @@
+open class A() {
+ {
+ do println(1) while (true)
+ }
+}
+/*
+open class A() {
+ {
+ do {
+ val i = 1
+ println(i)
+ } while (true)
+ }
+}
+*/
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/FunctionAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/FunctionAddBlockInner.kt
new file mode 100644
index 00000000000..c1ad9303d00
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/FunctionAddBlockInner.kt
@@ -0,0 +1,7 @@
+fun x(): Int = println(1)
+/*
+fun x(): Int {
+ val i = 1
+ println(i)
+}
+*/
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/IfElseAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/IfElseAddBlockInner.kt
new file mode 100644
index 00000000000..674eedd3031
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/IfElseAddBlockInner.kt
@@ -0,0 +1,13 @@
+fun a() {
+ if (true) 2
+ else println(1)
+}
+/*
+fun a() {
+ if (true) 2
+ else {
+ val i = 1
+ println(i)
+ }
+}
+*/
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/IfThenAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/IfThenAddBlockInner.kt
new file mode 100644
index 00000000000..a2d001ac260
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/IfThenAddBlockInner.kt
@@ -0,0 +1,12 @@
+fun a() {
+ if (true) println(1)
+ else 2
+}
+/*
+fun a() {
+ if (true) {
+ val i = 1
+ println(i)
+ } else 2
+}
+*/
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/WhenAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/WhenAddBlockInner.kt
new file mode 100644
index 00000000000..3ae3550318b
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/WhenAddBlockInner.kt
@@ -0,0 +1,15 @@
+fun a() {
+ when (1) {
+ is 1 -> println(2)
+ }
+}
+/*
+fun a() {
+ when (1) {
+ is 1 -> {
+ val i = 2
+ println(i)
+ }
+ }
+}
+*/
\ No newline at end of file
diff --git a/idea/testData/refactoring/introduceVariable/WhileAddBlockInner.kt b/idea/testData/refactoring/introduceVariable/WhileAddBlockInner.kt
new file mode 100644
index 00000000000..1d46852ce01
--- /dev/null
+++ b/idea/testData/refactoring/introduceVariable/WhileAddBlockInner.kt
@@ -0,0 +1,15 @@
+open class A() {
+ {
+ while (true) println(1)
+ }
+}
+/*
+open class A() {
+ {
+ while (true) {
+ val i = 1
+ println(i)
+ }
+ }
+}
+*/
\ No newline at end of file
diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableTest.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableTest.java
index 01da2d1b2eb..ceb6a3fb8f6 100644
--- a/idea/tests/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableTest.java
+++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableTest.java
@@ -37,6 +37,10 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
doTest();
}
+ public void testDoWhileAddBlockInner() {
+ doTest();
+ }
+
public void testFewOccurrences() {
doTest();
}
@@ -45,6 +49,10 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
doTest();
}
+ public void testFunctionAddBlockInner() {
+ doTest();
+ }
+
public void testIfCondition() {
doTest();
}
@@ -53,10 +61,18 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
doTest();
}
+ public void testIfElseAddBlockInner() {
+ doTest();
+ }
+
public void testIfThenAddBlock() {
doTest();
}
+ public void testIfThenAddBlockInner() {
+ doTest();
+ }
+
public void testIt() {
doTest();
}
@@ -93,6 +109,10 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
doTest();
}
+ public void testWhenAddBlockInner() {
+ doTest();
+ }
+
public void testWhenParts() {
doTest();
}
@@ -101,6 +121,10 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
doTest();
}
+ public void testWhileAddBlockInner() {
+ doTest();
+ }
+
public void testWhileCondition() {
doTest();
}
@@ -133,6 +157,7 @@ public class JetIntroduceVariableTest extends LightCodeInsightFixtureTestCase {
}
});
int endOffset = file.getLastChild().getTextRange().getStartOffset();
+// System.out.println(file.getText().substring(0, endOffset).trim());
assertEquals(expectedResultText, file.getText().substring(0, endOffset).trim());
}
}