1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Few formatting fixes.

This commit is contained in:
Sergey Prigogin 2012-03-05 18:52:58 -08:00
parent a88fc6d3b5
commit f360d64c77
3 changed files with 80 additions and 12 deletions

View file

@ -2275,7 +2275,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
boolean ok = false;
do {
try {
scribe.alignFragment(alignment, 0);
// In case of macros we may have already passed the expression position.
if (positiveExpression != null &&
scribe.scanner.getCurrentPosition() <= positiveExpression.getFileLocation().getNodeOffset()) {
scribe.alignFragment(alignment, 0);
}
scribe.setTailFormatter(new TrailingTokenFormatter(Token.tCOLON, node,
preferences.insert_space_before_colon_in_conditional,
preferences.insert_space_after_colon_in_conditional));
@ -2286,7 +2290,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.runTailFormatter();
if (!(negativeExpression instanceof IASTConditionalExpression)) {
scribe.alignFragment(alignment, 1);
// In case of macros we may have already passed the expression position.
if (scribe.scanner.getCurrentPosition() <= negativeExpression.getFileLocation().getNodeOffset()) {
scribe.alignFragment(alignment, 1);
}
scribe.setTailFormatter(tailFormatter);
negativeExpression.accept(this);
scribe.runTailFormatter();
@ -2817,16 +2824,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
do {
try {
for (int i = 0; i < elements.size(); i++) {
scribe.alignFragment(alignment, i);
int token= peekNextToken();
if (token == Token.tSHIFTL) {
scribe.printNextToken(token, preferences.insert_space_before_binary_operator);
scribe.printTrailingComment();
if (preferences.insert_space_after_binary_operator) {
scribe.space();
node= elements.get(i);
// In case of macros we may have already passed the operator position.
if (scribe.scanner.getCurrentPosition() < node.getFileLocation().getNodeOffset()) {
scribe.alignFragment(alignment, i);
int token= peekNextToken();
if (token == Token.tSHIFTL) {
scribe.printNextToken(token, preferences.insert_space_before_binary_operator);
scribe.printTrailingComment();
if (preferences.insert_space_after_binary_operator) {
scribe.space();
}
}
}
node= elements.get(i);
if (i == alignment.fragmentCount - 1) {
scribe.setTailFormatter(tailFormatter);
}

View file

@ -298,7 +298,7 @@ public class Alignment {
i = this.fragmentIndex;
do {
if (this.fragmentBreaks[i] == NONE) {
if ((this.mode & M_INDENT_ON_COLUMN) != 0 && i == 0) {
if ((this.mode & M_INDENT_ON_COLUMN) != 0 && isFirstBreakableFragment(i)) {
if (this.breakIndentationLevel <= this.alternativeBreakIndentationLevel) {
// Does not make sense to break here unless indentation is reduced.
break;
@ -371,6 +371,14 @@ public class Alignment {
return false; // Cannot split better
}
private boolean isFirstBreakableFragment(int i) {
while (--i >= 0) {
if (this.fragmentBreaks[i] != BREAK_NOT_ALLOWED)
return false;
}
return true;
}
private void eraseExistingBreaks(int startFragmentIndex) {
for (int j = startFragmentIndex + 1; j < this.fragmentIndentations.length; j++) {
if (this.fragmentBreaks[j] == BREAK) {

View file

@ -2535,6 +2535,57 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult();
}
//bool loooooooooooong_name(int, int);
//int loooong_name;
//int very_loooooooooooooooooooooooong_name;
//
//struct Stream {
//Stream& operator <<(const char*);
//};
//Stream GetStream();
//
//struct Voidifier {
//void operator&(Stream&);
//};
//
//#define MY_MACRO(a) (a) ? (void) 0 : Voidifier() & GetStream() << " "
//
//void test(const char* variable_with_a_loooong_name) {
// MY_MACRO(loooooooooooong_name(loooong_name,
// very_loooooooooooooooooooooooong_name))
// << variable_with_a_loooong_name;
//}
//bool loooooooooooong_name(int, int);
//int loooong_name;
//int very_loooooooooooooooooooooooong_name;
//
//struct Stream {
// Stream& operator <<(const char*);
//};
//Stream GetStream();
//
//struct Voidifier {
// void operator&(Stream&);
//};
//
//#define MY_MACRO(a) (a) ? (void) 0 : Voidifier() & GetStream() << " "
//
//void test(const char* variable_with_a_loooong_name) {
// MY_MACRO(loooooooooooong_name(loooong_name,
// very_loooooooooooooooooooooooong_name))
// << variable_with_a_loooong_name;
//}
public void testOverloadedLeftShiftChain_7() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2");
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_OVERLOADED_LEFT_SHIFT_CHAIN,
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION,
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
assertFormatterResult();
}
//struct Stream {
//Stream& operator <<(const char*);
//};
@ -2572,7 +2623,6 @@ public class CodeFormatterTest extends BaseUITestCase {
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
assertFormatterResult();
}
//int main() {
// std::vector<std::vector<int>> test;
// // some comment