forked from qt-creator/qt-creator
indenter.cpp: code cosmetics, re-indent code
This commit is contained in:
@@ -134,7 +134,7 @@ void Indenter::setIndentSize(int size)
|
||||
ppContinuationIndentSize = 2 * size;
|
||||
}
|
||||
|
||||
void Indenter::setTabSize(int size )
|
||||
void Indenter::setTabSize(int size)
|
||||
{
|
||||
ppHardwareTabSize = size;
|
||||
}
|
||||
@@ -153,11 +153,11 @@ void Indenter::setDoubleIndentBlocks(bool indent)
|
||||
Returns the first non-space character in the string t, or
|
||||
QChar::null if the string is made only of white space.
|
||||
*/
|
||||
QChar Indenter::firstNonWhiteSpace( const QString& t )
|
||||
QChar Indenter::firstNonWhiteSpace(const QString &t)
|
||||
{
|
||||
if (const int len = t.length())
|
||||
for ( int i = 0; i < len; i++)
|
||||
if ( !t[i].isSpace() )
|
||||
for (int i = 0; i < len; i++)
|
||||
if (!t[i].isSpace())
|
||||
return t[i];
|
||||
return QChar::Null;
|
||||
}
|
||||
@@ -166,9 +166,9 @@ QChar Indenter::firstNonWhiteSpace( const QString& t )
|
||||
Returns true if string t is made only of white space; otherwise
|
||||
returns false.
|
||||
*/
|
||||
bool Indenter::isOnlyWhiteSpace( const QString& t )
|
||||
bool Indenter::isOnlyWhiteSpace(const QString &t)
|
||||
{
|
||||
return t.isEmpty() || firstNonWhiteSpace( t ).isNull();
|
||||
return t.isEmpty() || firstNonWhiteSpace(t).isNull();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -176,17 +176,17 @@ bool Indenter::isOnlyWhiteSpace( const QString& t )
|
||||
index. Column numbers and index are identical for strings that don't
|
||||
contain '\t's.
|
||||
*/
|
||||
int Indenter::columnForIndex( const QString& t, int index ) const
|
||||
int Indenter::columnForIndex(const QString &t, int index) const
|
||||
{
|
||||
int col = 0;
|
||||
if ( index > t.length() )
|
||||
if (index > t.length())
|
||||
index = t.length();
|
||||
|
||||
const QChar tab = QLatin1Char('\t');
|
||||
|
||||
for ( int i = 0; i < index; i++ ) {
|
||||
if ( t[i] == tab ) {
|
||||
col = ( (col / ppHardwareTabSize) + 1 ) * ppHardwareTabSize;
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (t[i] == tab) {
|
||||
col = ((col / ppHardwareTabSize) + 1) * ppHardwareTabSize;
|
||||
} else {
|
||||
col++;
|
||||
}
|
||||
@@ -197,9 +197,9 @@ int Indenter::columnForIndex( const QString& t, int index ) const
|
||||
/*
|
||||
Returns the indentation size of string t.
|
||||
*/
|
||||
int Indenter::indentOfLine( const QString& t ) const
|
||||
int Indenter::indentOfLine(const QString &t) const
|
||||
{
|
||||
return columnForIndex( t, t.indexOf(firstNonWhiteSpace(t)) );
|
||||
return columnForIndex(t, t.indexOf(firstNonWhiteSpace(t)));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -208,9 +208,9 @@ int Indenter::indentOfLine( const QString& t ) const
|
||||
provisions are taken against '\n' or '\r', which shouldn't occur in
|
||||
t anyway.
|
||||
*/
|
||||
static inline void eraseChar( QString& t, int k, QChar ch )
|
||||
static inline void eraseChar(QString &t, int k, QChar ch)
|
||||
{
|
||||
if ( t[k] != QLatin1Char('\t') )
|
||||
if (t[k] != QLatin1Char('\t'))
|
||||
t[k] = ch;
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ static inline void eraseChar( QString& t, int k, QChar ch )
|
||||
Removes some nefast constructs from a code line and returns the
|
||||
resulting line.
|
||||
*/
|
||||
QString Indenter::trimmedCodeLine( const QString& t )
|
||||
QString Indenter::trimmedCodeLine(const QString &t)
|
||||
{
|
||||
QString trimmed = t;
|
||||
int k;
|
||||
@@ -235,10 +235,10 @@ QString Indenter::trimmedCodeLine( const QString& t )
|
||||
continuation lines.
|
||||
*/
|
||||
k = 0;
|
||||
while ( (k = m_constants.m_literal.indexIn(trimmed), k) != -1 ) {
|
||||
while ((k = m_constants.m_literal.indexIn(trimmed), k) != -1) {
|
||||
const int matchedLength = m_constants.m_literal.matchedLength();
|
||||
for ( int i = 0; i < matchedLength ; i++ )
|
||||
eraseChar( trimmed, k + i, capitalX );
|
||||
for (int i = 0; i < matchedLength ; i++)
|
||||
eraseChar(trimmed, k + i, capitalX);
|
||||
k += matchedLength;
|
||||
}
|
||||
|
||||
@@ -247,10 +247,10 @@ QString Indenter::trimmedCodeLine( const QString& t )
|
||||
handled elsewhere.
|
||||
*/
|
||||
k = 0;
|
||||
while ( (k = m_constants.m_inlineCComment.indexIn(trimmed, k)) != -1 ) {
|
||||
while ((k = m_constants.m_inlineCComment.indexIn(trimmed, k)) != -1) {
|
||||
const int matchedLength = m_constants.m_inlineCComment.matchedLength();
|
||||
for ( int i = 0; i < matchedLength; i++ )
|
||||
eraseChar( trimmed, k + i, blank );
|
||||
for (int i = 0; i < matchedLength; i++)
|
||||
eraseChar(trimmed, k + i, blank);
|
||||
k += matchedLength;
|
||||
}
|
||||
|
||||
@@ -261,21 +261,21 @@ QString Indenter::trimmedCodeLine( const QString& t )
|
||||
foo1: bar1;
|
||||
bar2;
|
||||
*/
|
||||
while ( trimmed.lastIndexOf(colon ) != -1 && m_constants.m_label.indexIn(trimmed) != -1 ) {
|
||||
const QString cap1 = m_constants.m_label.cap( 1 );
|
||||
const int pos1 = m_constants.m_label.pos( 1 );
|
||||
while (trimmed.lastIndexOf(colon) != -1 && m_constants.m_label.indexIn(trimmed) != -1) {
|
||||
const QString cap1 = m_constants.m_label.cap(1);
|
||||
const int pos1 = m_constants.m_label.pos(1);
|
||||
int stop = cap1.length();
|
||||
|
||||
if ( pos1 + stop < trimmed.length() && ppIndentSize < stop )
|
||||
if (pos1 + stop < trimmed.length() && ppIndentSize < stop)
|
||||
stop = ppIndentSize;
|
||||
|
||||
int i = 0;
|
||||
while ( i < stop ) {
|
||||
eraseChar( trimmed, pos1 + i, blank );
|
||||
while (i < stop) {
|
||||
eraseChar(trimmed, pos1 + i, blank );
|
||||
i++;
|
||||
}
|
||||
while ( i < cap1.length() ) {
|
||||
eraseChar( trimmed, pos1 + i,semicolon );
|
||||
while (i < cap1.length()) {
|
||||
eraseChar(trimmed, pos1 + i,semicolon);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -283,9 +283,9 @@ QString Indenter::trimmedCodeLine( const QString& t )
|
||||
/*
|
||||
Remove C++-style comments.
|
||||
*/
|
||||
k = trimmed.indexOf(m_constants.m_slashSlash );
|
||||
if ( k != -1 )
|
||||
trimmed.truncate( k );
|
||||
k = trimmed.indexOf(m_constants.m_slashSlash);
|
||||
if (k != -1)
|
||||
trimmed.truncate(k);
|
||||
|
||||
return trimmed;
|
||||
}
|
||||
@@ -294,7 +294,7 @@ QString Indenter::trimmedCodeLine( const QString& t )
|
||||
Returns '(' if the last parenthesis is opening, ')' if it is
|
||||
closing, and QChar::null if there are no parentheses in t.
|
||||
*/
|
||||
static inline QChar lastParen( const QString& t )
|
||||
static inline QChar lastParen(const QString &t)
|
||||
{
|
||||
|
||||
const QChar opening = QLatin1Char('(');
|
||||
@@ -302,10 +302,10 @@ static inline QChar lastParen( const QString& t )
|
||||
|
||||
|
||||
int i = t.length();
|
||||
while ( i > 0 ) {
|
||||
while (i > 0) {
|
||||
i--;
|
||||
const QChar c = t[i];
|
||||
if (c == opening || c == closing )
|
||||
if (c == opening || c == closing)
|
||||
return c;
|
||||
}
|
||||
return QChar::Null;
|
||||
@@ -315,7 +315,7 @@ static inline QChar lastParen( const QString& t )
|
||||
Returns true if typedIn the same as okayCh or is null; otherwise
|
||||
returns false.
|
||||
*/
|
||||
static inline bool okay( QChar typedIn, QChar okayCh )
|
||||
static inline bool okay(QChar typedIn, QChar okayCh)
|
||||
{
|
||||
return typedIn == QChar::Null || typedIn == okayCh;
|
||||
}
|
||||
@@ -326,17 +326,17 @@ static inline bool okay( QChar typedIn, QChar okayCh )
|
||||
backtracking.
|
||||
*/
|
||||
#define YY_SAVE() \
|
||||
LinizerState savedState = *yyLinizerState
|
||||
LinizerState savedState = *yyLinizerState
|
||||
#define YY_RESTORE() \
|
||||
*yyLinizerState = savedState
|
||||
|
||||
/*
|
||||
/*
|
||||
Advances to the previous line in yyProgram and update yyLine
|
||||
accordingly. yyLine is cleaned from comments and other damageable
|
||||
constructs. Empty lines are skipped.
|
||||
*/
|
||||
bool Indenter::readLine()
|
||||
{
|
||||
bool Indenter::readLine()
|
||||
{
|
||||
int k;
|
||||
|
||||
const QChar openingBrace = QLatin1Char('{');
|
||||
@@ -345,10 +345,10 @@ bool Indenter::readLine()
|
||||
const QChar hash = QLatin1Char('#');
|
||||
|
||||
yyLinizerState->leftBraceFollows =
|
||||
( firstNonWhiteSpace(yyLinizerState->line) == openingBrace );
|
||||
(firstNonWhiteSpace(yyLinizerState->line) == openingBrace );
|
||||
|
||||
do {
|
||||
if ( yyLinizerState->iter == yyProgramBegin ) {
|
||||
if (yyLinizerState->iter == yyProgramBegin) {
|
||||
yyLinizerState->line = QString::null;
|
||||
return false;
|
||||
}
|
||||
@@ -356,7 +356,7 @@ bool Indenter::readLine()
|
||||
--yyLinizerState->iter;
|
||||
yyLinizerState->line = *yyLinizerState->iter;
|
||||
|
||||
yyLinizerState->line = trimmedCodeLine( yyLinizerState->line );
|
||||
yyLinizerState->line = trimmedCodeLine(yyLinizerState->line);
|
||||
|
||||
/*
|
||||
Remove C-style comments that span multiple lines. If the
|
||||
@@ -368,22 +368,22 @@ bool Indenter::readLine()
|
||||
the first if. The order of the if's is also important.
|
||||
*/
|
||||
|
||||
if ( yyLinizerState->inCComment ) {
|
||||
if (yyLinizerState->inCComment) {
|
||||
|
||||
k = yyLinizerState->line.indexOf( m_constants.m_slashAster );
|
||||
if ( k == -1 ) {
|
||||
k = yyLinizerState->line.indexOf(m_constants.m_slashAster);
|
||||
if (k == -1) {
|
||||
yyLinizerState->line = QString::null;
|
||||
} else {
|
||||
yyLinizerState->line.truncate( k );
|
||||
yyLinizerState->line.truncate(k);
|
||||
yyLinizerState->inCComment = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !yyLinizerState->inCComment ) {
|
||||
k = yyLinizerState->line.indexOf( m_constants.m_asterSlash );
|
||||
if ( k != -1 ) {
|
||||
for ( int i = 0; i < k + 2; i++ )
|
||||
eraseChar( yyLinizerState->line, i, blank );
|
||||
if (!yyLinizerState->inCComment) {
|
||||
k = yyLinizerState->line.indexOf(m_constants.m_asterSlash);
|
||||
if (k != -1) {
|
||||
for (int i = 0; i < k + 2; i++)
|
||||
eraseChar(yyLinizerState->line, i, blank);
|
||||
yyLinizerState->inCComment = true;
|
||||
}
|
||||
}
|
||||
@@ -392,11 +392,11 @@ bool Indenter::readLine()
|
||||
Remove preprocessor directives.
|
||||
*/
|
||||
k = 0;
|
||||
while ( k < yyLinizerState->line.length() ) {
|
||||
while (k < yyLinizerState->line.length()) {
|
||||
QChar ch = yyLinizerState->line[k];
|
||||
if ( ch == hash ) {
|
||||
if (ch == hash) {
|
||||
yyLinizerState->line = QString::null;
|
||||
} else if ( !ch.isSpace() ) {
|
||||
} else if (!ch.isSpace()) {
|
||||
break;
|
||||
}
|
||||
k++;
|
||||
@@ -406,16 +406,16 @@ bool Indenter::readLine()
|
||||
Remove trailing spaces.
|
||||
*/
|
||||
k = yyLinizerState->line.length();
|
||||
while ( k > 0 && yyLinizerState->line[k - 1].isSpace() )
|
||||
while (k > 0 && yyLinizerState->line[k - 1].isSpace())
|
||||
k--;
|
||||
yyLinizerState->line.truncate( k );
|
||||
yyLinizerState->line.truncate(k);
|
||||
|
||||
/*
|
||||
'}' increment the brace depth and '{' decrements it and not
|
||||
the other way around, as we are parsing backwards.
|
||||
*/
|
||||
yyLinizerState->braceDepth +=
|
||||
yyLinizerState->line.count( closingBrace ) - yyLinizerState->line.count( openingBrace );
|
||||
yyLinizerState->line.count(closingBrace ) - yyLinizerState->line.count(openingBrace );
|
||||
|
||||
/*
|
||||
We use a dirty trick for
|
||||
@@ -428,13 +428,13 @@ bool Indenter::readLine()
|
||||
}
|
||||
else ...
|
||||
*/
|
||||
if ( yyLinizerState->pendingRightBrace )
|
||||
if (yyLinizerState->pendingRightBrace)
|
||||
yyLinizerState->braceDepth++;
|
||||
yyLinizerState->pendingRightBrace =
|
||||
( m_constants.m_braceX.indexIn(yyLinizerState->line) == 0 );
|
||||
if ( yyLinizerState->pendingRightBrace )
|
||||
(m_constants.m_braceX.indexIn(yyLinizerState->line) == 0);
|
||||
if (yyLinizerState->pendingRightBrace)
|
||||
yyLinizerState->braceDepth--;
|
||||
} while ( yyLinizerState->line.isEmpty() );
|
||||
} while (yyLinizerState->line.isEmpty());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -473,17 +473,17 @@ bool Indenter::bottomLineStartsInCComment()
|
||||
Iterator p = yyProgramEnd;
|
||||
--p; // skip bottom line
|
||||
|
||||
for ( int i = 0; i < BigRoof; i++ ) {
|
||||
if ( p == yyProgramBegin )
|
||||
for (int i = 0; i < BigRoof; i++) {
|
||||
if (p == yyProgramBegin)
|
||||
return false;
|
||||
--p;
|
||||
|
||||
if ( (*p).contains(m_constants.m_slashAster) || (*p).contains(m_constants.m_asterSlash) ) {
|
||||
QString trimmed = trimmedCodeLine( *p );
|
||||
if ((*p).contains(m_constants.m_slashAster) || (*p).contains(m_constants.m_asterSlash)) {
|
||||
QString trimmed = trimmedCodeLine(*p);
|
||||
|
||||
if ( trimmed.contains(m_constants.m_slashAster) ) {
|
||||
if (trimmed.contains(m_constants.m_slashAster)) {
|
||||
return true;
|
||||
} else if ( trimmed.contains(m_constants.m_asterSlash) ) {
|
||||
} else if (trimmed.contains(m_constants.m_asterSlash)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -501,24 +501,24 @@ bool Indenter::bottomLineStartsInCComment()
|
||||
*/
|
||||
int Indenter::indentWhenBottomLineStartsInCComment() const
|
||||
{
|
||||
int k = yyLine->lastIndexOf(m_constants.m_slashAster );
|
||||
if ( k == -1 ) {
|
||||
int k = yyLine->lastIndexOf(m_constants.m_slashAster);
|
||||
if (k == -1) {
|
||||
/*
|
||||
We found a normal text line in a comment. Align the
|
||||
bottom line with the text on this line.
|
||||
*/
|
||||
return indentOfLine( *yyLine );
|
||||
return indentOfLine(*yyLine);
|
||||
} else {
|
||||
/*
|
||||
The C-style comment starts on this line. If there is
|
||||
text on the same line, align with it. Otherwise, align
|
||||
with the slash-aster plus a given offset.
|
||||
*/
|
||||
const int indent = columnForIndex( *yyLine, k );
|
||||
const int indent = columnForIndex(*yyLine, k);
|
||||
k += 2;
|
||||
while ( k < yyLine->length() ) {
|
||||
if ( !(*yyLine)[k].isSpace() )
|
||||
return columnForIndex( *yyLine, k );
|
||||
while (k < yyLine->length()) {
|
||||
if (!(*yyLine)[k].isSpace())
|
||||
return columnForIndex(*yyLine, k);
|
||||
k++;
|
||||
}
|
||||
return indent + ppCommentOffset;
|
||||
@@ -540,7 +540,7 @@ int Indenter::indentWhenBottomLineStartsInCComment() const
|
||||
The first line of the following example is a "braceless control
|
||||
statement":
|
||||
|
||||
if ( x )
|
||||
if (x)
|
||||
y;
|
||||
*/
|
||||
bool Indenter::matchBracelessControlStatement()
|
||||
@@ -550,49 +550,49 @@ bool Indenter::matchBracelessControlStatement()
|
||||
const QChar semicolon = QLatin1Char(';');
|
||||
|
||||
|
||||
if ( yyLine->endsWith(m_constants.m_else))
|
||||
if (yyLine->endsWith(m_constants.m_else))
|
||||
return true;
|
||||
|
||||
if ( !yyLine->endsWith(QLatin1Char(')')))
|
||||
if (!yyLine->endsWith(QLatin1Char(')')))
|
||||
return false;
|
||||
|
||||
for ( int i = 0; i < SmallRoof; i++ ) {
|
||||
for (int i = 0; i < SmallRoof; i++) {
|
||||
int j = yyLine->length();
|
||||
while ( j > 0 ) {
|
||||
while (j > 0) {
|
||||
j--;
|
||||
QChar ch = (*yyLine)[j];
|
||||
|
||||
switch ( ch.unicode() ) {
|
||||
switch (ch.unicode()) {
|
||||
case ')':
|
||||
delimDepth++;
|
||||
break;
|
||||
case '(':
|
||||
delimDepth--;
|
||||
if ( delimDepth == 0 ) {
|
||||
if ( yyLine->contains(m_constants.m_iflikeKeyword) ) {
|
||||
if (delimDepth == 0) {
|
||||
if (yyLine->contains(m_constants.m_iflikeKeyword)) {
|
||||
/*
|
||||
We have
|
||||
|
||||
if ( x )
|
||||
if (x)
|
||||
y
|
||||
|
||||
"if ( x )" is not part of the statement
|
||||
"if (x)" is not part of the statement
|
||||
"y".
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ( delimDepth == -1 ) {
|
||||
if (delimDepth == -1) {
|
||||
/*
|
||||
We have
|
||||
|
||||
if ( (1 +
|
||||
if ((1 +
|
||||
2)
|
||||
|
||||
and not
|
||||
|
||||
if ( 1 +
|
||||
2 )
|
||||
if (1 +
|
||||
2)
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
@@ -606,12 +606,12 @@ bool Indenter::matchBracelessControlStatement()
|
||||
continuation line. Be careful with ';' in for,
|
||||
though.
|
||||
*/
|
||||
if ( ch != semicolon || delimDepth == 0 )
|
||||
if (ch != semicolon || delimDepth == 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !readLine() )
|
||||
if (!readLine())
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
@@ -640,32 +640,32 @@ bool Indenter::isUnfinishedLine()
|
||||
const QChar openingParenthesis = QLatin1Char('(');
|
||||
const QChar semicolon = QLatin1Char(';');
|
||||
|
||||
if ( yyLine->isEmpty() )
|
||||
if (yyLine->isEmpty())
|
||||
return false;
|
||||
|
||||
const QChar lastCh = (*yyLine)[ yyLine->length() - 1];
|
||||
if ( ! m_constants.m_bracesSemicolon.contains(lastCh) && !yyLine->endsWith(m_constants.m_3dots) ) {
|
||||
if (! m_constants.m_bracesSemicolon.contains(lastCh) && !yyLine->endsWith(m_constants.m_3dots)) {
|
||||
/*
|
||||
It doesn't end with ';' or similar. If it's neither
|
||||
"Q_OBJECT" nor "if ( x )" nor is a template function, it must be an unfinished line.
|
||||
"Q_OBJECT" nor "if (x)" nor is a template function, it must be an unfinished line.
|
||||
*/
|
||||
unf = ( !yyLine->contains(m_constants.m_qobject) &&
|
||||
unf = (!yyLine->contains(m_constants.m_qobject) &&
|
||||
!matchBracelessControlStatement() &&
|
||||
!yyLine->contains(m_constants.m_templateFunc) );
|
||||
} else if ( lastCh == semicolon ) {
|
||||
if ( lastParen(*yyLine) == openingParenthesis ) {
|
||||
!yyLine->contains(m_constants.m_templateFunc));
|
||||
} else if (lastCh == semicolon) {
|
||||
if (lastParen(*yyLine) == openingParenthesis) {
|
||||
/*
|
||||
Exception:
|
||||
|
||||
for ( int i = 1; i < 10;
|
||||
for (int i = 1; i < 10;
|
||||
*/
|
||||
unf = true;
|
||||
} else if ( readLine() && yyLine->endsWith(semicolon) &&
|
||||
lastParen(*yyLine) == openingParenthesis ) {
|
||||
} else if (readLine() && yyLine->endsWith(semicolon) &&
|
||||
lastParen(*yyLine) == openingParenthesis) {
|
||||
/*
|
||||
Exception:
|
||||
|
||||
for ( int i = 1;
|
||||
for (int i = 1;
|
||||
i < 10;
|
||||
*/
|
||||
unf = true;
|
||||
@@ -708,15 +708,15 @@ int Indenter::indentForContinuationLine()
|
||||
const QChar openingParenthesis = QLatin1Char('(');
|
||||
const QChar closingParenthesis = QLatin1Char(')');
|
||||
|
||||
for ( int i = 0; i < SmallRoof; i++ ) {
|
||||
for (int i = 0; i < SmallRoof; i++) {
|
||||
int hook = -1;
|
||||
|
||||
int j = yyLine->length();
|
||||
while ( j > 0 && hook < 0 ) {
|
||||
while (j > 0 && hook < 0) {
|
||||
j--;
|
||||
QChar ch = (*yyLine)[j];
|
||||
|
||||
switch ( ch.unicode() ) {
|
||||
switch (ch.unicode()) {
|
||||
case ')':
|
||||
case ']':
|
||||
delimDepth++;
|
||||
@@ -731,7 +731,7 @@ int Indenter::indentForContinuationLine()
|
||||
An unclosed delimiter is a good place to align at,
|
||||
at least for some styles (including Trolltech's).
|
||||
*/
|
||||
if ( delimDepth == -1 )
|
||||
if (delimDepth == -1)
|
||||
hook = j;
|
||||
break;
|
||||
case '{':
|
||||
@@ -742,8 +742,8 @@ int Indenter::indentForContinuationLine()
|
||||
Such a brace must be treated just like the other
|
||||
delimiters.
|
||||
*/
|
||||
if ( braceDepth == -1 ) {
|
||||
if ( j < yyLine->length() - 1 ) {
|
||||
if (braceDepth == -1) {
|
||||
if (j < yyLine->length() - 1) {
|
||||
hook = j;
|
||||
} else {
|
||||
return 0; // shouldn't happen
|
||||
@@ -764,62 +764,62 @@ int Indenter::indentForContinuationLine()
|
||||
default arguments and explicit enum constant
|
||||
values:
|
||||
|
||||
void foo( int x = 0,
|
||||
int y = 0 );
|
||||
void foo(int x = 0,
|
||||
int y = 0);
|
||||
|
||||
And not
|
||||
|
||||
void foo( int x = 0,
|
||||
int y = 0 );
|
||||
void foo(int x = 0,
|
||||
int y = 0);
|
||||
|
||||
These constructs are caracterized by a ',' at the
|
||||
end of the unfinished lines or by unbalanced
|
||||
parentheses.
|
||||
*/
|
||||
if ( j > 0 && j < yyLine->length() - 1
|
||||
if (j > 0 && j < yyLine->length() - 1
|
||||
&& !m_constants.m_operators.contains((*yyLine)[j - 1])
|
||||
&& (*yyLine)[j + 1] != equals ) {
|
||||
if ( braceDepth == 0 && delimDepth == 0 &&
|
||||
&& (*yyLine)[j + 1] != equals) {
|
||||
if (braceDepth == 0 && delimDepth == 0 &&
|
||||
!yyLine->endsWith(comma) &&
|
||||
(yyLine->contains(openingParenthesis) == yyLine->contains(closingParenthesis)) )
|
||||
(yyLine->contains(openingParenthesis) == yyLine->contains(closingParenthesis)))
|
||||
hook = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( hook >= 0 ) {
|
||||
if (hook >= 0) {
|
||||
/*
|
||||
Yes, we have a delimiter or an operator to align
|
||||
against! We don't really align against it, but rather
|
||||
against the following token, if any. In this example,
|
||||
the following token is "11":
|
||||
|
||||
int x = ( 11 +
|
||||
2 );
|
||||
int x = (11 +
|
||||
2);
|
||||
|
||||
If there is no such token, we use a continuation indent:
|
||||
|
||||
static QRegExp foo( QString(
|
||||
"foo foo foo foo foo foo foo foo foo") );
|
||||
static QRegExp foo(QString(
|
||||
"foo foo foo foo foo foo foo foo foo"));
|
||||
*/
|
||||
hook++;
|
||||
while ( hook < yyLine->length() ) {
|
||||
if ( !(*yyLine)[hook].isSpace() )
|
||||
return columnForIndex( *yyLine, hook );
|
||||
while (hook < yyLine->length()) {
|
||||
if (!(*yyLine)[hook].isSpace())
|
||||
return columnForIndex(*yyLine, hook);
|
||||
hook++;
|
||||
}
|
||||
return indentOfLine( *yyLine ) + ppContinuationIndentSize;
|
||||
return indentOfLine(*yyLine) + ppContinuationIndentSize;
|
||||
}
|
||||
|
||||
if ( braceDepth != 0 )
|
||||
if (braceDepth != 0)
|
||||
break;
|
||||
|
||||
/*
|
||||
The line's delimiters are balanced. It looks like a
|
||||
continuation line or something.
|
||||
*/
|
||||
if ( delimDepth == 0 ) {
|
||||
if ( leftBraceFollowed ) {
|
||||
if (delimDepth == 0) {
|
||||
if (leftBraceFollowed) {
|
||||
/*
|
||||
We have
|
||||
|
||||
@@ -829,14 +829,14 @@ int Indenter::indentForContinuationLine()
|
||||
or
|
||||
|
||||
Bar::Bar()
|
||||
: Foo( x )
|
||||
: Foo(x)
|
||||
{
|
||||
|
||||
The "{" should be flush left.
|
||||
*/
|
||||
if ( !isContinuationLine() )
|
||||
return indentOfLine( *yyLine );
|
||||
} else if ( isContinuationLine() || yyLine->endsWith(comma)) {
|
||||
if (!isContinuationLine())
|
||||
return indentOfLine(*yyLine);
|
||||
} else if (isContinuationLine() || yyLine->endsWith(comma)) {
|
||||
/*
|
||||
We have
|
||||
|
||||
@@ -853,7 +853,7 @@ int Indenter::indentForContinuationLine()
|
||||
The "c;" should fall right under the "b +", and the
|
||||
"4, 5, 6" right under the "1, 2, 3,".
|
||||
*/
|
||||
return indentOfLine( *yyLine );
|
||||
return indentOfLine(*yyLine);
|
||||
} else {
|
||||
/*
|
||||
We have
|
||||
@@ -871,11 +871,11 @@ int Indenter::indentForContinuationLine()
|
||||
We do have a special trick above for the assignment
|
||||
operator above, though.
|
||||
*/
|
||||
return indentOfLine( *yyLine ) + ppContinuationIndentSize;
|
||||
return indentOfLine(*yyLine) + ppContinuationIndentSize;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !readLine() )
|
||||
if (!readLine())
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
@@ -904,26 +904,26 @@ int Indenter::indentForContinuationLine()
|
||||
|
||||
Example 2:
|
||||
|
||||
if ( x ) {
|
||||
if (x) {
|
||||
y;
|
||||
|
||||
The hook line is "if ( x ) {". No matter what precedes it, "y;" has
|
||||
The hook line is "if (x) {". No matter what precedes it, "y;" has
|
||||
to be indented one level deeper than the hook line, since we met one
|
||||
opening brace along the way.
|
||||
|
||||
Example 3:
|
||||
|
||||
if ( a )
|
||||
while ( b ) {
|
||||
if (a)
|
||||
while (b) {
|
||||
c;
|
||||
}
|
||||
d;
|
||||
|
||||
To indent "d;" correctly, we have to go as far as the "if ( a )".
|
||||
To indent "d;" correctly, we have to go as far as the "if (a)".
|
||||
Compare with
|
||||
|
||||
if ( a ) {
|
||||
while ( b ) {
|
||||
if (a) {
|
||||
while (b) {
|
||||
c;
|
||||
}
|
||||
d;
|
||||
@@ -937,81 +937,81 @@ int Indenter::indentForStandaloneLine()
|
||||
const QChar semicolon = QLatin1Char(';');
|
||||
const QChar openingBrace = QLatin1Char('{');
|
||||
|
||||
for ( int i = 0; i < SmallRoof; i++ ) {
|
||||
if ( !*yyLeftBraceFollows ) {
|
||||
for (int i = 0; i < SmallRoof; i++) {
|
||||
if (!*yyLeftBraceFollows) {
|
||||
YY_SAVE();
|
||||
|
||||
if ( matchBracelessControlStatement() ) {
|
||||
if (matchBracelessControlStatement()) {
|
||||
/*
|
||||
The situation is this, and we want to indent "z;":
|
||||
|
||||
if ( x &&
|
||||
y )
|
||||
if (x &&
|
||||
y)
|
||||
z;
|
||||
|
||||
yyLine is "if ( x &&".
|
||||
yyLine is "if (x &&".
|
||||
*/
|
||||
return indentOfLine( *yyLine ) + ppIndentSize;
|
||||
return indentOfLine(*yyLine) + ppIndentSize;
|
||||
}
|
||||
YY_RESTORE();
|
||||
}
|
||||
|
||||
if ( yyLine->endsWith(semicolon) || yyLine->count(openingBrace) > 0 ) {
|
||||
if (yyLine->endsWith(semicolon) || yyLine->count(openingBrace) > 0) {
|
||||
/*
|
||||
The situation is possibly this, and we want to indent
|
||||
"z;":
|
||||
|
||||
while ( x )
|
||||
while (x)
|
||||
y;
|
||||
z;
|
||||
|
||||
We return the indent of "while ( x )". In place of "y;",
|
||||
We return the indent of "while (x)". In place of "y;",
|
||||
any arbitrarily complex compound statement can appear.
|
||||
*/
|
||||
|
||||
if ( *yyBraceDepth > 0 ) {
|
||||
if (*yyBraceDepth > 0) {
|
||||
do {
|
||||
if ( !readLine() )
|
||||
if (!readLine())
|
||||
break;
|
||||
} while ( *yyBraceDepth > 0 );
|
||||
} while (*yyBraceDepth > 0);
|
||||
}
|
||||
|
||||
LinizerState hookState;
|
||||
|
||||
while ( isContinuationLine() )
|
||||
while (isContinuationLine())
|
||||
readLine();
|
||||
hookState = *yyLinizerState;
|
||||
|
||||
readLine();
|
||||
if ( *yyBraceDepth <= 0 ) {
|
||||
if (*yyBraceDepth <= 0) {
|
||||
do {
|
||||
if ( !matchBracelessControlStatement() )
|
||||
if (!matchBracelessControlStatement())
|
||||
break;
|
||||
hookState = *yyLinizerState;
|
||||
} while ( readLine() );
|
||||
} while (readLine());
|
||||
}
|
||||
|
||||
*yyLinizerState = hookState;
|
||||
|
||||
while ( isContinuationLine() )
|
||||
while (isContinuationLine())
|
||||
readLine();
|
||||
|
||||
/*
|
||||
Never trust lines containing only '{' or '}', as some
|
||||
people (Richard M. Stallman) format them weirdly.
|
||||
*/
|
||||
if ( yyLine->trimmed().length() > 1 ) {
|
||||
if (yyLine->trimmed().length() > 1) {
|
||||
if (!ppDoubleIndentBlocks)
|
||||
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize;
|
||||
return indentOfLine(*yyLine) - *yyBraceDepth * ppIndentSize;
|
||||
else {
|
||||
if (*yyBraceDepth == -1 && indentOfLine( *yyLine ) == 0)
|
||||
if (*yyBraceDepth == -1 && indentOfLine(*yyLine) == 0)
|
||||
return ppIndentSize; // don't do double indent for upper level blocks
|
||||
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize * 2;
|
||||
return indentOfLine(*yyLine) - *yyBraceDepth * ppIndentSize * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !readLine() )
|
||||
if (!readLine())
|
||||
return -*yyBraceDepth * ppIndentSize;
|
||||
}
|
||||
return 0;
|
||||
@@ -1030,9 +1030,9 @@ int Indenter::indentForStandaloneLine()
|
||||
int Indenter::indentForBottomLine(const Iterator ¤t,
|
||||
const Iterator &programBegin,
|
||||
const Iterator &programEnd,
|
||||
QChar typedIn )
|
||||
QChar typedIn)
|
||||
{
|
||||
if ( programBegin == programEnd )
|
||||
if (programBegin == programEnd)
|
||||
return 0;
|
||||
|
||||
yyProgramBegin = programBegin;
|
||||
@@ -1043,7 +1043,7 @@ int Indenter::indentForBottomLine(const Iterator ¤t,
|
||||
Iterator lastIt = current;
|
||||
|
||||
QString bottomLine = *lastIt;
|
||||
QChar firstCh = firstNonWhiteSpace( bottomLine );
|
||||
QChar firstCh = firstNonWhiteSpace(bottomLine);
|
||||
int indent;
|
||||
|
||||
const QChar hash = QLatin1Char('#');
|
||||
@@ -1051,32 +1051,32 @@ int Indenter::indentForBottomLine(const Iterator ¤t,
|
||||
const QChar closingBrace = QLatin1Char('}');
|
||||
const QChar colon = QLatin1Char(':');
|
||||
|
||||
if ( bottomLineStartsInCComment() ) {
|
||||
if (bottomLineStartsInCComment()) {
|
||||
/*
|
||||
The bottom line starts in a C-style comment. Indent it
|
||||
smartly, unless the user has already played around with it,
|
||||
in which case it's better to leave her stuff alone.
|
||||
*/
|
||||
if ( isOnlyWhiteSpace(bottomLine) ) {
|
||||
if (isOnlyWhiteSpace(bottomLine)) {
|
||||
indent = indentWhenBottomLineStartsInCComment();
|
||||
} else {
|
||||
indent = indentOfLine( bottomLine );
|
||||
indent = indentOfLine(bottomLine);
|
||||
}
|
||||
} else if ( okay(typedIn, hash) && firstCh == hash ) {
|
||||
} else if (okay(typedIn, hash) && firstCh == hash) {
|
||||
/*
|
||||
Preprocessor directives go flush left.
|
||||
*/
|
||||
indent = 0;
|
||||
} else {
|
||||
if ( isUnfinishedLine() ) {
|
||||
if (isUnfinishedLine()) {
|
||||
indent = indentForContinuationLine();
|
||||
} else {
|
||||
indent = indentForStandaloneLine();
|
||||
}
|
||||
|
||||
if ( ppIndentBraces && firstCh == openingBrace ) {
|
||||
if (ppIndentBraces && firstCh == openingBrace) {
|
||||
indent += ppIndentSize;
|
||||
} else if ( firstCh == closingBrace ) {
|
||||
} else if (firstCh == closingBrace) {
|
||||
/*
|
||||
A closing brace is one level more to the left than the
|
||||
code it follows.
|
||||
@@ -1093,8 +1093,8 @@ int Indenter::indentForBottomLine(const Iterator ¤t,
|
||||
if (ppDoubleIndentBlocks)
|
||||
indent -= ppIndentSize;
|
||||
|
||||
} else if ( okay(typedIn, colon) ) {
|
||||
if ( m_constants.m_caseLabel.indexIn(bottomLine) != -1 ) {
|
||||
} else if (okay(typedIn, colon)) {
|
||||
if (m_constants.m_caseLabel.indexIn(bottomLine) != -1) {
|
||||
/*
|
||||
Move a case label (or the ':' in front of a
|
||||
constructor initialization list) one level to the
|
||||
@@ -1108,18 +1108,18 @@ int Indenter::indentForBottomLine(const Iterator ¤t,
|
||||
user is probably the middle of "foo::bar". (Who
|
||||
uses goto, anyway?)
|
||||
*/
|
||||
if ( indentOfLine(bottomLine) <= indent )
|
||||
if (indentOfLine(bottomLine) <= indent)
|
||||
indent -= ppIndentSize;
|
||||
else
|
||||
indent = indentOfLine( bottomLine );
|
||||
indent = indentOfLine(bottomLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( ppIndentBraces && indent == ppIndentSize &&
|
||||
(firstCh == openingBrace || firstCh == closingBrace ) ) {
|
||||
if (ppIndentBraces && indent == ppIndentSize &&
|
||||
(firstCh == openingBrace || firstCh == closingBrace) ) {
|
||||
indent = 0;
|
||||
}
|
||||
return qMax( 0, indent );
|
||||
return qMax(0, indent);
|
||||
}
|
||||
|
||||
} // namespace SharedTools
|
||||
|
||||
Reference in New Issue
Block a user