diff options
-rw-r--r-- | core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java | 94 | ||||
-rw-r--r-- | core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java | 85 |
2 files changed, 133 insertions, 46 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java index f0913658234..85622a20f79 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java @@ -88,7 +88,7 @@ public class CIndenterTest extends BaseUITestCase { //foo(arg, // "string"); - public void testStringLiteralAsLastArgument_Bug192412_1() throws Exception { + public void testStringLiteralAsLastArgument_1_Bug192412() throws Exception { assertIndenterResult(); } @@ -97,7 +97,7 @@ public class CIndenterTest extends BaseUITestCase { //a::foo(arg, // "string"); - public void testStringLiteralAsLastArgument_Bug192412_2() throws Exception { + public void testStringLiteralAsLastArgument_2_Bug192412() throws Exception { assertIndenterResult(); } @@ -106,7 +106,7 @@ public class CIndenterTest extends BaseUITestCase { //a::foo(arg, // "string"); - public void testStringLiteralAsLastArgument_Bug192412_3() throws Exception { + public void testStringLiteralAsLastArgument_3_Bug192412() throws Exception { assertIndenterResult(); } @@ -117,7 +117,7 @@ public class CIndenterTest extends BaseUITestCase { //if (1) // foo->bar(); //dontIndent(); - public void testIndentationAfterArrowOperator_Bug192412_4() throws Exception { + public void testIndentationAfterArrowOperator_Bug192412() throws Exception { assertIndenterResult(); } @@ -128,7 +128,7 @@ public class CIndenterTest extends BaseUITestCase { //if (1) // foo>>bar; //dontIndent(); - public void testIndentationAfterShiftRight_Bug192412_5() throws Exception { + public void testIndentationAfterShiftRight_Bug192412() throws Exception { assertIndenterResult(); } @@ -139,7 +139,7 @@ public class CIndenterTest extends BaseUITestCase { //if (1) // foo >= bar(); //dontIndent(); - public void testIndentationAfterGreaterOrEquals_Bug192412_6() throws Exception { + public void testIndentationAfterGreaterOrEquals_Bug192412() throws Exception { assertIndenterResult(); } @@ -152,7 +152,7 @@ public class CIndenterTest extends BaseUITestCase { // const BinFileParser::Exception& exp) //{ //} - public void testOperatorMethodBody_Bug192412_7() throws Exception { + public void testOperatorMethodBody_1_Bug192412() throws Exception { assertIndenterResult(); } @@ -165,7 +165,7 @@ public class CIndenterTest extends BaseUITestCase { // const BinFileParser::Exception& exp) //{ //} - public void testOperatorMethodBody_Bug192412_8() throws Exception { + public void testOperatorMethodBody_2_Bug192412() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_COMPACT, @@ -307,19 +307,38 @@ public class CIndenterTest extends BaseUITestCase { //class A { //A(int a, //int b) - //:f(0) - //{ + //: f(0), + //g(0) { //} //}; //class A { // A(int a, // int b) - // :f(0) - // { + // : f(0), + // g(0) { + // } + //}; + public void testConstructorBodyWithInitializer_1_Bug194586() throws Exception { + assertIndenterResult(); + } + + //class A { + //public: + //A(int a, int b) : + //f(0), + //g(0) { + //} + //}; + + //class A { + //public: + // A(int a, int b) : + // f(0), + // g(0) { // } //}; - public void testConstructorBodyWithInitializer_Bug194586() throws Exception { + public void testConstructorBodyWithInitializer_2() throws Exception { assertIndenterResult(); } @@ -417,7 +436,7 @@ public class CIndenterTest extends BaseUITestCase { // private: // int a; //}; - public void testClassDeclaration_278713() throws Exception { + public void testClassDeclaration_Bug278713() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, @@ -466,7 +485,7 @@ public class CIndenterTest extends BaseUITestCase { //union DisUnion // { // }; - public void testIndentedClass_Bug210417() throws Exception { + public void testIndentedClass_1_Bug210417() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED); assertIndenterResult(); @@ -481,7 +500,7 @@ public class CIndenterTest extends BaseUITestCase { //class MyClass : public Base // { // }; - public void testIndentedClass_Bug210417_2() throws Exception { + public void testIndentedClass_2_Bug210417() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED); assertIndenterResult(); @@ -496,7 +515,7 @@ public class CIndenterTest extends BaseUITestCase { //class MyClass : public Base, public OtherBase // { // }; - public void testIndentedClass_Bug210417_3() throws Exception { + public void testIndentedClass_3_Bug210417() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED); assertIndenterResult(); @@ -511,7 +530,7 @@ public class CIndenterTest extends BaseUITestCase { //class MyClass : public Base, public OtherBase // { // }; - public void testIndentedClass_Bug210417_4() throws Exception { + public void testIndentedClass_4_Bug210417() throws Exception { fOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED); assertIndenterResult(); @@ -522,7 +541,7 @@ public class CIndenterTest extends BaseUITestCase { //x = // 0; - public void testWrappedAssignment_277624_1() throws Exception { + public void testWrappedAssignment_1_Bug277624() throws Exception { assertIndenterResult(); } @@ -537,7 +556,7 @@ public class CIndenterTest extends BaseUITestCase { // x = 2 + // 2 + // 2; - public void testWrappedAssignment_277624_2() throws Exception { + public void testWrappedAssignment_2_Bug277624() throws Exception { assertIndenterResult(); } @@ -548,16 +567,29 @@ public class CIndenterTest extends BaseUITestCase { //if (1 > 0) { // double d = a * b / // c; - public void testWrappedAssignment_277624_3() throws Exception { + public void testWrappedAssignment_3_Bug277624() throws Exception { assertIndenterResult(); } - + + //int x = 1 < 2 ? + //f(0) : + //1; + //g(); + + //int x = 1 < 2 ? + // f(0) : + // 1; + //g(); + public void testConditionalExpression_Bug283970() throws Exception { + assertIndenterResult(); + } + //for (int i = 0; //i < 2; i++) //for (int i = 0; // i < 2; i++) - public void testWrappedFor_277625_1() throws Exception { + public void testWrappedFor_1_Bug277625() throws Exception { assertIndenterResult(); } @@ -566,7 +598,7 @@ public class CIndenterTest extends BaseUITestCase { //for (int i = 0; i < 2; // i++) - public void testWrappedFor_277625_2() throws Exception { + public void testWrappedFor_2_Bug277625() throws Exception { assertIndenterResult(); } @@ -579,7 +611,7 @@ public class CIndenterTest extends BaseUITestCase { // i < 2; // i++) //{ - public void testWrappedFor_277625_3() throws Exception { + public void testWrappedFor_3_Bug277625() throws Exception { assertIndenterResult(); } @@ -590,7 +622,7 @@ public class CIndenterTest extends BaseUITestCase { //; //for (hash_map<Node*, double>::const_iterator it = container_.begin(); // it != container_.end(); ++it) { - public void testWrappedFor_277625_4() throws Exception { + public void testWrappedFor_4_Bug277625() throws Exception { assertIndenterResult(); } @@ -663,7 +695,7 @@ public class CIndenterTest extends BaseUITestCase { //public: // A(); // }; - public void testWhiteSmithsAccessSpecifier_Bug204575_1() throws Exception { + public void testWhiteSmithsAccessSpecifier_1_Bug204575() throws Exception { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.FALSE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); @@ -681,7 +713,7 @@ public class CIndenterTest extends BaseUITestCase { // public: // A(); // }; - public void testWhiteSmithsAccessSpecifier_Bug204575_2() throws Exception { + public void testWhiteSmithsAccessSpecifier_2_Bug204575() throws Exception { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.FALSE); @@ -699,7 +731,7 @@ public class CIndenterTest extends BaseUITestCase { // public: // A(); // }; - public void testWhiteSmithsAccessSpecifier_Bug204575_3() throws Exception { + public void testWhiteSmithsAccessSpecifier_3_Bug204575() throws Exception { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); @@ -727,7 +759,7 @@ public class CIndenterTest extends BaseUITestCase { // doOther(); // } // } - public void testWhiteSmithsSwitch1() throws Exception { + public void testWhiteSmithsSwitch_1() throws Exception { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); @@ -756,7 +788,7 @@ public class CIndenterTest extends BaseUITestCase { // doOther(); // } // } - public void testWhiteSmithsSwitch2() throws Exception { + public void testWhiteSmithsSwitch_2() throws Exception { fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap()); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, DefaultCodeFormatterConstants.FALSE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.TRUE); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java index 30b986502bc..7424308ec71 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CIndenter.java @@ -671,7 +671,6 @@ public final class CIndenter { // remainder while (spaces-- > 0) ret.append(' '); - } catch (BadLocationException e) { } @@ -961,6 +960,7 @@ public final class CIndenter { nextToken(); } + int line= fLine; switch (fToken) { case Symbols.TokenGREATERTHAN: case Symbols.TokenRBRACE: @@ -1003,18 +1003,27 @@ public final class CIndenter { fIndent= fPrefs.prefBlockIndent; return pos; } - // TODO handle ternary deep indentation + fPosition= pos; + if (looksLikeConstructorInitializer()) { + fIndent= fPrefs.prefBlockIndent; + return pos; + } + fPosition= pos; + if (isConditional()) { + fPosition= offset; + fLine= line; + return skipToPreviousListItemOrListStart(); + } fPosition= pos; return skipToStatementStart(danglingElse, false); case Symbols.TokenQUESTIONMARK: if (fPrefs.prefTernaryDeepAlign) { setFirstElementAlignment(fPosition, offset + 1); - return fPosition; } else { fIndent= fPrefs.prefTernaryIndent; - return fPosition; } + return fPosition; // indentation for blockless introducers: case Symbols.TokenDO: @@ -1027,7 +1036,6 @@ public final class CIndenter { return skipToStatementStart(danglingElse, false); case Symbols.TokenRPAREN: - int line= fLine; if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) { int scope= fPosition; nextToken(); @@ -1144,7 +1152,7 @@ public final class CIndenter { /** * Test whether the colon at the current position marks a type inheritance decl. * - * @return <code>true</code> if this looks like a a type inheritance decl + * @return <code>true</code> if this looks like a type inheritance decl. */ private boolean looksLikeTypeInheritanceDecl() { nextToken(); @@ -1161,10 +1169,53 @@ public final class CIndenter { return true; } break; - case Symbols.TokenRPAREN: // constructor initializer - case Symbols.TokenPUBLIC: - case Symbols.TokenPROTECTED: - case Symbols.TokenPRIVATE: + } + return false; + } + + /** + * Test whether the colon at the current position marks a constructor initializer list. + * + * @return <code>true</code> if this looks like a constructor initializer list. + */ + private boolean looksLikeConstructorInitializer() { + nextToken(); + if (fToken != Symbols.TokenRPAREN) { + return false; + } + if (!skipScope()) { + return false; + } + nextToken(); + if (fToken == Symbols.TokenTHROW) { + nextToken(); + if (fToken != Symbols.TokenRPAREN) { + return false; + } + if (!skipScope()) { + return false; + } + nextToken(); + } + if (fToken != Symbols.TokenIDENT) { + return false; + } + nextToken(); + switch (fToken) { + case Symbols.TokenCOLON: + nextToken(); + switch (fToken) { + case Symbols.TokenCOLON: // A::A() : + case Symbols.TokenPUBLIC: // public: A() : + case Symbols.TokenPROTECTED: + case Symbols.TokenPRIVATE: + return true; + } + return false; + + case Symbols.TokenLBRACE: // class A { A() : + case Symbols.TokenRBRACE: + case Symbols.TokenSEMICOLON: return true; } return false; @@ -1250,9 +1301,9 @@ public final class CIndenter { fIndent = fPrefs.prefContinuationIndent; return fPosition; } - //$FALL-THROUGH$ + break; + case Symbols.TokenLBRACE: - case Symbols.TokenLBRACKET: case Symbols.TokenSEMICOLON: case Symbols.TokenEOF: if (isInBlock) @@ -1511,7 +1562,7 @@ public final class CIndenter { while (true) { nextToken(); - // if any line item comes with its own indentation, adapt to it + // If any line item comes with its own indentation, adapt to it if (fLine < startLine) { try { int lineOffset= fDocument.getLineOffset(startLine); @@ -1522,9 +1573,13 @@ public final class CIndenter { fIndent = fPrefs.prefContinuationIndent; } else { fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, bound); + // If the reference line starts with a colon, skip the colon. + if (peekToken(fAlign) == Symbols.TokenCOLON) { + fAlign= fScanner.findNonWhitespaceForwardInAnyPartition(fAlign + 1, bound); + } } } catch (BadLocationException e) { - // ignore and return just the position + // Ignore and return just the position } return startPosition; } @@ -1955,7 +2010,7 @@ public final class CIndenter { * Returns <code>true</code> if the current tokens look like a method * declaration header (i.e. only the return type and method name). The * heuristic calls <code>nextToken</code> and expects an identifier - * (method name) and an optional retrun type declaration. + * (method name) and an optional return type declaration. * * @return <code>true</code> if the current position looks like a method * declaration header. |