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