From 1ae4154fb6d92f5b7c1fef4e78fce52d0a4d832a Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 21 Mar 2007 15:03:29 +0000 Subject: [PATCH] Fix formatter issue with semicolons in for stmt --- .../internal/formatter/CodeFormatterVisitor.java | 16 ++++++++++++---- .../resources/formatter/bugs/After.cpp | 7 ++++++- .../resources/formatter/bugs/Before.cpp | 2 ++ 3 files changed, 20 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 c59c47c506a..f8c07594dff 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 @@ -1437,8 +1437,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(IASTExpressionStatement node) { node.getExpression().accept(this); - 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; } @@ -1446,14 +1448,16 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.printNextToken(Token.t_for); 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(); - fInsideFor= true; initializerStmt.accept(this); - fInsideFor= false; + 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) { @@ -1464,8 +1468,12 @@ public class CodeFormatterVisitor extends CPPASTVisitor { 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); } + 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/resources/formatter/bugs/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp index 8fe2d3d2c51..819e6603845 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp @@ -17,7 +17,7 @@ class AClass : public ABaseClass { }; AClass::AClass(int x) throw(int) : ABaseClass(x) { - for (int i=0; i < 12;i++) { + for (int i=0; i < 12; i++) { } } // keep space between decl spec and declarator @@ -32,3 +32,8 @@ char* s2= "this " "is " int main() { return ID(0); } +// semicolons inside for +void g() { + for (int i=0; i<10; ++i) { + } +} diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp index b100675f3c0..ce83ec39384 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp @@ -17,3 +17,5 @@ char* s2= "this " "is " // macro definition with line comment #define ID(x) x // identity int main() {return ID(0);} +// semicolons inside for +void g() {for(int i=0;i<10;++i){}}