diff options
author | Evgeniy A. Dushistov <dushistov@mail.ru> | 2020-08-15 13:13:45 +0300 |
---|---|---|
committer | Evgeniy A. Dushistov <dushistov@mail.ru> | 2020-08-20 17:23:40 +0300 |
commit | ac68310b7857693c9eb7ccb782133ac9ec8e2328 (patch) | |
tree | 2986baa269480ab4f403ef024e95941faa3f9802 /tools/qmlformat | |
parent | f04497b5d24e908beb50dff04b0a03358d7993c7 (diff) |
qmlformat: fix handling of "if else" inside "if" in "if else"
Fixes: QTBUG-86060
Change-Id: I7a191c451793bf4e1f6a2bd02ceb36b5957136c2
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools/qmlformat')
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 53 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.h | 2 |
2 files changed, 41 insertions, 14 deletions
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 0b656e8817..98720a7a77 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -252,7 +252,7 @@ QString DumpAstVisitor::parsePatternElement(PatternElement *element, bool scope) } } -QString escapeString(QString string) +static QString escapeString(QString string) { // Handle escape sequences string = string.replace("\r", "\\r").replace("\n", "\\n").replace("\t", "\\t") @@ -584,7 +584,7 @@ QString DumpAstVisitor::parseExportsList(ExportsList *list) return result; } -bool needsSemicolon(int kind) +static bool needsSemicolon(int kind) { switch (kind) { case Node::Kind_ForStatement: @@ -627,6 +627,11 @@ QString DumpAstVisitor::parseBlock(Block *block, bool hasNext, bool allowBracele return result; } +static bool endsWithSemicolon(const QStringView s) +{ + return s.trimmed().endsWith(';'); +} + QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, bool blockAllowBraceless) { @@ -674,10 +679,11 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, ifTrue = parseStatement(ifStatement->ok, !ifFalse.isEmpty(), false); } - if (ifStatement->ok->kind != Node::Kind_Block) + if (ifStatement->ok->kind != Node::Kind_Block && !endsWithSemicolon(ifTrue)) ifTrue += ";"; - if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block && ifStatement->ko->kind != Node::Kind_IfStatement) + if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block + && ifStatement->ko->kind != Node::Kind_IfStatement && !endsWithSemicolon(ifFalse)) ifFalse += ";"; QString result = "if (" + parseExpression(ifStatement->expression) + ")"; @@ -709,7 +715,7 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, } else { result += "\n"; m_indentLevel++; - result += formatLine(ifTrue); + result += formatPartlyFormatedLines(ifTrue); m_indentLevel--; } @@ -729,7 +735,7 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext, } else { result += "\n"; m_indentLevel++; - result += formatLine(ifFalse, false); + result += formatPartlyFormatedLines(ifFalse, false); m_indentLevel--; } } @@ -963,18 +969,39 @@ bool DumpAstVisitor::visit(UiPublicMember *node) { return true; } -QString DumpAstVisitor::generateIndent() const { +static QString generateIndent(int indentLevel) +{ constexpr int IDENT_WIDTH = 4; - QString indent = ""; - for (int i = 0; i < IDENT_WIDTH*m_indentLevel; i++) - indent += " "; + return QString(IDENT_WIDTH * indentLevel, ' '); +} + +QString DumpAstVisitor::formatLine(QString line, bool newline) const +{ + QString result = generateIndent(m_indentLevel) + line; + if (newline) + result += "\n"; - return indent; + return result; } -QString DumpAstVisitor::formatLine(QString line, bool newline) const { - QString result = generateIndent() + line; +QString DumpAstVisitor::formatPartlyFormatedLines(QString line, bool newline) const +{ + QString result; + + const auto lines = line.splitRef('\n'); + auto it = lines.cbegin(); + const auto endi = lines.cend(); + if (it != endi) { + result += generateIndent(m_indentLevel) + *it; + ++it; + const QString addonIdent = generateIndent(1); + for (; it != endi; ++it) { + result += '\n'; + result += addonIdent + *it; + } + } + if (newline) result += "\n"; diff --git a/tools/qmlformat/dumpastvisitor.h b/tools/qmlformat/dumpastvisitor.h index f84c4859ae..fbbd6871eb 100644 --- a/tools/qmlformat/dumpastvisitor.h +++ b/tools/qmlformat/dumpastvisitor.h @@ -95,8 +95,8 @@ private: QHash<QString, UiObjectMember*> m_bindings; }; - QString generateIndent() const; QString formatLine(QString line, bool newline = true) const; + QString formatPartlyFormatedLines(QString line, bool newline = true) const; QString formatComment(const Comment &comment) const; |