diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-19 13:49:17 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-25 11:24:32 +0100 |
commit | 8ab467835cdf590bcbc3a4d520f0e44dc8b457d9 (patch) | |
tree | c58491f1bc8787022ab444fec3aa04a234e58d30 /tools | |
parent | b1f53bf6eaabc9caa4f1eb523ad605c6719c752c (diff) |
qmlformat: Fix inconsistent if statements
Should now produce more consistent output for if statements.
Fixes: QTBUG-82261
Change-Id: I39da0c80c4aadc2c5bdef32953c34ed9f0708a9e
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 565e188160..ff0674eded 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -626,7 +626,7 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, case Node::Kind_IfStatement: { auto *ifStatement = cast<IfStatement *>(statement); - m_blockNeededBraces = false; + m_blockNeededBraces = !blockAllowBraceless; QString ifFalse = parseStatement(ifStatement->ko, false, true); QString ifTrue = parseStatement(ifStatement->ok, !ifFalse.isEmpty(), true); @@ -641,8 +641,36 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, ifTrue = parseStatement(ifStatement->ok, !ifFalse.isEmpty(), false); } + if (ifStatement->ok->kind != Node::Kind_Block) + ifTrue += ";"; + + if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block && ifStatement->ko->kind != Node::Kind_IfStatement) + ifFalse += ";"; + QString result = "if (" + parseExpression(ifStatement->expression) + ")"; + if (m_blockNeededBraces) { + if (ifStatement->ok->kind != Node::Kind_Block) { + QString result = "{\n"; + m_indentLevel++; + result += formatLine(ifTrue); + m_indentLevel--; + result += formatLine("} ", false); + ifTrue = result; + ifTrueBlock = true; + } + + if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block && ifStatement->ko->kind != Node::Kind_IfStatement) { + QString result = "{\n"; + m_indentLevel++; + result += formatLine(ifFalse); + m_indentLevel--; + result += formatLine("} ", false); + ifFalse = result; + ifFalseBlock = true; + } + } + if (ifTrueBlock) { result += " " + ifTrue; } else { @@ -724,8 +752,6 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, else result += ";"; - - return result; } case Node::Kind_WhileStatement: { @@ -825,7 +851,7 @@ QString DumpAstVisitor::parseStatementList(StatementList *list) result += getOrphanedComments(list); for (auto *item = list; item != nullptr; item = item->next) { - QString statement = parseStatement(item->statement->statementCast()); + QString statement = parseStatement(item->statement->statementCast(), false, true); if (statement.isEmpty()) continue; |