QmlJSEditor: Improve indentation of square brackets.

Task-number: QTCREATORBUG-583
Reviewed-by: Roberto Raggi
This commit is contained in:
Christian Kamm
2010-04-14 15:15:56 +02:00
parent 3d8537922c
commit 6a12c99bd1
2 changed files with 37 additions and 9 deletions

View File

@@ -253,12 +253,14 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
switch (last.kind) { switch (last.kind) {
case Token::LeftParenthesis: case Token::LeftParenthesis:
case Token::LeftBrace: case Token::LeftBrace:
case Token::LeftBracket:
case Token::Semicolon: case Token::Semicolon:
case Token::Delimiter: case Token::Delimiter:
break; break;
case Token::RightParenthesis: case Token::RightParenthesis:
case Token::RightBrace: case Token::RightBrace:
case Token::RightBracket:
if (isBinding) if (isBinding)
needSemicolon = true; needSemicolon = true;
break; break;
@@ -267,8 +269,6 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
case Token::Number: case Token::Number:
case Token::Colon: case Token::Colon:
case Token::Comma: case Token::Comma:
case Token::LeftBracket:
case Token::RightBracket:
needSemicolon = true; needSemicolon = true;
break; break;
@@ -314,6 +314,31 @@ QChar QmlJSIndenter::lastParen() const
return QChar(); return QChar();
} }
bool QmlJSIndenter::hasUnclosedParenOrBracket() const
{
int closedParen = 0;
int closedBracket = 0;
for (int index = yyLinizerState.tokens.size() - 1; index != -1; --index) {
const Token &token = yyLinizerState.tokens.at(index);
if (token.is(Token::RightParenthesis)) {
closedParen++;
} else if (token.is(Token::RightBracket)) {
closedBracket++;
} else if (token.is(Token::LeftParenthesis)) {
closedParen--;
if (closedParen < 0)
return true;
} else if (token.is(Token::LeftBracket)) {
closedBracket--;
if (closedBracket < 0)
return true;
}
}
return false;
}
/* /*
Returns true if typedIn the same as okayCh or is null; otherwise Returns true if typedIn the same as okayCh or is null; otherwise
returns false. returns false.
@@ -383,8 +408,8 @@ bool QmlJSIndenter::readLine()
the other way around, as we are parsing backwards. the other way around, as we are parsing backwards.
*/ */
yyLinizerState.braceDepth += yyLinizerState.braceDepth +=
yyLinizerState.line.count('}') - yyLinizerState.line.count('}') + yyLinizerState.line.count(']') -
yyLinizerState.line.count('{'); yyLinizerState.line.count('{') - yyLinizerState.line.count('[');
/* /*
We use a dirty trick for We use a dirty trick for
@@ -620,7 +645,7 @@ bool QmlJSIndenter::isUnfinishedLine()
const 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.
*/ */
@@ -630,15 +655,16 @@ bool QmlJSIndenter::isUnfinishedLine()
unf = false; unf = false;
} else if (lastCh == QLatin1Char(';')) { } else if (lastCh == QLatin1Char(';')) {
if (lastParen() == QLatin1Char('(')) { if (hasUnclosedParenOrBracket()) {
/* /*
Exception: Exception:
for (int i = 1; i < 10; for (int i = 1; i < 10;
*/ */
unf = true; unf = true;
} else if (readLine() && yyLine->endsWith(QLatin1String(";")) &&
lastParen() == QLatin1Char('(')) { // ### This only checks one line back.
} else if (readLine() && yyLine->endsWith(QLatin1String(";")) && hasUnclosedParenOrBracket()) {
/* /*
Exception: Exception:
@@ -1037,7 +1063,8 @@ int QmlJSIndenter::indentForBottomLine(QTextBlock begin, QTextBlock end, QChar t
indent = indentForStandaloneLine(); indent = indentForStandaloneLine();
} }
if (okay(typedIn, QLatin1Char('}')) && firstCh == QLatin1Char('}')) { if ((okay(typedIn, QLatin1Char('}')) && firstCh == QLatin1Char('}'))
|| (okay(typedIn, QLatin1Char(']')) && firstCh == QLatin1Char(']'))) {
/* /*
A closing brace is one level more to the left than the A closing brace is one level more to the left than the
code it follows. code it follows.

View File

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