From 280c0190d2e4ff33ed35135a011d934e15cb1c64 Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Sun, 10 Mar 2019 17:25:45 +0100 Subject: [PATCH] Bug 453385 - Fix for loop formatting When "before semicolon" was selected the space was inserted only if the init clause was a declaration. If it was an expression the formatter was skipped. Change-Id: I54605591b9a0829338dadb51e59460064b060697 Signed-off-by: Marco Stornelli --- .../formatter/CodeFormatterVisitor.java | 12 ++++-------- .../cdt/ui/tests/text/CodeFormatterTest.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 8 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 aad171e7f8c..20350cc9f63 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 @@ -3255,15 +3255,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private int visit(IASTExpressionStatement node) { Runnable semicolonFormatter = null; - if (!fHasClauseInitStatement) { - semicolonFormatter = new TrailingSemicolonFormatter(node); - scribe.setTailFormatter(semicolonFormatter); - } + semicolonFormatter = new TrailingSemicolonFormatter(node); + scribe.setTailFormatter(semicolonFormatter); node.getExpression().accept(this); - if (semicolonFormatter != null) { - semicolonFormatter.run(); - scribe.setTailFormatter(null); - } + semicolonFormatter.run(); + scribe.setTailFormatter(null); if (!fHasClauseInitStatement) { 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 a921e2bff3f..b4121dbfc3f 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 @@ -3903,4 +3903,23 @@ public class CodeFormatterTest extends BaseUITestCase { public void testFormmatterWithMacroFuncStyle_Bug475349() throws Exception { assertFormatterResult(); } + + //int main() { + // int i = 0; + // for(i = 0;i<3;i++){ + // } + // return 0; + //} + + //int main() { + // int i = 0; + // for (i = 0 ; i < 3 ; i++) { + // } + // return 0; + //} + public void testSpaceAfterSemicolonInFor_Bug453385() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_SEMICOLON_IN_FOR, CCorePlugin.INSERT); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_SEMICOLON_IN_FOR, CCorePlugin.INSERT); + assertFormatterResult(); + } }