From dd6f20db8604f06a7b1a657c89b75ff36f01a918 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Thu, 31 Mar 2011 03:07:20 +0000 Subject: [PATCH] Line breaking in 'if' statements. --- .../formatter/CodeFormatterVisitor.java | 19 ++++++++++++++----- .../cdt/ui/tests/text/CodeFormatterTest.java | 17 +++++++++++++++++ 2 files changed, 31 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 a97604222f8..49d63acae8b 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 @@ -3209,16 +3209,25 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.space(); } IASTExpression condExpr= node.getConditionExpression(); + final IASTStatement thenStatement = node.getThenClause(); + final IASTStatement elseStatement = node.getElseClause(); + Runnable tailFormatter = null; + if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_block) && + thenStatement instanceof IASTCompoundStatement && !startsWithMacroExpansion(thenStatement)) { + tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, + thenStatement.getFileLocation().getNodeOffset(), + preferences.insert_space_before_opening_brace_in_block, false); + } + tailFormatter = new ClosingParensesisTailFormatter( + preferences.insert_space_before_closing_paren_in_if, tailFormatter); + scribe.setTailFormatter(tailFormatter); if (condExpr == null || condExpr instanceof IASTProblemExpression) { scribe.skipToToken(Token.tRPAREN); } else { condExpr.accept(this); } - if (peekNextToken() == Token.tRPAREN) { - scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_if); - } - final IASTStatement thenStatement = node.getThenClause(); - final IASTStatement elseStatement = node.getElseClause(); + scribe.runTailFormatter(); + scribe.setTailFormatter(null); boolean thenStatementIsBlock = false; if (thenStatement != null) { 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 21cf4b6a449..e7f96a11245 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 @@ -237,6 +237,23 @@ public class CodeFormatterTest extends BaseUITestCase { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); assertFormatterResult(); } + + //void test() { + //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 7000000) {} + //if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 70000000) {} + //} + + //void test() { + // if (1000000 + 2000000 < 3000000 + 4000000 && 5000000 + 6000000 <= 7000000) { + // } + // if (1000000 + 2000000 < 3000000 + 4000000 + // && 5000000 + 6000000 <= 70000000) { + // } + //} + public void testIfStatement() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } //#define MY private: //