1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-17 22:15:23 +02:00

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.

This commit is contained in:
Sergey Prigogin 2009-06-02 03:38:06 +00:00
parent 354541a130
commit 9531500aac
4 changed files with 114 additions and 30 deletions

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation * Anton Leherbauer (Wind River Systems) - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.ui.tests.text; package org.eclipse.cdt.ui.tests.text;
@ -280,7 +281,64 @@ public class CIndenterTest extends BaseUITestCase {
public void testIndentationOfNestedInitializerLists_Bug194585() throws Exception { public void testIndentationOfNestedInitializerLists_Bug194585() throws Exception {
assertIndenterResult(); 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 //// a comment
//class MyClass //class MyClass
//{ //{

View file

@ -433,7 +433,9 @@ public final class IndentUtil {
* <code>false</code> if not * <code>false</code> if not
* @throws BadLocationException if the document got changed concurrently * @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); IRegion currentLine= document.getLineInformation(line);
final int offset= currentLine.getOffset(); final int offset= currentLine.getOffset();
int wsStart= offset; // where we start searching for non-WS; after the "//" in single line comments 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()); CHeuristicScanner ppScanner= new CHeuristicScanner(document, ICPartitions.C_PARTITIONING, partition.getType());
CIndenter ppIndenter= new CIndenter(document, ppScanner); CIndenter ppIndenter= new CIndenter(document, ppScanner);
if (line == ppFirstLine + 1) { 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); StringBuilder computed= ppIndenter.computeIndentation(document.getLineOffset(line), false);
if (computed != null) { if (computed != null) {

View file

@ -280,7 +280,7 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
indent= new StringBuilder(); indent= new StringBuilder();
} }
if (addIndent > 0 && indent.length() == 0) { if (addIndent > 0 && indent.length() == 0) {
indent= indenter.createReusingIndent(indent, addIndent); indent= indenter.createReusingIndent(indent, addIndent, 0);
} }
StringBuilder buf = new StringBuilder(c.text + indent); StringBuilder buf = new StringBuilder(c.text + indent);
@ -1078,10 +1078,11 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
String indent; String indent;
if (nextToken == Symbols.TokenCASE || nextToken == Symbols.TokenDEFAULT || if (nextToken == Symbols.TokenCASE || nextToken == Symbols.TokenDEFAULT ||
nextToken == Symbols.TokenPUBLIC || nextToken == Symbols.TokenPROTECTED || nextToken == Symbols.TokenPUBLIC || nextToken == Symbols.TokenPROTECTED ||
nextToken == Symbols.TokenPRIVATE) nextToken == Symbols.TokenPRIVATE) {
indent = getIndentOfLine(doc, refLine); 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(); indent = indenter.computeIndentation(p).toString();
}
if (indent != null) { if (indent != null) {
c.text = indent.toString() + doc.get(p, offset - p) + c.text; 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); ITypedRegion partition= TextUtilities.getPartition(doc, fPartitioning, c.offset, false);
if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) { if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
IRegion startLine= doc.getLineInformationOfOffset(c.offset); 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) { if (indent.trim().length() == 0) {
c.offset -= indent.length(); c.offset -= indent.length();
c.length += indent.length(); c.length += indent.length();

View file

@ -13,6 +13,8 @@
package org.eclipse.cdt.internal.ui.text; package org.eclipse.cdt.internal.ui.text;
import org.eclipse.core.runtime.Assert; 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.BadLocationException;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.IRegion;
@ -61,6 +63,7 @@ public final class CIndenter {
final int prefMethodBodyIndent; final int prefMethodBodyIndent;
final int prefTypeIndent; final int prefTypeIndent;
final int prefAccessSpecifierIndent; final int prefAccessSpecifierIndent;
final int prefAccessSpecifierExtraSpaces;
final int prefNamespaceBodyIndent; final int prefNamespaceBodyIndent;
final boolean prefIndentBracesForBlocks; final boolean prefIndentBracesForBlocks;
final boolean prefIndentBracesForArrays; final boolean prefIndentBracesForArrays;
@ -111,6 +114,7 @@ public final class CIndenter {
prefMethodBodyIndent= prefMethodBodyIndent(); prefMethodBodyIndent= prefMethodBodyIndent();
prefTypeIndent= prefTypeIndent(); prefTypeIndent= prefTypeIndent();
prefAccessSpecifierIndent= prefAccessSpecifierIndent(); prefAccessSpecifierIndent= prefAccessSpecifierIndent();
prefAccessSpecifierExtraSpaces= prefAccessSpecifierExtraSpaces();
prefNamespaceBodyIndent= prefNamespaceBodyIndent(); prefNamespaceBodyIndent= prefNamespaceBodyIndent();
prefIndentBracesForArrays= prefIndentBracesForArrays(); prefIndentBracesForArrays= prefIndentBracesForArrays();
prefIndentBracesForMethods= prefIndentBracesForMethods(); prefIndentBracesForMethods= prefIndentBracesForMethods();
@ -203,7 +207,8 @@ public final class CIndenter {
private int prefSimpleIndent() { private int prefSimpleIndent() {
if (prefIndentBracesForBlocks() && prefBlockIndent() == 0) if (prefIndentBracesForBlocks() && prefBlockIndent() == 0)
return 1; return 1;
else return prefBlockIndent(); else
return prefBlockIndent();
} }
private int prefBracketIndent() { private int prefBracketIndent() {
@ -304,6 +309,12 @@ public final class CIndenter {
return 0; 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() { private int prefNamespaceBodyIndent() {
if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER))) if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER)))
return prefBlockIndent(); return prefBlockIndent();
@ -346,6 +357,8 @@ public final class CIndenter {
private final IDocument fDocument; private final IDocument fDocument;
/** The indentation accumulated by <code>findReferencePosition</code>. */ /** The indentation accumulated by <code>findReferencePosition</code>. */
private int fIndent; private int fIndent;
/** Extra spaces to add on top of fIndent */
private int fExtraSpaces;
/** /**
* The absolute (character-counted) indentation offset for special cases * The absolute (character-counted) indentation offset for special cases
* (method defs, array initializers) * (method defs, array initializers)
@ -473,7 +486,7 @@ public final class CIndenter {
return null; return null;
// Add additional indent // 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) if (string.trim().length() == 0)
return reference; return reference;
// Add additional indent // 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 buffer the original indent to reuse if possible
* @param additional the additional indentation units to add or subtract to * @param additional the additional indentation units to add or subtract to
* reference * reference
* @param extraSpaces additional spaces to add to indentation.
* @return the modified <code>buffer</code> reflecting the indentation * @return the modified <code>buffer</code> reflecting the indentation
* adapted to <code>additional</code> * adapted to <code>additional</code>
*/ */
public StringBuilder createReusingIndent(StringBuilder buffer, int additional) { public StringBuilder createReusingIndent(StringBuilder buffer, int additional, int extraSpaces) {
int refLength= computeVisualLength(buffer); 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); int totalLength= Math.max(0, refLength + addLength);
// copy the reference indentation for the indent up to the last tab // copy the reference indentation for the indent up to the last tab
@ -907,6 +921,11 @@ public final class CIndenter {
} }
nextToken(); nextToken();
// Skip access specifiers
while (fToken == Symbols.TokenCOLON && isAccessSpecifier()) {
nextToken();
}
switch (fToken) { switch (fToken) {
case Symbols.TokenGREATERTHAN: case Symbols.TokenGREATERTHAN:
case Symbols.TokenRBRACE: case Symbols.TokenRBRACE:
@ -940,11 +959,6 @@ public final class CIndenter {
case Symbols.TokenCOLON: case Symbols.TokenCOLON:
pos= fPosition; pos= fPosition;
if (isAccessSpecifier()) {
fIndent= fPrefs.prefTypeIndent;
return pos;
}
fPosition= pos;
if (looksLikeCaseStatement()) { if (looksLikeCaseStatement()) {
fIndent= fPrefs.prefCaseBlockIndent; fIndent= fPrefs.prefCaseBlockIndent;
return pos; return pos;
@ -1127,6 +1141,8 @@ public final class CIndenter {
* @return <code>true</code> if current position marks an access specifier * @return <code>true</code> if current position marks an access specifier
*/ */
private boolean isAccessSpecifier() { private boolean isAccessSpecifier() {
int pos= fPosition;
int token = fToken;
nextToken(); nextToken();
switch (fToken) { switch (fToken) {
case Symbols.TokenPUBLIC: case Symbols.TokenPUBLIC:
@ -1134,6 +1150,8 @@ public final class CIndenter {
case Symbols.TokenPRIVATE: case Symbols.TokenPRIVATE:
return true; return true;
} }
fToken = token;
fPosition= pos;
return false; return false;
} }
@ -1291,12 +1309,13 @@ public final class CIndenter {
} }
private int getBlockIndent(boolean isMethodBody, boolean isTypeBody) { private int getBlockIndent(boolean isMethodBody, boolean isTypeBody) {
if (isTypeBody) if (isTypeBody) {
return fPrefs.prefTypeIndent + (fPrefs.prefIndentBracesForTypes ? 1 : 0); return fPrefs.prefTypeIndent + fPrefs.prefAccessSpecifierIndent;
else if (isMethodBody) } else if (isMethodBody) {
return fPrefs.prefMethodBodyIndent + (fPrefs.prefIndentBracesForMethods ? 1 : 0); return fPrefs.prefMethodBodyIndent + (fPrefs.prefIndentBracesForMethods ? 1 : 0);
else } else {
return fIndent; return fIndent;
}
} }
/** /**
@ -1412,6 +1431,7 @@ public final class CIndenter {
int pos= fPosition; int pos= fPosition;
int typeDeclPos= matchTypeDeclaration(); int typeDeclPos= matchTypeDeclaration();
fIndent= fPrefs.prefAccessSpecifierIndent; fIndent= fPrefs.prefAccessSpecifierIndent;
fExtraSpaces = fPrefs.prefAccessSpecifierExtraSpaces;
if (typeDeclPos != CHeuristicScanner.NOT_FOUND) { if (typeDeclPos != CHeuristicScanner.NOT_FOUND) {
return typeDeclPos; return typeDeclPos;
} }
@ -1582,11 +1602,11 @@ public final class CIndenter {
* @return the indent * @return the indent
*/ */
private int handleScopeIntroduction(int bound) { private int handleScopeIntroduction(int bound) {
int pos= fPosition; // store
switch (fToken) { switch (fToken) {
// scope introduction: special treat who special is // scope introduction: special treat who special is
case Symbols.TokenLPAREN: case Symbols.TokenLPAREN:
int pos= fPosition; // store
// special: method declaration deep indentation // special: method declaration deep indentation
if (looksLikeMethodDecl()) { if (looksLikeMethodDecl()) {
if (fPrefs.prefMethodDeclDeepIndent) { if (fPrefs.prefMethodDeclDeepIndent) {
@ -1614,8 +1634,6 @@ public final class CIndenter {
return pos; return pos;
case Symbols.TokenLBRACE: case Symbols.TokenLBRACE:
pos= fPosition; // store
final boolean looksLikeArrayInitializerIntro= looksLikeArrayInitializerIntro(); final boolean looksLikeArrayInitializerIntro= looksLikeArrayInitializerIntro();
// special: array initializer // special: array initializer
if (looksLikeArrayInitializerIntro) { if (looksLikeArrayInitializerIntro) {
@ -1626,12 +1644,17 @@ public final class CIndenter {
} else if (isNamespace()) { } else if (isNamespace()) {
fIndent= fPrefs.prefNamespaceBodyIndent; fIndent= fPrefs.prefNamespaceBodyIndent;
} else { } 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 // normal: skip to the statement start before the scope introducer
// opening braces are often on differently ending indents than e.g. a method definition // opening braces are often on differently ending indents than e.g. a method definition
if (!looksLikeArrayInitializerIntro && !fPrefs.prefIndentBracesForBlocks) { if (!looksLikeArrayInitializerIntro) {
fPosition= pos; // restore fPosition= pos; // restore
return skipToStatementStart(true, true); // set to true to match the first if return skipToStatementStart(true, true); // set to true to match the first if
} else { } else {
@ -1639,8 +1662,6 @@ public final class CIndenter {
} }
case Symbols.TokenLBRACKET: case Symbols.TokenLBRACKET:
pos= fPosition; // store
// special: method declaration deep indentation // special: method declaration deep indentation
if (fPrefs.prefArrayDimensionsDeepIndent) { if (fPrefs.prefArrayDimensionsDeepIndent) {
return setFirstElementAlignment(pos, bound); return setFirstElementAlignment(pos, bound);
@ -1710,6 +1731,7 @@ public final class CIndenter {
* @return <code>true</code> if the next elements look like the start of a namespace declaration. * @return <code>true</code> if the next elements look like the start of a namespace declaration.
*/ */
private boolean isNamespace() { private boolean isNamespace() {
int pos = fPosition;
if (fToken == Symbols.TokenNAMESPACE) { if (fToken == Symbols.TokenNAMESPACE) {
return true; // Anonymous namespace return true; // Anonymous namespace
} else if (fToken == Symbols.TokenIDENT) { } else if (fToken == Symbols.TokenIDENT) {
@ -1718,6 +1740,7 @@ public final class CIndenter {
return true; // Named namespace return true; // Named namespace
} }
} }
fPosition = pos;
return false; return false;
} }