1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Code formatting.

This commit is contained in:
Sergey Prigogin 2009-05-28 07:34:25 +00:00
parent 5ebe6f4989
commit 62b0e8bd2e

View file

@ -7,7 +7,7 @@
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Sergey Prigogin, Google * Sergey Prigogin (Google)
* Anton Leherbauer (Wind River Systems) * Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.text; package org.eclipse.cdt.internal.ui.text;
@ -23,14 +23,13 @@ import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil; import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil;
/** /**
* Uses the {@link org.eclipse.cdt.internal.ui.text.CHeuristicScanner} to * Uses the {@link org.eclipse.cdt.internal.ui.text.CHeuristicScanner} to
* get the indentation level for a certain position in a document. * get the indentation level for a certain position in a document.
* *
* <p> * <p>
* An instance holds some internal position in the document and is therefore * An instance holds some internal position in the document and is therefore
* not threadsafe. * not thread-safe.
* </p> * </p>
*/ */
public final class CIndenter { public final class CIndenter {
@ -739,11 +738,10 @@ public final class CIndenter {
boolean matchCase= false; boolean matchCase= false;
boolean matchAccessSpecifier= false; boolean matchAccessSpecifier= false;
// account for un-indentation characters already typed in, but after position // Account for un-indentation characters already typed in, but after position.
// if they are on a line by themselves, the indentation gets adjusted // If they are on a line by themselves, the indentation gets adjusted accordingly.
// accordingly
// //
// also account for a dangling else // Also account for a dangling else.
if (offset < fDocument.getLength()) { if (offset < fDocument.getLength()) {
try { try {
IRegion line= fDocument.getLineInformationOfOffset(offset); IRegion line= fDocument.getLineInformationOfOffset(offset);
@ -842,53 +840,55 @@ public final class CIndenter {
fPosition= offset; fPosition= offset;
// forward cases // forward cases
// an unindentation happens sometimes if the next token is special, namely on braces, parens and case labels // An unindentation happens sometimes if the next token is special, namely on braces, parens and case
// align braces, but handle the case where we align with the method declaration start instead of // labels align braces, but handle the case where we align with the method declaration start instead
// the opening brace. // of the opening brace.
if (matchBrace) { if (matchBrace) {
if (skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE)) { if (skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE)) {
try { try {
// align with the opening brace that is on a line by its own // Align with the opening brace that is on a line by its own
int lineOffset= fDocument.getLineOffset(fLine); int lineOffset= fDocument.getLineOffset(fLine);
if (lineOffset <= fPosition && fDocument.get(lineOffset, fPosition - lineOffset).trim().length() == 0) if (lineOffset <= fPosition && fDocument.get(lineOffset, fPosition - lineOffset).trim().length() == 0)
return fPosition; return fPosition;
} catch (BadLocationException e) { } catch (BadLocationException e) {
// concurrent modification - walk default path // Concurrent modification - walk default path
} }
// if the opening brace is not on the start of the line, skip to the start // If the opening brace is not on the start of the line, skip to the start
int pos= skipToStatementStart(true, true); int pos= skipToStatementStart(true, true);
fIndent= 0; // indent is aligned with reference position fIndent= 0; // indent is aligned with reference position
return pos; return pos;
} else { } else {
// if we can't find the matching brace, the heuristic is to unindent // If we can't find the matching brace, the heuristic is to unindent
// by one against the normal position // by one against the normal position
int pos= findReferencePosition(offset, danglingElse, false, matchParen, matchCase, matchAccessSpecifier); int pos= findReferencePosition(offset, danglingElse, false, matchParen, matchCase,
matchAccessSpecifier);
fIndent--; fIndent--;
return pos; return pos;
} }
} }
// align parenthesis' // Align parentheses
if (matchParen) { if (matchParen) {
if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN))
return fPosition; return fPosition;
else { else {
// if we can't find the matching paren, the heuristic is to unindent // if we can't find the matching paren, the heuristic is to unindent
// by one against the normal position // by one against the normal position
int pos= findReferencePosition(offset, danglingElse, matchBrace, false, matchCase, matchAccessSpecifier); int pos= findReferencePosition(offset, danglingElse, matchBrace, false, matchCase,
matchAccessSpecifier);
fIndent--; fIndent--;
return pos; return pos;
} }
} }
// the only reliable way to get case labels aligned (due to many different styles of using braces in a block) // The only reliable way to get case labels aligned (due to many different styles of using braces in
// is to go for another case statement, or the scope opening brace // a block) is to go for another case statement, or the scope opening brace.
if (matchCase) { if (matchCase) {
return matchCaseAlignment(); return matchCaseAlignment();
} }
// the only reliable way to get access specifiers aligned (due to many different styles of using braces in a block) // the only reliable way to get access specifiers aligned (due to many different styles of using
// is to go for another access specifier, or the scope opening brace // braces in a block) is to go for another access specifier, or the scope opening brace.
if (matchAccessSpecifier) { if (matchAccessSpecifier) {
return matchAccessSpecifierAlignment(); return matchAccessSpecifierAlignment();
} }
@ -906,7 +906,8 @@ public final class CIndenter {
case Symbols.TokenSEMICOLON: case Symbols.TokenSEMICOLON:
// this is the 90% case: after a statement block // this is the 90% case: after a statement block
// the end of the previous statement / block previous.end // the end of the previous statement / block previous.end
// search to the end of the statement / block before the previous; the token just after that is previous.start // search to the end of the statement / block before the previous;
// the token just after that is previous.start
return skipToStatementStart(danglingElse, false); return skipToStatementStart(danglingElse, false);
// scope introduction: special treat who special is // scope introduction: special treat who special is
@ -1001,8 +1002,9 @@ public final class CIndenter {
return skipToPreviousListItemOrListStart(); return skipToPreviousListItemOrListStart();
default: default:
// inside whatever we don't know about: similar to the list case: // inside whatever we don't know about: similar to the list case:
// if we are inside a continued expression, then either align with a previous line that has indentation // if we are inside a continued expression, then either align with a previous line that
// or indent from the expression start line (either a scope introducer or the start of the expr). // has indentation or indent from the expression start line (either a scope introducer
// or the start of the expression).
return skipToPreviousListItemOrListStart(); return skipToPreviousListItemOrListStart();
} }
} }
@ -1026,7 +1028,7 @@ public final class CIndenter {
|| fToken == Symbols.TokenPRIVATE) { || fToken == Symbols.TokenPRIVATE) {
continue; continue;
} }
else if (fToken == Symbols.TokenCLASS if (fToken == Symbols.TokenCLASS
|| fToken == Symbols.TokenSTRUCT || fToken == Symbols.TokenSTRUCT
|| fToken == Symbols.TokenUNION || fToken == Symbols.TokenUNION
|| fToken == Symbols.TokenENUM) { || fToken == Symbols.TokenENUM) {
@ -1039,11 +1041,11 @@ public final class CIndenter {
} else { } else {
return CHeuristicScanner.NOT_FOUND; return CHeuristicScanner.NOT_FOUND;
} }
} } else {
else
return CHeuristicScanner.NOT_FOUND; return CHeuristicScanner.NOT_FOUND;
} }
} }
}
/** /**
* Test whether the colon at the current position marks a case statement * Test whether the colon at the current position marks a case statement
@ -1061,15 +1063,13 @@ public final class CIndenter {
while (skipQualifiers()) { while (skipQualifiers()) {
nextToken(); nextToken();
} }
switch (fToken) { if (fToken == Symbols.TokenCASE) {
case Symbols.TokenCASE:
return true; return true;
} }
break; break;
case Symbols.TokenOTHER: case Symbols.TokenOTHER:
nextToken(); nextToken();
switch (fToken) { if (fToken == Symbols.TokenCASE) {
case Symbols.TokenCASE:
return true; return true;
} }
break; break;
@ -1128,7 +1128,8 @@ public final class CIndenter {
* Skips to the start of a statement that ends at the current position. * Skips to the start of a statement that ends at the current position.
* *
* @param danglingElse whether to indent aligned with the last <code>if</code> * @param danglingElse whether to indent aligned with the last <code>if</code>
* @param isInBlock whether the current position is inside a block, which limits the search scope to the next scope introducer * @param isInBlock whether the current position is inside a block, which limits the search scope to
* the next scope introducer
* @return the reference offset of the start of the statement * @return the reference offset of the start of the statement
*/ */
private int skipToStatementStart(boolean danglingElse, boolean isInBlock) { private int skipToStatementStart(boolean danglingElse, boolean isInBlock) {
@ -1274,10 +1275,10 @@ public final class CIndenter {
} }
/** /**
* Returns true if the colon at the current position is part of a conditional * Returns <code>true</code> if the colon at the current position is part of a conditional
* (ternary) expression, false otherwise. * (ternary) expression, <code>false</code> otherwise.
* *
* @return true if the colon at the current position is part of a conditional * @return <code>true</code> if the colon at the current position is part of a conditional
*/ */
private boolean isConditional() { private boolean isConditional() {
while (true) { while (true) {
@ -1420,7 +1421,6 @@ public final class CIndenter {
* The indentation will either match the list scope introducer (e.g. for * The indentation will either match the list scope introducer (e.g. for
* method declarations), so called deep indents, or simply increase the * method declarations), so called deep indents, or simply increase the
* indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int)}. * indentation by a number of standard indents. See also {@link #handleScopeIntroduction(int)}.
*
* @return the reference position for a list item: either a previous list item * @return the reference position for a list item: either a previous list item
* that has its own indentation, or the list introduction start. * that has its own indentation, or the list introduction start.
*/ */
@ -1459,17 +1459,17 @@ public final class CIndenter {
case Symbols.TokenSEMICOLON: case Symbols.TokenSEMICOLON:
return fPosition; return fPosition;
case Symbols.TokenQUESTIONMARK: case Symbols.TokenQUESTIONMARK:
if (fPrefs.prefTernaryDeepAlign) { if (fPrefs.prefTernaryDeepAlign) {
setFirstElementAlignment(fPosition - 1, fPosition + 1); setFirstElementAlignment(fPosition - 1, fPosition + 1);
return fPosition;
} else { } else {
fIndent= fPrefs.prefTernaryIndent; fIndent= fPrefs.prefTernaryIndent;
return fPosition;
} }
return fPosition;
case Symbols.TokenEOF: case Symbols.TokenEOF:
return 0; return 0;
} }
} }
} }
@ -1587,24 +1587,25 @@ public final class CIndenter {
// special: method declaration deep indentation // special: method declaration deep indentation
if (looksLikeMethodDecl()) { if (looksLikeMethodDecl()) {
if (fPrefs.prefMethodDeclDeepIndent) if (fPrefs.prefMethodDeclDeepIndent) {
return setFirstElementAlignment(pos, bound); return setFirstElementAlignment(pos, bound);
else { } else {
fIndent= fPrefs.prefMethodDeclIndent; fIndent= fPrefs.prefMethodDeclIndent;
return pos; return pos;
} }
} else { } else {
fPosition= pos; fPosition= pos;
if (looksLikeMethodCall()) { if (looksLikeMethodCall()) {
if (fPrefs.prefMethodCallDeepIndent) if (fPrefs.prefMethodCallDeepIndent) {
return setFirstElementAlignment(pos, bound); return setFirstElementAlignment(pos, bound);
else { } else {
fIndent= fPrefs.prefMethodCallIndent; fIndent= fPrefs.prefMethodCallIndent;
return pos; return pos;
} }
} else if (fPrefs.prefParenthesisDeepIndent) } else if (fPrefs.prefParenthesisDeepIndent) {
return setFirstElementAlignment(pos, bound); return setFirstElementAlignment(pos, bound);
} }
}
// normal: return the parenthesis as reference // normal: return the parenthesis as reference
fIndent= fPrefs.prefParenthesisIndent; fIndent= fPrefs.prefParenthesisIndent;
@ -1672,7 +1673,6 @@ public final class CIndenter {
return fAlign; return fAlign;
} }
/** /**
* Returns <code>true</code> if the next token received after calling * Returns <code>true</code> if the next token received after calling
* <code>nextToken</code> is either an equal sign, an opening brace, * <code>nextToken</code> is either an equal sign, an opening brace,
@ -1760,7 +1760,6 @@ public final class CIndenter {
} }
} }
/** /**
* while(condition); is ambiguous when parsed backwardly, as it is a valid * while(condition); is ambiguous when parsed backwardly, as it is a valid
* statement by its own, so we have to check whether there is a matching * statement by its own, so we have to check whether there is a matching
@ -1778,6 +1777,7 @@ public final class CIndenter {
skipScope(); // and fall thru skipScope(); // and fall thru
skipToStatementStart(false, false); skipToStatementStart(false, false);
return fToken == Symbols.TokenDO; return fToken == Symbols.TokenDO;
case Symbols.TokenSEMICOLON: case Symbols.TokenSEMICOLON:
skipToStatementStart(false, false); skipToStatementStart(false, false);
return fToken == Symbols.TokenDO; return fToken == Symbols.TokenDO;
@ -1785,7 +1785,6 @@ public final class CIndenter {
return false; return false;
} }
/** /**
* Skips pointer operators if the current token is a pointer operator. * Skips pointer operators if the current token is a pointer operator.
* *
@ -2010,7 +2009,6 @@ public final class CIndenter {
* otherwise * otherwise
*/ */
private boolean skipScope(int openToken, int closeToken) { private boolean skipScope(int openToken, int closeToken) {
int depth= 1; int depth= 1;
while (true) { while (true) {