Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToni Suter2019-02-21 11:01:50 +0000
committerDoug Schaefer2019-02-22 15:01:29 +0000
commit182de94259b9f3e6a23609d184145b51910f7597 (patch)
tree0403ade1f596d2a927a2d8dbdd3712ab9f3b586b
parent90c82078ac31ecc426c5d5223b5c865dd620f2dd (diff)
downloadorg.eclipse.cdt-182de94259b9f3e6a23609d184145b51910f7597.tar.gz
org.eclipse.cdt-182de94259b9f3e6a23609d184145b51910f7597.tar.xz
org.eclipse.cdt-182de94259b9f3e6a23609d184145b51910f7597.zip
Bug 544470. Fix code formatting of switch with controller declaration
Change-Id: I0d18b5767503e6bb3d137c9950b023f5c5084bd8 Signed-off-by: Toni Suter <tsuter@hsr.ch>
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java16
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java53
2 files changed, 64 insertions, 5 deletions
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
index 4ed2239950..e0f9ae3b49 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
@@ -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;
}
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
index f959926efa..45f8255701 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
@@ -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 {
//}

Back to the top