1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Fix for 239461: Code formatter: problems with the C++ boolean operator "not"

This commit is contained in:
Anton Leherbauer 2008-07-10 11:03:12 +00:00
parent b3d0d40b3d
commit 43f7fdc68d
2 changed files with 35 additions and 6 deletions

View file

@ -2017,8 +2017,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
case IASTUnaryExpression.op_alignOf:
default:
int operatorToken= peekNextToken();
boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar());
scribe.printNextToken(operatorToken, preferences.insert_space_before_unary_operator);
if (preferences.insert_space_after_unary_operator) {
if (forceSpace || preferences.insert_space_after_unary_operator) {
scribe.space();
} else if (operatorToken == Token.tIDENTIFIER && peekNextToken() != Token.tLPAREN) {
scribe.space();
@ -2047,7 +2048,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
scribe.alignFragment(expressionAlignment, 0);
// operator
switch (node.getOperator()) {
final int nextToken= peekNextToken();
// in case of C++ alternative operators, like 'and', 'not', etc. a space
boolean forceSpace= Character.isJavaIdentifierStart(peekNextChar());
switch (node.getOperator()) {
case IASTBinaryExpression.op_assign:
case IASTBinaryExpression.op_binaryAndAssign:
case IASTBinaryExpression.op_binaryOrAssign:
@ -2059,14 +2064,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
case IASTBinaryExpression.op_plusAssign:
case IASTBinaryExpression.op_shiftLeftAssign:
case IASTBinaryExpression.op_shiftRightAssign:
scribe.printNextToken(peekNextToken(), preferences.insert_space_before_assignment_operator);
if (preferences.insert_space_after_assignment_operator) {
scribe.printNextToken(nextToken, forceSpace || preferences.insert_space_before_assignment_operator);
if (forceSpace || preferences.insert_space_after_assignment_operator) {
scribe.space();
}
break;
default:
scribe.printNextToken(peekNextToken(), preferences.insert_space_before_binary_operator);
if (preferences.insert_space_after_binary_operator) {
scribe.printNextToken(nextToken, forceSpace || preferences.insert_space_before_binary_operator);
if (forceSpace || preferences.insert_space_after_binary_operator) {
scribe.space();
}
}
@ -3093,6 +3098,16 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
return false;
}
private char peekNextChar() {
if (peekNextToken() != Token.tBADCHAR) {
char[] text= localScanner.getCurrentTokenSource();
if (text.length > 0) {
return text[0];
}
}
return 0;
}
private int peekNextToken() {
if (scribe.shouldSkip(scribe.scanner.getCurrentPosition())) {
return Token.tBADCHAR;

View file

@ -746,4 +746,18 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult();
}
//bool test(bool x)
//{
// return x or x and not (not x);
//}
//bool test(bool x) {
// return x or x and not (not x);
//}
public void testSpaceBeforeAndAfterAlternativeLogicalOperator_Bug239461() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, CCorePlugin.DO_NOT_INSERT);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, CCorePlugin.DO_NOT_INSERT);
assertFormatterResult();
}
}