From 182de94259b9f3e6a23609d184145b51910f7597 Mon Sep 17 00:00:00 2001 From: Toni Suter Date: Thu, 21 Feb 2019 12:01:50 +0100 Subject: [PATCH] Bug 544470. Fix code formatting of switch with controller declaration Change-Id: I0d18b5767503e6bb3d137c9950b023f5c5084bd8 Signed-off-by: Toni Suter --- .../formatter/CodeFormatterVisitor.java | 16 ++++-- .../cdt/ui/tests/text/CodeFormatterTest.java | 53 +++++++++++++++++++ 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 4ed22399500..e0f9ae3b494 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 f959926efae..45f8255701f 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 { //}