Implemented token-based version of QmlJSIndenter::matchBracelessControlStatement().

This commit is contained in:
Roberto Raggi
2010-01-18 18:31:17 +01:00
parent c943d8e4f1
commit 669c2de681

View File

@@ -472,38 +472,82 @@ bool QmlJSIndenter::matchBracelessControlStatement()
{ {
int delimDepth = 0; int delimDepth = 0;
if (yyLine->endsWith(QLatin1String("else"))) if (! yyLinizerState.tokens.isEmpty()) {
const QmlJSScanner::Token &tk = yyLinizerState.tokens.last();
if (tk.is(QmlJSScanner::Token::Identifier) &&
yyLinizerState.line.midRef(tk.offset, tk.length) == QLatin1String("else"))
return true; return true;
if (!yyLine->endsWith(QLatin1String(")"))) else if (tk.isNot(QmlJSScanner::Token::RightParenthesis))
return false; return false;
}
for (int i = 0; i < SmallRoof; i++) { for (int i = 0; i < SmallRoof; i++) {
int j = yyLine->length(); for (int tokenIndex = yyLinizerState.tokens.size() - 1; tokenIndex != -1; --tokenIndex) {
while (j > 0) { const QmlJSScanner::Token &token = yyLinizerState.tokens.at(tokenIndex);
j--;
QChar ch = yyLine->at(j);
switch (ch.unicode()) { switch (token.kind) {
case ')': default:
delimDepth++;
break; break;
case '(':
delimDepth--; case QmlJSScanner::Token::RightParenthesis:
if (delimDepth == 0) { ++delimDepth;
if (yyLine->indexOf(iflikeKeyword) != -1) { break;
case QmlJSScanner::Token::LeftBrace:
case QmlJSScanner::Token::RightBrace:
case QmlJSScanner::Token::Semicolon:
/*
We met a statement separator, but not where we
expected it. What follows is probably a weird
continuation line. Be careful with ';' in for,
though.
*/
if (token.kind != QmlJSScanner::Token::Semicolon || delimDepth == 0)
return false;
case QmlJSScanner::Token::LeftParenthesis:
--delimDepth;
if (delimDepth == 0 && tokenIndex > 0) {
const QmlJSScanner::Token &tk = yyLinizerState.tokens.at(tokenIndex - 1);
if (tk.is(QmlJSScanner::Token::Identifier)) {
const QStringRef tokenText = yyLinizerState.line.midRef(tk.offset, tk.length);
/* /*
We have We have
if (x) if-like (x)
y y
"if (x)" is not part of the statement "if (x)" is not part of the statement
"y". "y".
*/ */
if (tk.length == 5 && tokenText == QLatin1String("catch"))
return true;
else if (tk.length == 2 && tokenText == QLatin1String("do"))
return true;
else if (tk.length == 3 && tokenText == QLatin1String("for"))
return true;
else if (tk.length == 2 && tokenText == QLatin1String("if"))
return true;
else if (tk.length == 5 && tokenText == QLatin1String("while"))
return true;
else if (tk.length == 4 && tokenText == QLatin1String("with"))
return true; return true;
} }
} }
if (delimDepth == -1) { if (delimDepth == -1) {
/* /*
We have We have
@@ -519,23 +563,14 @@ bool QmlJSIndenter::matchBracelessControlStatement()
return false; return false;
} }
break; break;
case '{':
case '}': } // end of switch
case ';':
/*
We met a statement separator, but not where we
expected it. What follows is probably a weird
continuation line. Be careful with ';' in for,
though.
*/
if (ch != QLatin1Char(';') || delimDepth == 0)
return false;
}
} }
if (!readLine()) if (! readLine())
break; break;
} }
return false; return false;
} }