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 2394d93c9e9..0164d7e1a03 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 @@ -468,9 +468,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } IASTName name= node.getName(); if (name != null && name.toCharArray().length != 0) { - // preserve non-space between pointer operator and name - if (pointerOperators.length == 0 || scribe.printComment()) { - scribe.space(); + if (isFirstDeclarator(node)) { + // preserve non-space between pointer operator and name + if (pointerOperators.length == 0 || scribe.printComment()) { + scribe.space(); + } } name.accept(this); } @@ -505,6 +507,21 @@ public class CodeFormatterVisitor extends CPPASTVisitor { return PROCESS_SKIP; } + /** + * Determine whether the given declarator is the first in a list of declarators (if any). + * + * @param node the declarator node + * @return true if this node is the first in a list + */ + private boolean isFirstDeclarator(IASTDeclarator node) { + IASTNode parent= node.getParent(); + if (parent instanceof IASTSimpleDeclaration) { + IASTSimpleDeclaration simpleDecl= (IASTSimpleDeclaration) parent; + return simpleDecl.getDeclarators()[0] == node; + } + return true; + } + /* * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier) */ @@ -2208,17 +2225,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { if (expectParen) { scribe.printNextToken(Token.tRPAREN); } - // array spec - final IASTExpression[] newTypeIdArrayExpressions= node.getNewTypeIdArrayExpressions(); - for (int i= 0; i < newTypeIdArrayExpressions.length; i++) { - IASTExpression expression= newTypeIdArrayExpressions[i]; - scribe.printNextToken(Token.tLBRACKET, preferences.insert_space_before_opening_bracket); - if (preferences.insert_space_after_opening_bracket ) { - scribe.space(); - } - expression.accept(this); - scribe.printNextToken(Token.tRBRACKET, preferences.insert_space_before_closing_bracket); - } + // initializer final IASTExpression newInitializer= node.getNewInitializer(); if (newInitializer != null || peekNextToken() == Token.tLPAREN) { 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 213474c6472..81d7aec5042 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 @@ -408,14 +408,14 @@ public class CodeFormatterTest extends BaseUITestCase { //void f() { //int *px= :: new int( 0 ); - //int py [] = new int [5 ] (0, 1,2,3, 4); + //int* py [] = new int [5 ] (0, 1,2,3, 4); //int *pz[ ] =new ( px)int(0); //delete [] py; //:: delete px;} //void f() { // int *px = ::new int(0); - // int py[] = new int[5](0, 1, 2, 3, 4); + // int* py[] = new int[5](0, 1, 2, 3, 4); // int *pz[] = new (px) int(0); // delete[] py; // ::delete px; @@ -563,8 +563,7 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } - //NOT_DEFINED void foo() - // { + //NOT_DEFINED void foo(){ // } // //enum T1 @@ -706,4 +705,20 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //int a = 0, b = 1, c = 2, d = 3; + + //int a = 0,b = 1,c = 2,d = 3; + public void testSpaceAfterCommaInDeclaratorList_Bug234915() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, CCorePlugin.DO_NOT_INSERT); + assertFormatterResult(); + } + + //int a = 0,b = 1,c = 2,d = 3; + + //int a = 0, b = 1, c = 2, d = 3; + public void testSpaceAfterCommaInDeclaratorList2_Bug234915() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, CCorePlugin.INSERT); + assertFormatterResult(); + } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/WhiteSpaceOptions.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/WhiteSpaceOptions.java index d720b1f2995..f2bc410aa47 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/WhiteSpaceOptions.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/WhiteSpaceOptions.java @@ -805,7 +805,7 @@ public final class WhiteSpaceOptions { final InnerNode root= new InnerNode(parent, workingValues, FormatterMessages.WhiteSpaceTabPage_declarator_list); createOption(root, workingValues, FormatterMessages.WhiteSpaceTabPage_declarator_list_before_comma, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_DECLARATOR_LIST, DECLARATOR_LIST_PREVIEW); - createOption(root, workingValues, FormatterMessages.WhiteSpaceTabPage_declarator_list_after_comma, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, EXPRESSION_LIST_PREVIEW); + createOption(root, workingValues, FormatterMessages.WhiteSpaceTabPage_declarator_list_after_comma, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_DECLARATOR_LIST, DECLARATOR_LIST_PREVIEW); return root; }