Fixed KT-1295, first part of removing 'println'. Undo part is still not working.

This commit is contained in:
Alefas
2012-02-15 19:38:42 +04:00
parent 4ef61d331b
commit c643b1cf45
8 changed files with 167 additions and 4 deletions
@@ -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);
@@ -0,0 +1,15 @@
open class A() {
{
do println(<selection>1</selection>) while (true)
}
}
/*
open class A() {
{
do {
val i = 1
println(i)
} while (true)
}
}
*/
@@ -0,0 +1,7 @@
fun x(): Int = println(<selection>1</selection>)
/*
fun x(): Int {
val i = 1
println(i)
}
*/
@@ -0,0 +1,13 @@
fun a() {
if (true) 2
else println(<selection>1</selection>)
}
/*
fun a() {
if (true) 2
else {
val i = 1
println(i)
}
}
*/
@@ -0,0 +1,12 @@
fun a() {
if (true) println(<selection>1</selection>)
else 2
}
/*
fun a() {
if (true) {
val i = 1
println(i)
} else 2
}
*/
@@ -0,0 +1,15 @@
fun a() {
when (1) {
is 1 -> println(<selection>2</selection>)
}
}
/*
fun a() {
when (1) {
is 1 -> {
val i = 2
println(i)
}
}
}
*/
@@ -0,0 +1,15 @@
open class A() {
{
while (true) println(<selection>1</selection>)
}
}
/*
open class A() {
{
while (true) {
val i = 1
println(i)
}
}
}
*/
@@ -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());
}
}