From f360d64c77c71187041a08cc97bf132b7ede88c9 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 5 Mar 2012 18:52:58 -0800 Subject: [PATCH] Few formatting fixes. --- .../formatter/CodeFormatterVisitor.java | 30 +++++++---- .../internal/formatter/align/Alignment.java | 10 +++- .../cdt/ui/tests/text/CodeFormatterTest.java | 52 ++++++++++++++++++- 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index 44ad4462c9c..3d9610941ec 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -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); } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java index 16cfa690e16..b6cc3a4b953 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java @@ -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) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 747ca721377..dac004425f9 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -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> test; // // some comment