From 39b2ea6eb4438ea97c083f7cb9e510e8dece4e2a Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 12 Jul 2007 14:16:24 +0000 Subject: [PATCH] Fix for 195942: [Formatter] for with empty expression causes confusion --- .../formatter/CodeFormatterVisitor.java | 52 +++++++++++-------- .../cdt/ui/tests/text/CodeFormatterTest.java | 38 ++++++++++++++ 2 files changed, 67 insertions(+), 23 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 32aee5684de..938149ff5be 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 @@ -1578,8 +1578,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor { // probably a macro with empty expansion skipToNode(node); } - scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); - scribe.printTrailingComment(); + if (!fInsideFor) { + scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); + scribe.printTrailingComment(); + } return PROCESS_SKIP; } @@ -1605,31 +1607,35 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final int line = scribe.line; scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_for); fInsideFor= true; - - if (preferences.insert_space_after_opening_paren_in_for) { - scribe.space(); - } - IASTStatement initializerStmt= node.getInitializerStatement(); - initializerStmt.accept(this); - if (peekNextToken() == Token.tSEMI) { - scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); - } - final IASTExpression condition = node.getConditionExpression(); - if (condition != null) { - if (preferences.insert_space_after_semicolon_in_for) { + try { + if (preferences.insert_space_after_opening_paren_in_for) { scribe.space(); } - condition.accept(this); - } - scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); - IASTExpression iterationExpr= node.getIterationExpression(); - if (iterationExpr != null) { - if (preferences.insert_space_after_semicolon_in_for) { - scribe.space(); + IASTStatement initializerStmt= node.getInitializerStatement(); + initializerStmt.accept(this); + if (peekNextToken() == Token.tSEMI) { + scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); } - iterationExpr.accept(this); + final IASTExpression condition = node.getConditionExpression(); + if (condition != null) { + if (preferences.insert_space_after_semicolon_in_for) { + scribe.space(); + } + condition.accept(this); + } + if (peekNextToken() == Token.tSEMI) { + scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for); + } + IASTExpression iterationExpr= node.getIterationExpression(); + if (iterationExpr != null) { + if (preferences.insert_space_after_semicolon_in_for) { + scribe.space(); + } + iterationExpr.accept(this); + } + } finally { + fInsideFor= false; } - fInsideFor= false; scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for); formatAction(line, node.getBody(), preferences.brace_position_for_block, preferences.insert_space_before_opening_brace_in_block); 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 14c0bdbb920..3f6e8f5a80d 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 @@ -141,4 +141,42 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //void foo(){ + //for(;;){ + //int a=0; + //switch(a){ + //case 0: + //++a; + //break; + //case 1: + //--a; + //break; + //} + //} + //} + //int main(void){ + //foo(); + //return 1; + //} + + //void foo() { + // for (;;) { + // int a=0; + // switch (a) { + // case 0: + // ++a; + // break; + // case 1: + // --a; + // break; + // } + // } + //} + //int main(void) { + // foo(); + // return 1; + //} + public void testForWithEmptyExpression_Bug195942() throws Exception { + assertFormatterResult(); + } }