Fixed indentation of QML/JS for-statements.

This commit is contained in:
Roberto Raggi
2010-01-19 13:57:59 +01:00
parent 6b6b827d9b
commit e87cd6d1a8
2 changed files with 26 additions and 21 deletions

View File

@@ -227,7 +227,7 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
trimmed.append(QLatin1Char(' ')); trimmed.append(QLatin1Char(' '));
} else { } else {
trimmed.append(t.midRef(token.offset, token.length)); trimmed.append(tokenText(token));
} }
previousTokenEnd = token.end(); previousTokenEnd = token.end();
@@ -272,7 +272,7 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
case Token::Identifier: case Token::Identifier:
case Token::Keyword: case Token::Keyword:
if (t.midRef(last.offset, last.length) != QLatin1String("else")) if (tokenText(last) != QLatin1String("else"))
trimmed.append(QLatin1Char(';')); trimmed.append(QLatin1Char(';'));
break; break;
@@ -289,14 +289,18 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
Returns '(' if the last parenthesis is opening, ')' if it is Returns '(' if the last parenthesis is opening, ')' if it is
closing, and QChar() if there are no parentheses in t. closing, and QChar() if there are no parentheses in t.
*/ */
QChar QmlJSIndenter::lastParen(const QString &t) const QChar QmlJSIndenter::lastParen() const
{ {
int i = t.length(); for (int index = yyLinizerState.tokens.size() - 1; index != -1; --index) {
while (i > 0) { const Token &token = yyLinizerState.tokens.at(index);
i--;
if (t.at(i) == QLatin1Char('(') || t.at(i) == QLatin1Char(')')) if (token.is(Token::LeftParenthesis))
return t.at(i); return QChar('(');
else if (token.is(Token::RightParenthesis))
return QChar(')');
} }
return QChar(); return QChar();
} }
@@ -492,8 +496,7 @@ bool QmlJSIndenter::matchBracelessControlStatement()
if (! yyLinizerState.tokens.isEmpty()) { if (! yyLinizerState.tokens.isEmpty()) {
Token tk = lastToken(); Token tk = lastToken();
if (tk.is(Token::Identifier) && if (tk.is(Token::Identifier) && tokenText(tk) == QLatin1String("else"))
yyLinizerState.line.midRef(tk.offset, tk.length) == QLatin1String("else"))
return true; return true;
else if (tk.isNot(Token::RightParenthesis)) else if (tk.isNot(Token::RightParenthesis))
@@ -527,6 +530,7 @@ bool QmlJSIndenter::matchBracelessControlStatement()
*/ */
if (token.kind != Token::Semicolon || delimDepth == 0) if (token.kind != Token::Semicolon || delimDepth == 0)
return false; return false;
break;
case Token::LeftParenthesis: case Token::LeftParenthesis:
@@ -536,7 +540,7 @@ bool QmlJSIndenter::matchBracelessControlStatement()
const Token &tk = yyLinizerState.tokens.at(tokenIndex - 1); const Token &tk = yyLinizerState.tokens.at(tokenIndex - 1);
if (tk.is(Token::Identifier)) { if (tk.is(Token::Identifier)) {
const QStringRef tokenText = yyLinizerState.line.midRef(tk.offset, tk.length); const QStringRef text = tokenText(tk);
/* /*
We have We have
@@ -549,22 +553,22 @@ bool QmlJSIndenter::matchBracelessControlStatement()
*/ */
if (tk.length == 5 && tokenText == QLatin1String("catch")) if (tk.length == 5 && text == QLatin1String("catch"))
return true; return true;
else if (tk.length == 2 && tokenText == QLatin1String("do")) else if (tk.length == 2 && text == QLatin1String("do"))
return true; return true;
else if (tk.length == 3 && tokenText == QLatin1String("for")) else if (tk.length == 3 && text == QLatin1String("for"))
return true; return true;
else if (tk.length == 2 && tokenText == QLatin1String("if")) else if (tk.length == 2 && text == QLatin1String("if"))
return true; return true;
else if (tk.length == 5 && tokenText == QLatin1String("while")) else if (tk.length == 5 && text == QLatin1String("while"))
return true; return true;
else if (tk.length == 4 && tokenText == QLatin1String("with")) else if (tk.length == 4 && text == QLatin1String("with"))
return true; return true;
} }
} }
@@ -618,7 +622,8 @@ bool QmlJSIndenter::isUnfinishedLine()
if (yyLine->isEmpty()) if (yyLine->isEmpty())
return false; return false;
QChar lastCh = yyLine->at(yyLine->length() - 1); const QChar lastCh = yyLine->at(yyLine->length() - 1);
if (QString::fromLatin1("{};").indexOf(lastCh) == -1) { if (QString::fromLatin1("{};").indexOf(lastCh) == -1) {
/* /*
It doesn't end with ';' or similar. If it's not an "if (x)", it must be an unfinished line. It doesn't end with ';' or similar. If it's not an "if (x)", it must be an unfinished line.
@@ -629,7 +634,7 @@ bool QmlJSIndenter::isUnfinishedLine()
unf = false; unf = false;
} else if (lastCh == QLatin1Char(';')) { } else if (lastCh == QLatin1Char(';')) {
if (lastParen(*yyLine) == QLatin1Char('(')) { if (lastParen() == QLatin1Char('(')) {
/* /*
Exception: Exception:
@@ -637,7 +642,7 @@ bool QmlJSIndenter::isUnfinishedLine()
*/ */
unf = true; unf = true;
} else if (readLine() && yyLine->endsWith(QLatin1String(";")) && } else if (readLine() && yyLine->endsWith(QLatin1String(";")) &&
lastParen(*yyLine) == QLatin1Char('(')) { lastParen() == QLatin1Char('(')) {
/* /*
Exception: Exception:

View File

@@ -63,7 +63,7 @@ private:
QString trimmedCodeLine(const QString &t); QString trimmedCodeLine(const QString &t);
void eraseChar(QString &t, int k, QChar ch) const; void eraseChar(QString &t, int k, QChar ch) const;
QChar lastParen(const QString &t) const; QChar lastParen() const;
bool okay(QChar typedIn, QChar okayCh) const; bool okay(QChar typedIn, QChar okayCh) const;
/* /*