From 9c9a82a646a7b1aae315636afe8e52fd83be7342 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 3 Mar 2010 15:09:34 +0000 Subject: [PATCH] Bug 304150 - case within switch indentation issue Bug 303175 - enum with value-assignment indents badly --- .../cdt/ui/tests/text/CIndenterTest.java | 66 +++++++++++++++++++ .../internal/ui/text/CHeuristicScanner.java | 5 ++ .../cdt/internal/ui/text/CIndenter.java | 54 ++++++++++++--- .../eclipse/cdt/internal/ui/text/Symbols.java | 1 + 4 files changed, 118 insertions(+), 8 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 17e3b6e18ac..f66a63dbd13 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 @@ -850,4 +850,70 @@ public class CIndenterTest extends BaseUITestCase { public void testIndentationOfConstructorBodyWithFieldInitializer_Bug298282() throws Exception { assertIndenterResult(); } + + //class A { + // enum E1 { + // a=1, + // b=2, + // c=3 + // }; + // enum E2 { + // x=1, + // y=2, + // z=3 + // }; + //}; + + //class A { + // enum E1 { + // a=1, + // b=2, + // c=3 + // }; + // enum E2 { + // x=1, + // y=2, + // z=3 + // }; + //}; + public void testIndentationOfEnumeratorDeclWithInitializer_Bug303175() throws Exception { + assertIndenterResult(); + } + + //void f() { + //switch(i) { + //case -2: + //f(); + //break; + //case -1: + //f(); + //break; + //case 0: + //f(); + //break; + //case +1: + //f(); + //break; + //} + //} + + //void f() { + // switch(i) { + // case -2: + // f(); + // break; + // case -1: + // f(); + // break; + // case 0: + // f(); + // break; + // case +1: + // f(); + // break; + // } + //} + public void testIndentationOfCaseWithSignedConstant_Bug304150() throws Exception { + assertIndenterResult(); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java index 09f80650249..e6d4d18cc92 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CHeuristicScanner.java @@ -60,6 +60,7 @@ public final class CHeuristicScanner implements Symbols { private static final char RANGLE= '>'; private static final char DOT= '.'; private static final char MINUS= '-'; + private static final char PLUS= '+'; private static final char TILDE= '~'; /** @@ -385,6 +386,8 @@ public final class CHeuristicScanner implements Symbols { return TokenARROW; } return TokenMINUS; + case PLUS: + return TokenPLUS; case TILDE: return TokenTILDE; } @@ -486,6 +489,8 @@ public final class CHeuristicScanner implements Symbols { return TokenDOT; case MINUS: return TokenMINUS; + case PLUS: + return TokenPLUS; case TILDE: return TokenTILDE; } 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 d57a5c6de76..46f88bd562d 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 @@ -1121,8 +1121,7 @@ public final class CIndenter { } if (fToken == Symbols.TokenCLASS || fToken == Symbols.TokenSTRUCT - || fToken == Symbols.TokenUNION - || fToken == Symbols.TokenENUM) { + || fToken == Symbols.TokenUNION) { // inside a type declaration? Only so if not preceded by '(' or ',' as in // a parameter list. To be safe, only accept ';' or EOF int pos= fPosition; @@ -1154,6 +1153,9 @@ public final class CIndenter { while (skipQualifiers()) { nextToken(); } + while (fToken == Symbols.TokenMINUS || fToken == Symbols.TokenPLUS) { + nextToken(); + } if (fToken == Symbols.TokenCASE) { return true; } @@ -1242,6 +1244,32 @@ public final class CIndenter { return false; } + /** + * Test whether the left brace at the current position marks an enum decl. + * + * @return true if this looks like an enum decl. + */ + private boolean looksLikeEnumDeclaration() { + int pos = fPosition; + nextToken(); + switch (fToken) { + case Symbols.TokenIDENT: + nextToken(); + while (skipQualifiers()) { + nextToken(); + } + switch (fToken) { + case Symbols.TokenENUM: + fPosition = pos; + return true; + } + break; + } + fPosition = pos; + return false; + } + + /** * Test whether the colon at the current position marks an access specifier. * @@ -1294,7 +1322,6 @@ public final class CIndenter { return fPosition; case Symbols.TokenCLASS: - case Symbols.TokenENUM: case Symbols.TokenSTRUCT: case Symbols.TokenUNION: isTypeBody= true; @@ -1445,6 +1472,8 @@ public final class CIndenter { continue; case Symbols.TokenDOUBLECOLON: case Symbols.TokenOTHER: + case Symbols.TokenMINUS: + case Symbols.TokenPLUS: continue; case Symbols.TokenQUESTIONMARK: @@ -1589,7 +1618,7 @@ public final class CIndenter { int lineOffset= fDocument.getLineOffset(startLine); int bound= Math.min(fDocument.getLength(), startPosition + 1); if ((fToken == Symbols.TokenSEMICOLON || fToken == Symbols.TokenRBRACE || - fToken == Symbols.TokenLBRACE && !looksLikeArrayInitializerIntro()) && + fToken == Symbols.TokenLBRACE && !looksLikeArrayInitializerIntro() && !looksLikeEnumDeclaration()) && (seenEqual || seenShiftLeft || seenRightParen)) { fIndent = fPrefs.prefContinuationIndent; } else { @@ -1765,6 +1794,8 @@ public final class CIndenter { fIndent= fPrefs.prefArrayIndent; } else if (isNamespace() || isLinkageSpec()) { fIndent= fPrefs.prefNamespaceBodyIndent; + } else if (looksLikeEnumDeclaration()) { + fIndent = fPrefs.prefTypeIndent; } else { int typeDeclPos = matchTypeDeclaration(); if (typeDeclPos == CHeuristicScanner.NOT_FOUND) { @@ -1853,6 +1884,7 @@ public final class CIndenter { fPosition= pos; return true; } + fPosition= pos; return false; } @@ -1863,18 +1895,19 @@ public final class CIndenter { * @return true if the next elements look like the start of a namespace declaration. */ private boolean isNamespace() { + int pos = fPosition; + nextToken(); if (fToken == Symbols.TokenNAMESPACE) { + fPosition = pos; return true; // Anonymous namespace } else if (fToken == Symbols.TokenIDENT) { - int pos = fPosition; - int token = fToken; nextToken(); // Get previous token if (fToken == Symbols.TokenNAMESPACE) { + fPosition = pos; return true; // Named namespace } - fToken = token; - fPosition = pos; } + fPosition = pos; return false; } @@ -1884,9 +1917,13 @@ public final class CIndenter { * @return true if the next elements look like the start of a linkage spec. */ private boolean isLinkageSpec() { + int pos = fPosition; + nextToken(); if (fToken == Symbols.TokenEXTERN) { + fPosition = pos; return true; } + fPosition = pos; return false; } @@ -2126,6 +2163,7 @@ public final class CIndenter { case Symbols.TokenGREATERTHAN: case Symbols.TokenLESSTHAN: case Symbols.TokenMINUS: + case Symbols.TokenPLUS: case Symbols.TokenSHIFTRIGHT: case Symbols.TokenSHIFTLEFT: case Symbols.TokenDELETE: diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java index 2e5b432f436..786d737992f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/Symbols.java @@ -38,6 +38,7 @@ public interface Symbols { int TokenARROW= 19; int TokenDOUBLECOLON= 20; int TokenSHIFTLEFT= 21; + int TokenPLUS= 22; int TokenIF= 109; int TokenDO= 1010; int TokenFOR= 1011;