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