switch -> if: fix comments line breaks (IDEA-195383)

This commit is contained in:
Anna.Kozlova
2018-07-11 13:02:10 +02:00
parent b1a377ff20
commit ae3afdbaf8
3 changed files with 35 additions and 5 deletions
@@ -280,6 +280,8 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
out.append(variable.getType().getCanonicalText()).append(' ').append(variable.getName()).append(';');
}
}
boolean addLineBreak = true;
for (PsiElement bodyStatement : bodyStatements) {
if (bodyStatement instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement = (PsiBlockStatement)bodyStatement;
@@ -289,21 +291,25 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
}
}
else {
appendElement(bodyStatement, out, commentTracker);
addLineBreak = appendElement(bodyStatement, out, commentTracker);
}
}
out.append("\n}");
if (addLineBreak) {
out.append("\n");
}
out.append("}");
}
private static void appendElement(PsiElement element, @NonNls StringBuilder out, CommentTracker commentTracker) {
if (element instanceof PsiWhiteSpace) return;
private static boolean appendElement(PsiElement element, @NonNls StringBuilder out, CommentTracker commentTracker) {
if (element instanceof PsiBreakStatement) {
final PsiBreakStatement breakStatement = (PsiBreakStatement)element;
final PsiIdentifier identifier = breakStatement.getLabelIdentifier();
if (identifier == null) {
return;
return false;
}
}
out.append(commentTracker.text(element));
return true;
}
}
@@ -0,0 +1,11 @@
// "Replace 'switch' with 'if'" "true"
class X {
void test(int i) {
if (i == 1) {//foo
if (Math.random() > 0.5) {
System.out.println("Hello");
}
}
}
}
@@ -0,0 +1,13 @@
// "Replace 'switch' with 'if'" "true"
class X {
void test(int i) {
<caret>switch (i) {
case 1:
//foo
if (Math.random() > 0.5) {
System.out.println("Hello");
}
}
}
}