Skip to content

Commit

Permalink
Added tests & modifications made for the Switch rewrite formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Dec 14, 2024
1 parent 4332c2d commit 12ee810
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,9 @@ public void testSwitchStatement_Bug543720_since_12() throws Exception {
buf.append(" int z = 100;\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" case 100:\n");
buf.append(" {\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" case 100: {\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" default : {\n");
buf.append(" break;\n");
buf.append(" }\n");
Expand Down Expand Up @@ -852,8 +851,7 @@ public void testSwitchExpressions_05_since_12() throws Exception {
builder.append(" public String foo(int i) {\n" +
" String ret = switch(i%2) {\n" +
" case 0 : \"even\";\n" +
" case 1:\n" +
" \"odd\";\n" +
" case 1: \"odd\";\n" +
" default : \"\";\n" +
" };\n" +
" return ret;");
Expand Down Expand Up @@ -999,16 +997,13 @@ static int foo(int i) {
int z = 100;
break;
}
default: {
break;
}
case 2:
return 2;
default:
return 3;
}
return tw;
}
public static void main(String[] args) {
System.out.print(foo(1));
}
}
""";
ICompilationUnit cu = pack1.createCompilationUnit("X.java", code, false, null);
Expand All @@ -1022,34 +1017,48 @@ public static void main(String[] args) {
MethodDeclaration methodDecl = findMethodDeclaration(type, "foo");
Block block = methodDecl.getBody();
List<Statement> blockStatements = block.statements();
SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
{
SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);

SwitchCase newCase = ast.newSwitchCase();
newCase.setSwitchLabeledRule(false);
newCase.expressions().add(ast.newNumberLiteral("100"));

SwitchCase newCase1 = ast.newSwitchCase();
newCase1.setSwitchLabeledRule(true);
newCase1.expressions().add(ast.newNumberLiteral("1000"));


BreakStatement breakStatement = ast.newBreakStatement();
Block newBlock = ast.newBlock();
newBlock.statements().add(breakStatement);

BreakStatement breakStatement1 = ast.newBreakStatement();
Block newBlock1 = ast.newBlock();
newBlock1.statements().add(breakStatement1);

SwitchCase defaultCase = (SwitchCase) switchStmt.statements().get(2);
ListRewrite listRewrite = rewrite.getListRewrite(switchStmt, SwitchStatement.STATEMENTS_PROPERTY);

listRewrite.insertBefore(newCase, defaultCase, null);
listRewrite.insertBefore(newBlock, defaultCase, null);
}

{
NumberLiteral nl = ast.newNumberLiteral();
nl.setToken("20");
ReturnStatement rsNew = ast.newReturnStatement();
rsNew.setExpression(nl);

ReturnStatement rsOld = (ReturnStatement) switchStmt.statements().get(3);

listRewrite.insertBefore(newCase1, defaultCase, null);
listRewrite.insertBefore(newBlock1, defaultCase, null);
rewrite.replace(rsOld, rsNew, null);

}
{
ThrowStatement throwStatement = ast.newThrowStatement();
ClassInstanceCreation instanceOfCreation = ast.newClassInstanceCreation();
StringLiteral sl = ast.newStringLiteral();
sl.setEscapedValue("\"Unexpected value\"");

instanceOfCreation.arguments().add(sl);
instanceOfCreation.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException")));

throwStatement.setExpression(instanceOfCreation);

ReturnStatement rs = (ReturnStatement) switchStmt.statements().get(5);

rewrite.replace(rs, throwStatement, null);
}

String preview = evaluateRewrite(cu, rewrite);
Expand All @@ -1066,20 +1075,16 @@ static int foo(int i) {
case 100: {
break;
}
default: {
break;
}
case 2:
return 20;
default:
throw new IllegalArgumentException("Unexpected value");
}
return tw;
}
public static void main(String[] args) {
System.out.print(foo(1));
}
}
""";

assertEqualString(preview, expectedCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ private int rewriteList(
TextEditGroup editGroup= getEditGroup(currEvent);
ASTNode changed= (ASTNode) currEvent.getNewValue();

updateIndent(prevMark, currPos, i, editGroup);
// make sure that comments between last modified source position and extended starting position of
// node to be replaced are not touched
try {
Expand Down Expand Up @@ -3779,9 +3778,15 @@ protected String getSeparatorString(int nodeIndex, int nextNodeIndex) {
boolean isSwitchLabelRule = isSwitchLabeledRule(nodeIndex, nextNodeIndex);
String spaceDelim = JavaCore.INSERT.equals(ASTRewriteAnalyzer.this.options.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ARROW_IN_SWITCH_CASE))? " ":""; //$NON-NLS-1$ //$NON-NLS-2$
String lineDelim = isSwitchLabelRule ? spaceDelim : getLineDelimiter();

StringBuilder buf= new StringBuilder(lineDelim);
buf.append(createIndentString(getNodeIndent(nextNodeIndex)));
StringBuilder buf = new StringBuilder();
Object object = ((NodeRewriteEvent) this.list[nodeIndex]).getNewValue();
if (object instanceof SwitchCase) {
buf.append(spaceDelim);
} else if (object instanceof Block || object instanceof YieldStatement || object instanceof ExpressionStatement || object instanceof ReturnStatement || object instanceof BreakStatement) {
buf.append(lineDelim).append(createIndentString(getNodeIndent(nextNodeIndex)));
} else {
buf.append(createIndentString(getNodeIndent(nextNodeIndex)));
}
return buf.toString();
}

Expand All @@ -3801,7 +3806,7 @@ protected int getNodeIndent(int nodeIndex) {
ASTNode prevNode = getNode(nodeIndex -1);
if (prevNode.getNodeType() == ASTNode.SWITCH_CASE && ((SwitchCase)prevNode).isSwitchLabeledRule()) {
return 0;
} else {
} else if((changeKind == RewriteEvent.REPLACED) && (node instanceof ReturnStatement || node instanceof ThrowStatement)) {
indent++;
}
}
Expand Down

0 comments on commit 12ee810

Please sign in to comment.