1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Bug 544470. Fix code formatting of switch with controller declaration

Change-Id: I0d18b5767503e6bb3d137c9950b023f5c5084bd8
Signed-off-by: Toni Suter <tsuter@hsr.ch>
This commit is contained in:
Toni Suter 2019-02-21 12:01:50 +01:00 committed by Doug Schaefer
parent 90c82078ac
commit 182de94259
2 changed files with 64 additions and 5 deletions

View file

@ -3686,11 +3686,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (!startsWithMacroExpansion(node)) {
scribe.printNextToken(Token.t_switch);
}
IASTExpression controllerExpression = node.getControllerExpression();
IASTNode controller = node.getControllerExpression();
try {
// optional init-statement
if (node instanceof ICPPASTSwitchStatement) {
IASTStatement initStatement = ((ICPPASTSwitchStatement) node).getInitializerStatement();
ICPPASTSwitchStatement cppSwitchStatement = ((ICPPASTSwitchStatement) node);
IASTStatement initStatement = cppSwitchStatement.getInitializerStatement();
if (initStatement != null) {
beginSwitchClause();
fHasClauseInitStatement = true;
@ -3699,12 +3700,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.space();
}
}
if (controller == null) {
controller = cppSwitchStatement.getControllerDeclaration();
}
}
// Controller expression
if (!doNodesHaveSameOffset(node, controllerExpression) && !fHasClauseInitStatement) {
if (!doNodesHaveSameOffset(node, controller) && !fHasClauseInitStatement) {
beginSwitchClause();
}
controllerExpression.accept(this);
controller.accept(this);
if (peekNextToken() == Token.tRPAREN) {
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
}
@ -3743,7 +3749,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
boolean wasAStatement = false;
for (int i = 0; i < statementsLength; i++) {
final IASTStatement statement = statements.get(i);
if (doNodeLocationsOverlap(controllerExpression, statement)) {
if (doNodeLocationsOverlap(controller, statement)) {
statement.accept(this);
continue;
}

View file

@ -3365,6 +3365,59 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult();
}
//void foo() {
// switch(int i{}){
// }
//}
//void foo() {
// switch (int i { }) {
// }
//}
public void testSwitchControllerDeclarationFormat_1() throws Exception {
assertFormatterResult();
}
//void foo() {
// switch( int i=42 ){
// }
//}
//void foo() {
// switch (int i = 42) {
// }
//}
public void testSwitchControllerDeclarationFormat_2() throws Exception {
assertFormatterResult();
}
//void foo() {
// switch
// (int i{}){
// }
//}
//void foo() {
// switch (int i { }) {
// }
//}
public void testSwitchControllerDeclarationFormat_3() throws Exception {
assertFormatterResult();
}
//void foo() {
// switch(constexpr bool k=true;int i{}){
// }
//}
//void foo() {
// switch (constexpr bool k = true; int i { }) {
// }
//}
public void testSwitchControllerDeclarationFormat_4() throws Exception {
assertFormatterResult();
}
//namespace na {
//inline namespace nb {
//}