Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch statement rewrite formatting #3349

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -988,4 +986,105 @@ public void testSwitchExpressions_Bug567975_since_12() throws Exception {
assertEqualString(preview, builder.toString());
}

public void testSwitchStatementCase_01() throws Exception {
IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null);
String code = """
public class X {
static int foo(int i) {
int tw = 0;
switch (i) {
case 1: {
int z = 100;
break;
}
case 2:
return 2;
default:
return 3;
}
return tw;
}
}
""";
ICompilationUnit cu = pack1.createCompilationUnit("X.java", code, false, null);

CompilationUnit astRoot = createAST(cu);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
AST ast = astRoot.getAST();

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
TypeDeclaration type = findTypeDeclaration(astRoot, "X");
MethodDeclaration methodDecl = findMethodDeclaration(type, "foo");
Block block = methodDecl.getBody();
List<Statement> blockStatements = block.statements();
SwitchStatement switchStmt = (SwitchStatement) blockStatements.get(1);
{
SwitchCase newCase = ast.newSwitchCase();
newCase.setSwitchLabeledRule(false);
newCase.expressions().add(ast.newNumberLiteral("100"));

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

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);

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);

String expectedCode = """
public class X {
static int foo(int i) {
int tw = 0;
switch (i) {
case 1: {
int z = 100;
break;
}
case 100: {
break;
}
case 2:
return 20;
default:
throw new IllegalArgumentException("Unexpected value");
}
return tw;
}
}
""";

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
Loading