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