Commit b4bd5449 authored by Christian Kamm's avatar Christian Kamm

QmlJS indenter: Fix problem with object literals.

Reviewed-by: Roberto Raggi
parent 19abd0f2
......@@ -70,8 +70,9 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
for (; m_tokenIndex < m_tokens.size(); ) {
m_currentToken = tokenAt(m_tokenIndex);
const int kind = extendedTokenKind(m_currentToken);
//dump();
//qDebug() << "Token" << m_currentLine.mid(m_currentToken.begin(), m_currentToken.length) << m_tokenIndex << "in line" << block.blockNumber() + 1;
//dump();
if (kind == Comment
&& state().type != multiline_comment_cont
......@@ -285,6 +286,13 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
case RightBracket: leave(); break;
} break;
case objectliteral_open:
if (tryInsideExpression())
break;
switch (kind) {
case RightBrace: leave(); break;
} break;
case bracket_element_start:
switch (kind) {
case Identifier: turnInto(bracket_element_maybe_objectdefinition); break;
......@@ -612,6 +620,7 @@ bool CodeFormatter::tryInsideExpression(bool alsoExpression)
switch (kind) {
case LeftParenthesis: newState = paren_open; break;
case LeftBracket: newState = bracket_open; break;
case LeftBrace: newState = objectliteral_open; break;
case Function: newState = function_start; break;
case Question: newState = ternary_op; break;
}
......
......@@ -134,6 +134,7 @@ public: // must be public to make Q_GADGET introspection work
paren_open, // opening ( in expression
bracket_open, // opening [ in expression
objectliteral_open, // opening { in expression
bracket_element_start, // after starting bracket_open or after ',' in bracket_open
bracket_element_maybe_objectdefinition, // after an identifier in bracket_element_start
......
......@@ -49,6 +49,7 @@ private Q_SLOTS:
// void gnuStyle();
// void whitesmithsStyle();
void expressionContinuation();
void objectLiteral();
};
struct Line {
......@@ -883,6 +884,18 @@ void tst_QMLCodeFormatter::expressionContinuation()
checkIndent(data);
}
void tst_QMLCodeFormatter::objectLiteral()
{
QList<Line> data;
data << Line("function shuffle() {")
<< Line(" for (var i = 0; i < 10; ++i) {")
<< Line(" x[i] = { index: i }")
<< Line(" }")
<< Line("}")
;
checkIndent(data);
}
QTEST_APPLESS_MAIN(tst_QMLCodeFormatter)
#include "tst_qmlcodeformatter.moc"
......
Markdown is supported
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