mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-11 10:15:39 +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:
parent
90c82078ac
commit
182de94259
2 changed files with 64 additions and 5 deletions
|
@ -3686,11 +3686,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
if (!startsWithMacroExpansion(node)) {
|
if (!startsWithMacroExpansion(node)) {
|
||||||
scribe.printNextToken(Token.t_switch);
|
scribe.printNextToken(Token.t_switch);
|
||||||
}
|
}
|
||||||
IASTExpression controllerExpression = node.getControllerExpression();
|
IASTNode controller = node.getControllerExpression();
|
||||||
try {
|
try {
|
||||||
// optional init-statement
|
// optional init-statement
|
||||||
if (node instanceof ICPPASTSwitchStatement) {
|
if (node instanceof ICPPASTSwitchStatement) {
|
||||||
IASTStatement initStatement = ((ICPPASTSwitchStatement) node).getInitializerStatement();
|
ICPPASTSwitchStatement cppSwitchStatement = ((ICPPASTSwitchStatement) node);
|
||||||
|
IASTStatement initStatement = cppSwitchStatement.getInitializerStatement();
|
||||||
if (initStatement != null) {
|
if (initStatement != null) {
|
||||||
beginSwitchClause();
|
beginSwitchClause();
|
||||||
fHasClauseInitStatement = true;
|
fHasClauseInitStatement = true;
|
||||||
|
@ -3699,12 +3700,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (controller == null) {
|
||||||
|
controller = cppSwitchStatement.getControllerDeclaration();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Controller expression
|
// Controller expression
|
||||||
if (!doNodesHaveSameOffset(node, controllerExpression) && !fHasClauseInitStatement) {
|
if (!doNodesHaveSameOffset(node, controller) && !fHasClauseInitStatement) {
|
||||||
beginSwitchClause();
|
beginSwitchClause();
|
||||||
}
|
}
|
||||||
controllerExpression.accept(this);
|
controller.accept(this);
|
||||||
if (peekNextToken() == Token.tRPAREN) {
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
|
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;
|
boolean wasAStatement = false;
|
||||||
for (int i = 0; i < statementsLength; i++) {
|
for (int i = 0; i < statementsLength; i++) {
|
||||||
final IASTStatement statement = statements.get(i);
|
final IASTStatement statement = statements.get(i);
|
||||||
if (doNodeLocationsOverlap(controllerExpression, statement)) {
|
if (doNodeLocationsOverlap(controller, statement)) {
|
||||||
statement.accept(this);
|
statement.accept(this);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3365,6 +3365,59 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
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 {
|
//namespace na {
|
||||||
//inline namespace nb {
|
//inline namespace nb {
|
||||||
//}
|
//}
|
||||||
|
|
Loading…
Add table
Reference in a new issue