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

Bug fixes.

This commit is contained in:
Sergey Prigogin 2011-03-01 23:24:26 +00:00
parent eaabdd84c8
commit b4e5df991c
2 changed files with 140 additions and 85 deletions

View file

@ -1544,8 +1544,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} catch (ASTProblemException e) {
scribe.skipToToken(Token.tRBRACKET);
}
boolean insertSpace= emptyBrackets ? preferences.insert_space_between_empty_brackets
: preferences.insert_space_before_closing_bracket;
boolean insertSpace= emptyBrackets ?
preferences.insert_space_between_empty_brackets :
preferences.insert_space_before_closing_bracket;
scribe.printNextToken(Token.tRBRACKET, insertSpace);
}
}
@ -2081,7 +2082,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private int visit(IASTConditionalExpression node) {
Runnable tailFormatter = scribe.takeTailFormatter();
try {
node.getLogicalConditionExpression().accept(this);
} finally {
scribe.setTailFormatter(tailFormatter);
}
scribe.printTrailingComment();
if (preferences.insert_space_before_question_in_conditional) {
scribe.space();
@ -2097,23 +2103,33 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
do {
try {
scribe.alignFragment(alignment, 0);
final IASTExpression positiveExpression = node.getPositiveResultExpression();
final IASTExpression negativeExpression = node.getNegativeResultExpression();
final IASTExpression nextExpression = positiveExpression != null ?
positiveExpression : negativeExpression;
// In case of macros we may have already passed the question mark position.
if (scribe.scanner.getCurrentPosition() < nextExpression.getFileLocation().getNodeOffset()) {
scribe.printNextToken(Token.tQUESTION, false);
if (preferences.insert_space_after_question_in_conditional) {
scribe.space();
}
final IASTExpression positiveExpression = node.getPositiveResultExpression();
}
if (positiveExpression != null) { // gcc-extension allows to omit the positive expression.
positiveExpression.accept(this);
}
scribe.printTrailingComment();
scribe.alignFragment(alignment, 1);
scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_conditional);
// In case of macros we may have already passed the colon position.
if (scribe.scanner.getCurrentPosition() < negativeExpression.getFileLocation().getNodeOffset()) {
scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_conditional);
if (preferences.insert_space_after_colon_in_conditional) {
scribe.space();
}
node.getNegativeResultExpression().accept(this);
}
negativeExpression.accept(this);
ok = true;
} catch (AlignmentException e) {
@ -2125,7 +2141,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private int visit(IASTFunctionCallExpression node) {
Runnable tailFormatter = scribe.takeTailFormatter();
try {
node.getFunctionNameExpression().accept(this);
} finally {
scribe.setTailFormatter(tailFormatter);
}
IASTInitializerClause[] paramExpr= node.getArguments();
if (peekNextToken() == Token.tIDENTIFIER) {
skipNode(node);
@ -2455,9 +2476,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.printTrailingComment();
scribe.alignFragment(expressionAlignment, 1);
// operator
// In case of macros we may have already passed the operator position.
if (scribe.scanner.getCurrentPosition() < node.getOperand2().getFileLocation().getNodeOffset()) {
// Operator
final int nextToken= peekNextToken();
// in case of C++ alternative operators, like 'and', 'not', etc. a space
// In case of C++ alternative operators, like 'and', 'not', etc. a space
boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar());
switch (node.getOperator()) {
@ -2471,6 +2494,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.space();
}
}
}
// operand 2
final IASTExpression op2= node.getOperand2();
@ -2490,15 +2514,18 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// operand 1
op1.accept(this);
// operator
// In case of macros we may have already passed the equal sign position.
if (scribe.scanner.getCurrentPosition() < node.getOperand2().getFileLocation().getNodeOffset()) {
// Operator
final int nextToken= peekNextToken();
// in case of C++ alternative operators, like 'and', 'not', etc. a space
// In case of C++ alternative operators, like 'and', 'not', etc. a space
boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar());
scribe.printNextToken(nextToken, forceSpace || preferences.insert_space_before_assignment_operator);
if (forceSpace || preferences.insert_space_after_assignment_operator) {
scribe.space();
}
}
Alignment expressionAlignment= scribe.createAlignment(
Alignment.ASSIGNMENT_EXPRESSION,
@ -2580,7 +2607,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private int visit(IASTFieldReference node) {
IASTExpression expr= node.getFieldOwner();
if (expr != null) {
Runnable tailFormatter = scribe.takeTailFormatter();
try {
expr.accept(this);
} finally {
scribe.setTailFormatter(tailFormatter);
}
}
final IASTName fieldName= node.getFieldName();
if (fieldName != null) {
@ -2619,6 +2651,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private int visit(IASTArraySubscriptExpression node) {
Runnable tailFormatter = scribe.takeTailFormatter();
try {
node.getArrayExpression().accept(this);
scribe.printNextToken(Token.tLBRACKET, preferences.insert_space_before_opening_bracket);
@ -2629,6 +2663,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
node.getArgument().accept(this);
scribe.printNextToken(Token.tRBRACKET, preferences.insert_space_before_closing_bracket);
} finally {
scribe.setTailFormatter(tailFormatter);
}
return PROCESS_SKIP;
}

View file

@ -2122,7 +2122,7 @@ public class Scribe {
/*
* Returns the tail formatter associated with the current alignment or, if there is no current
* alignment, with the Scribe itself.
* alignment, with the scribe itself.
* @see #tailFormatter
*/
public Runnable getTailFormatter() {
@ -2133,9 +2133,27 @@ public class Scribe {
}
}
/*
* Returns the tail formatter associated with the current alignment or, if there is no current
* alignment, with the scribe itself. The tail formatter associated with the alignment or
* the scribe is set to {@code null}.
* @see #tailFormatter
*/
public Runnable takeTailFormatter() {
Runnable formatter;
if (currentAlignment != null) {
formatter = currentAlignment.tailFormatter;
currentAlignment.tailFormatter = null;
} else {
formatter = this.tailFormatter;
this.tailFormatter = null;
}
return formatter;
}
/*
* Sets the tail formatter associated with the current alignment or, if there is no current
* alignment, with the Scribe itself.
* alignment, with the scribe itself.
* @see #tailFormatter
*/
public void setTailFormatter(Runnable tailFormatter) {
@ -2148,7 +2166,7 @@ public class Scribe {
/*
* Runs the tail formatter associated with the current alignment or, if there is no current
* alignment, with the Scribe itself.
* alignment, with the scribe itself.
* @see #tailFormatter
*/
public void runTailFormatter() {