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:
parent
305143a8d0
commit
ab6b931d69
3 changed files with 41 additions and 19 deletions
|
@ -468,9 +468,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
IASTName name= node.getName();
|
IASTName name= node.getName();
|
||||||
if (name != null && name.toCharArray().length != 0) {
|
if (name != null && name.toCharArray().length != 0) {
|
||||||
// preserve non-space between pointer operator and name
|
if (isFirstDeclarator(node)) {
|
||||||
if (pointerOperators.length == 0 || scribe.printComment()) {
|
// preserve non-space between pointer operator and name
|
||||||
scribe.space();
|
if (pointerOperators.length == 0 || scribe.printComment()) {
|
||||||
|
scribe.space();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
name.accept(this);
|
name.accept(this);
|
||||||
}
|
}
|
||||||
|
@ -505,6 +507,21 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
return PROCESS_SKIP;
|
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)
|
* @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) {
|
if (expectParen) {
|
||||||
scribe.printNextToken(Token.tRPAREN);
|
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
|
// initializer
|
||||||
final IASTExpression newInitializer= node.getNewInitializer();
|
final IASTExpression newInitializer= node.getNewInitializer();
|
||||||
if (newInitializer != null || peekNextToken() == Token.tLPAREN) {
|
if (newInitializer != null || peekNextToken() == Token.tLPAREN) {
|
||||||
|
|
|
@ -408,14 +408,14 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
|
|
||||||
//void f() {
|
//void f() {
|
||||||
//int *px= :: new int( 0 );
|
//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);
|
//int *pz[ ] =new ( px)int(0);
|
||||||
//delete [] py;
|
//delete [] py;
|
||||||
//:: delete px;}
|
//:: delete px;}
|
||||||
|
|
||||||
//void f() {
|
//void f() {
|
||||||
// int *px = ::new int(0);
|
// 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);
|
// int *pz[] = new (px) int(0);
|
||||||
// delete[] py;
|
// delete[] py;
|
||||||
// ::delete px;
|
// ::delete px;
|
||||||
|
@ -563,8 +563,7 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
//NOT_DEFINED void foo()
|
//NOT_DEFINED void foo(){
|
||||||
// {
|
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
//enum T1
|
//enum T1
|
||||||
|
@ -706,4 +705,20 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -805,7 +805,7 @@ public final class WhiteSpaceOptions {
|
||||||
final InnerNode root= new InnerNode(parent, workingValues, FormatterMessages.WhiteSpaceTabPage_declarator_list);
|
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_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;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue