From 101bcadce5495a3b15ace8231e492ec84dd3a4d2 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Thu, 31 Mar 2011 05:32:31 +0000 Subject: [PATCH] Line breaking in 'if' statements. --- .../formatter/CodeFormatterVisitor.java | 17 +++++++++-------- .../cdt/ui/tests/text/CodeFormatterTest.java | 10 ++++++++-- 2 files changed, 17 insertions(+), 10 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 49d63acae8b..e874c69da42 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 @@ -3236,18 +3236,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, thenStatementIsBlock = true; final List statements = Arrays.asList(block.getStatements()); if (isGuardClause(block, statements) && elseStatement == null && preferences.keep_guardian_clause_on_one_line) { - /* - * Need a specific formatting for guard clauses - * guard clauses are block with a single return or throw - * statement - */ - scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_block); - scribe.space(); + // Specific formatting for guard clauses. A guard clause is a block + // with a single return or throw statement. + if (scribe.scanner.getCurrentPosition() <= thenStatement.getFileLocation().getNodeOffset()) { + scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_block); + scribe.space(); + } statements.get(0).accept(this); scribe.printNextToken(Token.tRBRACE, true); scribe.printTrailingComment(); } else { - formatLeftCurlyBrace(line, preferences.brace_position_for_block); + if (scribe.scanner.getCurrentPosition() <= thenStatement.getFileLocation().getNodeOffset()) { + formatLeftCurlyBrace(line, preferences.brace_position_for_block); + } thenStatement.accept(this); if (elseStatement != null && preferences.insert_new_line_before_else_in_if_statement) { scribe.startNewLine(); 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 e7f96a11245..01e3334a72d 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 @@ -239,15 +239,21 @@ public class CodeFormatterTest extends BaseUITestCase { } //void test() { - //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 7000000) {} - //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 70000000) {} + //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 7000000) { + // // comment + //} + //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 70000000) { + // // comment + //} //} //void test() { // if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 7000000) { + // // comment // } // if (1000000 + 2000000 < 3000000 + 4000000 // && 5000000 + 6000000 <= 70000000) { + // // comment // } //} public void testIfStatement() throws Exception {