Commit d0d0a8c0 authored by Christian Kamm's avatar Christian Kamm

QmlJS: Fix indent of object literals.

Reviewed-by: Roberto Raggi
parent e3928941
......@@ -295,9 +295,24 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
if (tryInsideExpression())
break;
switch (kind) {
case Colon: enter(objectliteral_assignment); break;
case RightBracket:
case RightParenthesis: leave(); continue; // error recovery
case RightBrace: leave(); break;
} break;
// pretty much like expression, but ends with , or }
case objectliteral_assignment:
if (tryInsideExpression())
break;
switch (kind) {
case Delimiter: enter(expression_continuation); break;
case RightBracket:
case RightParenthesis: leave(); continue; // error recovery
case RightBrace: leave(); continue; // so we also leave objectliteral_open
case Comma: leave(); break;
} break;
case bracket_element_start:
switch (kind) {
case Identifier: turnInto(bracket_element_maybe_objectdefinition); break;
......@@ -451,7 +466,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
int topState = m_currentState.top().type;
if (topState == expression
|| topState == expression_or_objectdefinition) {
|| topState == expression_or_objectdefinition
|| topState == objectliteral_assignment) {
enter(expression_maybe_continuation);
}
if (topState != multiline_comment_start
......@@ -743,7 +759,8 @@ bool CodeFormatter::isExpressionEndState(int type) const
type == substatement_open ||
type == bracket_open ||
type == paren_open ||
type == case_cont;
type == case_cont ||
type == objectliteral_open;
}
const Token &CodeFormatter::tokenAt(int idx) const
......
......@@ -138,6 +138,8 @@ public: // must be public to make Q_GADGET introspection work
bracket_open, // opening [ in expression
objectliteral_open, // opening { in expression
objectliteral_assignment, // after : in object literal
bracket_element_start, // after starting bracket_open or after ',' in bracket_open
bracket_element_maybe_objectdefinition, // after an identifier in bracket_element_start
......
......@@ -117,6 +117,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
break;
case binding_assignment:
case objectliteral_assignment:
if (lastToken)
*indentDepth = *savedIndentDepth + 4;
else
......@@ -203,6 +204,16 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
*indentDepth = *savedIndentDepth + m_indentSize;
break;
case objectliteral_open:
if (parentState.type == expression || parentState.type == objectliteral_assignment) {
// undo the continuation indent of the expression
*indentDepth = parentState.savedIndentDepth;
*savedIndentDepth = *indentDepth;
}
*indentDepth += m_indentSize;
break;
case statement_with_condition:
case statement_with_block:
case if_statement:
......@@ -290,7 +301,8 @@ void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int lexerSta
const int type = state(i).type;
if (type == objectdefinition_open
|| type == jsblock_open
|| type == substatement_open) {
|| type == substatement_open
|| type == objectliteral_open) {
*indentDepth = state(i).savedIndentDepth;
break;
}
......
......@@ -83,7 +83,10 @@ private Q_SLOTS:
// void gnuStyle();
// void whitesmithsStyle();
void expressionContinuation();
void objectLiteral();
void objectLiteral1();
void objectLiteral2();
void objectLiteral3();
void objectLiteral4();
void keywordStatement();
void namespacedObjects();
};
......@@ -962,7 +965,7 @@ void tst_QMLCodeFormatter::expressionContinuation()
checkIndent(data);
}
void tst_QMLCodeFormatter::objectLiteral()
void tst_QMLCodeFormatter::objectLiteral1()
{
QList<Line> data;
data << Line("function shuffle() {")
......@@ -974,6 +977,59 @@ void tst_QMLCodeFormatter::objectLiteral()
checkIndent(data);
}
void tst_QMLCodeFormatter::objectLiteral2()
{
QList<Line> data;
data << Line("var x = {")
<< Line(" \"x\": 12,")
<< Line(" \"y\": 34,")
<< Line(" z: \"abc\"")
<< Line("}")
;
checkIndent(data);
}
void tst_QMLCodeFormatter::objectLiteral3()
{
QList<Line> data;
data << Line("var x = {")
<< Line(" x: {")
<< Line(" y: 12,")
<< Line(" z: [1, 3]")
<< Line(" },")
<< Line(" \"z\": {")
<< Line(" a: 1 + 2 + 3,")
<< Line(" b: \"12\"")
<< Line(" },")
<< Line(" a: b")
<< Line("}")
;
checkIndent(data);
}
void tst_QMLCodeFormatter::objectLiteral4()
{
QList<Line> data;
data << Line("var x = { a: 12, b: 13 }")
<< Line("y = {")
<< Line(" a: 1 +")
<< Line(" 2 + 3")
<< Line(" + 4")
<< Line("}")
<< Line("y = {")
<< Line(" a: 1 +")
<< Line(" 2 + 3")
<< Line(" + 4,")
<< Line(" b: {")
<< Line(" adef: 1 +")
<< Line(" 2 + 3")
<< Line(" + 4,")
<< Line(" }")
<< Line("}")
;
checkIndent(data);
}
void tst_QMLCodeFormatter::keywordStatement()
{
QList<Line> data;
......
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