SplitConditionUtil#createAndAnd: fix corner cases with ending line comment

Review ID: IDEA-CR-31527
This commit is contained in:
Tagir Valeev
2018-04-12 10:49:13 +07:00
parent 0092b7b123
commit f805fa2840
6 changed files with 61 additions and 2 deletions

View File

@@ -126,7 +126,7 @@ public class SplitConditionUtil {
PsiExpression nextCondition = PsiUtil.skipParenthesizedExprDown(nextIf.getCondition());
if (nextCondition == null) break;
if (PsiEquivalenceUtil.areElementsEquivalent(extract, nextCondition) && nextIf.getThenBranch() != null) {
elseChain.add(nextIf.getThenBranch().getText());
elseChain.add(tracker.text(nextIf.getThenBranch()));
chainFinished = true;
}
else {
@@ -160,7 +160,7 @@ public class SplitConditionUtil {
thenString = createIfString(leave, thenBranch, (String)null, tracker);
}
else {
thenString = "{" + createIfString(leave, thenBranch, String.join(" else ", elseChain), tracker) + "}";
thenString = "{" + createIfString(leave, thenBranch, String.join("\nelse ", elseChain), tracker) + "\n}";
}
String ifString = createIfString(extract, thenString, elseBranch, tracker);
return (PsiIfStatement)factory.createStatementFromText(ifString, thenBranch);

View File

@@ -0,0 +1,12 @@
class C {
void foo() {
if (a) {
if (b) {
call();
} // foo
else if (c) {
otherCall();
} // bar
}
}
}

View File

@@ -0,0 +1,17 @@
class C {
void foo() {
if (a) {
if (b) {
call();
} // foo
else if (c) {
otherCall();
} // bar
else {
thirdCall();
} // baz
} else {
fourthCall();
} // qux
}
}

View File

@@ -0,0 +1,10 @@
class C {
void foo() {
if (a &<caret>& b) {
call();
} // foo
else if (a && c) {
otherCall();
} // bar
}
}

View File

@@ -0,0 +1,16 @@
class C {
void foo() {
if (a &<caret>& b) {
call();
} // foo
else if (a && c) {
otherCall();
} // bar
else if(a) {
thirdCall();
} // baz
else {
fourthCall();
} // qux
}
}

View File

@@ -61,6 +61,10 @@ public class SplitIfActionTest extends LightCodeInsightTestCase {
public void testCommentInside() { doTest(); }
public void testCommentBeforeElse() { doTest(); }
public void testCommentBeforeElse2() { doTest(); }
public void testChain() { doTest(); }
public void testChain2() { doTest(); }