From 9531500aacb6b6d627feae5219a1276f5302c65f Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 2 Jun 2009 03:38:06 +0000 Subject: [PATCH] Bug 278713. Also added a hidden option that enables fractional indent of public/protected/private. Google coding standard requires access specifiers to be indented by 1/2 of the indentation unit. --- .../cdt/ui/tests/text/CIndenterTest.java | 62 ++++++++++++++++- .../cdt/internal/ui/editor/IndentUtil.java | 6 +- .../internal/ui/text/CAutoIndentStrategy.java | 9 +-- .../cdt/internal/ui/text/CIndenter.java | 67 +++++++++++++------ 4 files changed, 114 insertions(+), 30 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 61818f9f92c..7556c99e9e5 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2009 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * Anton Leherbauer (Wind River Systems) - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.ui.tests.text; @@ -280,7 +281,64 @@ public class CIndenterTest extends BaseUITestCase { public void testIndentationOfNestedInitializerLists_Bug194585() throws Exception { assertIndenterResult(); } - + + //class MyClass { + //typedef int MyType; + //public: + //int getA() { + //return a; + //} + //MyClass(); + //protected: + //private: + //int a; + //}; + + //class MyClass { + // typedef int MyType; + // public: + // int getA() { + // return a; + // } + // MyClass(); + // protected: + // private: + // int a; + //}; + public void testClassDeclaration_278713() 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, + DefaultCodeFormatterConstants.TRUE); + assertIndenterResult(); + } + + //namespace ns { + //class A; + //} + + //namespace ns { + // class A; + //} + public void testNamespace_1() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER, + DefaultCodeFormatterConstants.TRUE); + assertIndenterResult(); + } + + //namespace ns { + //class A; + //} + + //namespace ns { + //class A; + //} + public void testNamespace_2() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER, + DefaultCodeFormatterConstants.FALSE); + assertIndenterResult(); + } + //// a comment //class MyClass //{ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java index f58a72e759b..0b0578f2cd1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/IndentUtil.java @@ -433,7 +433,9 @@ public final class IndentUtil { * false if not * @throws BadLocationException if the document got changed concurrently */ - private static boolean indentLine(IDocument document, int line, CIndenter indenter, CHeuristicScanner scanner, boolean[] commentLines, int lineIndex, int tabSize, boolean indentInsideLineComments) throws BadLocationException { + private static boolean indentLine(IDocument document, int line, CIndenter indenter, + CHeuristicScanner scanner, boolean[] commentLines, int lineIndex, int tabSize, + boolean indentInsideLineComments) throws BadLocationException { IRegion currentLine= document.getLineInformation(line); final int offset= currentLine.getOffset(); int wsStart= offset; // where we start searching for non-WS; after the "//" in single line comments @@ -599,7 +601,7 @@ public final class IndentUtil { CHeuristicScanner ppScanner= new CHeuristicScanner(document, ICPartitions.C_PARTITIONING, partition.getType()); CIndenter ppIndenter= new CIndenter(document, ppScanner); if (line == ppFirstLine + 1) { - return ppIndenter.createReusingIndent(new StringBuilder(), ppIndenter.getContinuationLineIndent()).toString(); + return ppIndenter.createReusingIndent(new StringBuilder(), ppIndenter.getContinuationLineIndent(), 0).toString(); } StringBuilder computed= ppIndenter.computeIndentation(document.getLineOffset(line), false); if (computed != null) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java index c4126fefe5f..2c4a993a68d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java @@ -280,7 +280,7 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { indent= new StringBuilder(); } if (addIndent > 0 && indent.length() == 0) { - indent= indenter.createReusingIndent(indent, addIndent); + indent= indenter.createReusingIndent(indent, addIndent, 0); } StringBuilder buf = new StringBuilder(c.text + indent); @@ -1078,10 +1078,11 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { String indent; if (nextToken == Symbols.TokenCASE || nextToken == Symbols.TokenDEFAULT || nextToken == Symbols.TokenPUBLIC || nextToken == Symbols.TokenPROTECTED || - nextToken == Symbols.TokenPRIVATE) + nextToken == Symbols.TokenPRIVATE) { indent = getIndentOfLine(doc, refLine); - else // at the brace of the switch or the class + } else { // at the brace of the switch or the class indent = indenter.computeIndentation(p).toString(); + } if (indent != null) { c.text = indent.toString() + doc.get(p, offset - p) + c.text; @@ -1101,7 +1102,7 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { ITypedRegion partition= TextUtilities.getPartition(doc, fPartitioning, c.offset, false); if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) { IRegion startLine= doc.getLineInformationOfOffset(c.offset); - String indent= (doc.get(startLine.getOffset(), c.offset - startLine.getOffset())); + String indent= doc.get(startLine.getOffset(), c.offset - startLine.getOffset()); if (indent.trim().length() == 0) { c.offset -= indent.length(); c.length += indent.length(); 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 6d32d16b2ad..4ef796732da 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 @@ -13,6 +13,8 @@ package org.eclipse.cdt.internal.ui.text; import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.IPreferencesService; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; @@ -61,6 +63,7 @@ public final class CIndenter { final int prefMethodBodyIndent; final int prefTypeIndent; final int prefAccessSpecifierIndent; + final int prefAccessSpecifierExtraSpaces; final int prefNamespaceBodyIndent; final boolean prefIndentBracesForBlocks; final boolean prefIndentBracesForArrays; @@ -111,6 +114,7 @@ public final class CIndenter { prefMethodBodyIndent= prefMethodBodyIndent(); prefTypeIndent= prefTypeIndent(); prefAccessSpecifierIndent= prefAccessSpecifierIndent(); + prefAccessSpecifierExtraSpaces= prefAccessSpecifierExtraSpaces(); prefNamespaceBodyIndent= prefNamespaceBodyIndent(); prefIndentBracesForArrays= prefIndentBracesForArrays(); prefIndentBracesForMethods= prefIndentBracesForMethods(); @@ -203,7 +207,8 @@ public final class CIndenter { private int prefSimpleIndent() { if (prefIndentBracesForBlocks() && prefBlockIndent() == 0) return 1; - else return prefBlockIndent(); + else + return prefBlockIndent(); } private int prefBracketIndent() { @@ -304,6 +309,12 @@ public final class CIndenter { return 0; } + private int prefAccessSpecifierExtraSpaces() { + // Hidden option that enables fractional indent of access specifiers. + IPreferencesService prefs = Platform.getPreferencesService(); + return prefs.getInt(CCorePlugin.PLUGIN_ID, CCorePlugin.PLUGIN_ID + ".formatter.indent_access_specifier_extra_spaces", 0, null); //$NON-NLS-1$ + } + private int prefNamespaceBodyIndent() { if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER))) return prefBlockIndent(); @@ -346,6 +357,8 @@ public final class CIndenter { private final IDocument fDocument; /** The indentation accumulated by findReferencePosition. */ private int fIndent; + /** Extra spaces to add on top of fIndent */ + private int fExtraSpaces; /** * The absolute (character-counted) indentation offset for special cases * (method defs, array initializers) @@ -473,7 +486,7 @@ public final class CIndenter { return null; // Add additional indent - return createReusingIndent(reference, fIndent); + return createReusingIndent(reference, fIndent, fExtraSpaces); } /** @@ -492,7 +505,7 @@ public final class CIndenter { if (string.trim().length() == 0) return reference; // Add additional indent - return createReusingIndent(reference, fPrefs.prefContinuationIndent); + return createReusingIndent(reference, fPrefs.prefContinuationIndent, 0); } /** @@ -637,12 +650,13 @@ public final class CIndenter { * @param buffer the original indent to reuse if possible * @param additional the additional indentation units to add or subtract to * reference + * @param extraSpaces additional spaces to add to indentation. * @return the modified buffer reflecting the indentation * adapted to additional */ - public StringBuilder createReusingIndent(StringBuilder buffer, int additional) { + public StringBuilder createReusingIndent(StringBuilder buffer, int additional, int extraSpaces) { int refLength= computeVisualLength(buffer); - int addLength= fPrefs.prefIndentationSize * additional; // may be < 0 + int addLength= fPrefs.prefIndentationSize * additional + extraSpaces; // may be < 0 int totalLength= Math.max(0, refLength + addLength); // copy the reference indentation for the indent up to the last tab @@ -907,6 +921,11 @@ public final class CIndenter { } nextToken(); + // Skip access specifiers + while (fToken == Symbols.TokenCOLON && isAccessSpecifier()) { + nextToken(); + } + switch (fToken) { case Symbols.TokenGREATERTHAN: case Symbols.TokenRBRACE: @@ -940,11 +959,6 @@ public final class CIndenter { case Symbols.TokenCOLON: pos= fPosition; - if (isAccessSpecifier()) { - fIndent= fPrefs.prefTypeIndent; - return pos; - } - fPosition= pos; if (looksLikeCaseStatement()) { fIndent= fPrefs.prefCaseBlockIndent; return pos; @@ -1127,6 +1141,8 @@ public final class CIndenter { * @return true if current position marks an access specifier */ private boolean isAccessSpecifier() { + int pos= fPosition; + int token = fToken; nextToken(); switch (fToken) { case Symbols.TokenPUBLIC: @@ -1134,6 +1150,8 @@ public final class CIndenter { case Symbols.TokenPRIVATE: return true; } + fToken = token; + fPosition= pos; return false; } @@ -1291,12 +1309,13 @@ public final class CIndenter { } private int getBlockIndent(boolean isMethodBody, boolean isTypeBody) { - if (isTypeBody) - return fPrefs.prefTypeIndent + (fPrefs.prefIndentBracesForTypes ? 1 : 0); - else if (isMethodBody) + if (isTypeBody) { + return fPrefs.prefTypeIndent + fPrefs.prefAccessSpecifierIndent; + } else if (isMethodBody) { return fPrefs.prefMethodBodyIndent + (fPrefs.prefIndentBracesForMethods ? 1 : 0); - else + } else { return fIndent; + } } /** @@ -1412,6 +1431,7 @@ public final class CIndenter { int pos= fPosition; int typeDeclPos= matchTypeDeclaration(); fIndent= fPrefs.prefAccessSpecifierIndent; + fExtraSpaces = fPrefs.prefAccessSpecifierExtraSpaces; if (typeDeclPos != CHeuristicScanner.NOT_FOUND) { return typeDeclPos; } @@ -1582,11 +1602,11 @@ public final class CIndenter { * @return the indent */ private int handleScopeIntroduction(int bound) { + int pos= fPosition; // store + switch (fToken) { // scope introduction: special treat who special is case Symbols.TokenLPAREN: - int pos= fPosition; // store - // special: method declaration deep indentation if (looksLikeMethodDecl()) { if (fPrefs.prefMethodDeclDeepIndent) { @@ -1614,8 +1634,6 @@ public final class CIndenter { return pos; case Symbols.TokenLBRACE: - pos= fPosition; // store - final boolean looksLikeArrayInitializerIntro= looksLikeArrayInitializerIntro(); // special: array initializer if (looksLikeArrayInitializerIntro) { @@ -1626,12 +1644,17 @@ public final class CIndenter { } else if (isNamespace()) { fIndent= fPrefs.prefNamespaceBodyIndent; } else { - fIndent= fPrefs.prefBlockIndent; + int typeDeclPos = matchTypeDeclaration(); + if (typeDeclPos == CHeuristicScanner.NOT_FOUND) { + fIndent= fPrefs.prefBlockIndent; + } else { + fIndent= fPrefs.prefAccessSpecifierIndent + fPrefs.prefTypeIndent; + } } // normal: skip to the statement start before the scope introducer // opening braces are often on differently ending indents than e.g. a method definition - if (!looksLikeArrayInitializerIntro && !fPrefs.prefIndentBracesForBlocks) { + if (!looksLikeArrayInitializerIntro) { fPosition= pos; // restore return skipToStatementStart(true, true); // set to true to match the first if } else { @@ -1639,8 +1662,6 @@ public final class CIndenter { } case Symbols.TokenLBRACKET: - pos= fPosition; // store - // special: method declaration deep indentation if (fPrefs.prefArrayDimensionsDeepIndent) { return setFirstElementAlignment(pos, bound); @@ -1710,6 +1731,7 @@ public final class CIndenter { * @return true if the next elements look like the start of a namespace declaration. */ private boolean isNamespace() { + int pos = fPosition; if (fToken == Symbols.TokenNAMESPACE) { return true; // Anonymous namespace } else if (fToken == Symbols.TokenIDENT) { @@ -1718,6 +1740,7 @@ public final class CIndenter { return true; // Named namespace } } + fPosition = pos; return false; }