From 2ce87d555ec826063e72467a0293742981d7f74a Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 15 Mar 2011 22:34:29 +0000 Subject: [PATCH] Fixed formatting of C++-style casts and template function calls. --- .../formatter/CodeFormatterVisitor.java | 16 +++--- .../cdt/ui/tests/text/CodeFormatterTest.java | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+), 7 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 16fcb75356a..5cc81b81fdc 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 @@ -2314,7 +2314,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } node.getTypeId().accept(this); scribe.printNextToken(Token.tGT, preferences.insert_space_before_closing_angle_bracket_in_template_arguments); - if (preferences.insert_space_after_closing_angle_bracket_in_template_arguments) { + if (preferences.insert_space_before_opening_paren_in_method_invocation) { scribe.space(); } // operand @@ -3235,8 +3235,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } int nextToken= peekNextToken(); if (node.getPropertyInParent() != ICPPASTQualifiedName.SEGMENT_NAME || nextToken == Token.tGT) { - if (preferences.insert_space_after_closing_angle_bracket_in_template_arguments) { - // avoid explicit space if followed by pointer operator + if (node.getParent().getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME && + preferences.insert_space_before_opening_paren_in_method_invocation) { + scribe.space(); + } else if (node.getParent().getPropertyInParent() != IASTFunctionCallExpression.FUNCTION_NAME && + preferences.insert_space_after_closing_angle_bracket_in_template_arguments) { + // Avoid explicit space if followed by '*' or '&'. if (nextToken != Token.tSTAR && nextToken != Token.tAMPER) { scribe.space(); } @@ -3253,12 +3257,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.printNextToken(Token.t_return); final IASTExpression expression = node.getReturnValue(); if (expression != null) { -// if (peekNextToken() != Token.tLPAREN) { - scribe.space(); -// } + scribe.space(); expression.accept(this); } - // sometimes the return expression is null, when it should not + // Sometimes the return expression is null, when it should not be. if (expression == null && Token.tSEMI != peekNextToken()) { scribe.skipToToken(Token.tSEMI); } 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 b0a1b5b7e83..575456c313a 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 @@ -1079,6 +1079,60 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //int x=static_cast < int > ( 0 ) ; + + //int x = static_cast(0); + public void testCppCast_1() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + + //int x=static_cast < int >( 0 ) ; + + //int x = static_cast (0); + public void testCppCast_2() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_METHOD_INVOCATION, CCorePlugin.INSERT); + assertFormatterResult(); + } + + //template < typename T > + //void foo ( T t ) ; + // + //void test() { + //foo < const char* > ( "" ) ; + //} + + //template + //void foo(T t); + // + //void test() { + // foo(""); + //} + public void testTemplateFunctionCall_1() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + + //template < typename T > + //void foo ( T t ) ; + // + //void test() { + //foo < const char* >( "" ) ; + //} + + //template + //void foo(T t); + // + //void test() { + // foo (""); + //} + public void testTemplateFunctionCall_2() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_METHOD_INVOCATION, CCorePlugin.INSERT); + assertFormatterResult(); + } + //#define MY_MACRO int a; \ // int b; \ // int c();