1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fix for 234915: [formatter] Option "after comma in declarator list" has no effect

Fix formatting of new expressions, related to bug 236856
This commit is contained in:
Anton Leherbauer 2008-06-19 09:31:46 +00:00
parent 305143a8d0
commit ab6b931d69
3 changed files with 41 additions and 19 deletions

View file

@ -468,10 +468,12 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
IASTName name= node.getName();
if (name != null && name.toCharArray().length != 0) {
if (isFirstDeclarator(node)) {
// preserve non-space between pointer operator and name
if (pointerOperators.length == 0 || scribe.printComment()) {
scribe.space();
}
}
name.accept(this);
}
IASTDeclarator nestedDecl= node.getNestedDeclarator();
@ -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 <code>true</code> 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) {

View file

@ -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();
}
}

View file

@ -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;
}