Commit 6a12c99b authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJSEditor: Improve indentation of square brackets.

Task-number: QTCREATORBUG-583
Reviewed-by: Roberto Raggi
parent 3d853792
......@@ -253,12 +253,14 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
switch (last.kind) {
case Token::LeftParenthesis:
case Token::LeftBrace:
case Token::LeftBracket:
case Token::Semicolon:
case Token::Delimiter:
break;
case Token::RightParenthesis:
case Token::RightBrace:
case Token::RightBracket:
if (isBinding)
needSemicolon = true;
break;
......@@ -267,8 +269,6 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
case Token::Number:
case Token::Colon:
case Token::Comma:
case Token::LeftBracket:
case Token::RightBracket:
needSemicolon = true;
break;
......@@ -314,6 +314,31 @@ QChar QmlJSIndenter::lastParen() const
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 false.
......@@ -383,8 +408,8 @@ bool QmlJSIndenter::readLine()
the other way around, as we are parsing backwards.
*/
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
......@@ -620,7 +645,7 @@ bool QmlJSIndenter::isUnfinishedLine()
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.
*/
......@@ -630,15 +655,16 @@ bool QmlJSIndenter::isUnfinishedLine()
unf = false;
} else if (lastCh == QLatin1Char(';')) {
if (lastParen() == QLatin1Char('(')) {
if (hasUnclosedParenOrBracket()) {
/*
Exception:
for (int i = 1; i < 10;
*/
unf = true;
} else if (readLine() && yyLine->endsWith(QLatin1String(";")) &&
lastParen() == QLatin1Char('(')) {
// ### This only checks one line back.
} else if (readLine() && yyLine->endsWith(QLatin1String(";")) && hasUnclosedParenOrBracket()) {
/*
Exception:
......@@ -1037,7 +1063,8 @@ int QmlJSIndenter::indentForBottomLine(QTextBlock begin, QTextBlock end, QChar t
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
code it follows.
......
......@@ -64,6 +64,7 @@ private:
void eraseChar(QString &t, int k, QChar ch) const;
QChar lastParen() const;
bool hasUnclosedParenOrBracket() const;
bool okay(QChar typedIn, QChar okayCh) const;
/*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment