diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2020-06-12 13:52:38 +0200 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2020-07-01 12:35:59 +0200 |
commit | 05daefeb27e301a29990e8a97a8c8dec70b4a44c (patch) | |
tree | 0a2a806f699ff81b3f01f36856d062ccd1f80d92 | |
parent | f6d849c2d807f268af1c25da1b8ce385e0368418 (diff) |
qmlformat: Copy string literals verbatim
Copy string literals verbatim instead of reformatting them.
Fixes: QTBUG-84599
Change-Id: I36307eb30faa586f50cf0ce2660fb4e2686a3e4a
(cherry picked from commit 237a0e86c700116bfa55a1e1ee6fdabcc1ca5c9c)
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | tests/auto/qml/qmlformat/data/verbatimString.formatted.qml | 4 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/verbatimString.qml | 4 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 7 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 10 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.h | 3 | ||||
-rw-r--r-- | tools/qmlformat/main.cpp | 2 |
6 files changed, 25 insertions, 5 deletions
diff --git a/tests/auto/qml/qmlformat/data/verbatimString.formatted.qml b/tests/auto/qml/qmlformat/data/verbatimString.formatted.qml new file mode 100644 index 0000000000..fa7fbbe32b --- /dev/null +++ b/tests/auto/qml/qmlformat/data/verbatimString.formatted.qml @@ -0,0 +1,4 @@ +Item { + property string verbatim1: 'A "verbatim" string!' + property string verbatim2: "A 'verbatim' string\u2757" +} diff --git a/tests/auto/qml/qmlformat/data/verbatimString.qml b/tests/auto/qml/qmlformat/data/verbatimString.qml new file mode 100644 index 0000000000..15b080a87b --- /dev/null +++ b/tests/auto/qml/qmlformat/data/verbatimString.qml @@ -0,0 +1,4 @@ +Item { +property string verbatim1: 'A "verbatim" string!' +property string verbatim2: "A 'verbatim' string\u2757" +}
\ No newline at end of file diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 5a8974b907..ba34f0f923 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -51,6 +51,7 @@ private Q_SLOTS: void testIfBlocks(); void testReadOnlyProps(); + void testVerbatimStrings(); #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled void testExample(); @@ -217,6 +218,12 @@ void TestQmlformat::testReadOnlyProps() QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); } +void TestQmlformat::testVerbatimStrings() +{ + QCOMPARE(runQmlformat(testFile("verbatimString.qml"), false, true), + readTestFile("verbatimString.formatted.qml")); +} + void TestQmlformat::testLineEndings() { // macos diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index ff0674eded..a92bd83c70 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -30,7 +30,8 @@ #include <QtQml/private/qqmljslexer_p.h> -DumpAstVisitor::DumpAstVisitor(Node *rootNode, CommentAstVisitor *comment): m_comment(comment) +DumpAstVisitor::DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment) + : m_engine(engine), m_comment(comment) { // Add all completely orphaned comments m_result += getOrphanedComments(nullptr); @@ -417,8 +418,11 @@ QString DumpAstVisitor::parseExpression(ExpressionNode *expression) return "--"+parseExpression(cast<PreDecrementExpression *>(expression)->expression); case Node::Kind_NumericLiteral: return QString::number(cast<NumericLiteral *>(expression)->value); - case Node::Kind_StringLiteral: - return escapeString(cast<StringLiteral *>(expression)->value.toString()); + case Node::Kind_StringLiteral: { + auto srcLoc = cast<StringLiteral *>(expression)->firstSourceLocation(); + return m_engine->code().mid(static_cast<int>(srcLoc.begin()), + static_cast<int>(srcLoc.end() - srcLoc.begin())); + } case Node::Kind_BinaryExpression: { auto *binExpr = expression->binaryExpressionCast(); return parseExpression(binExpr->left) + " " + operatorToString(binExpr->op) diff --git a/tools/qmlformat/dumpastvisitor.h b/tools/qmlformat/dumpastvisitor.h index faf067d400..01b7e418e2 100644 --- a/tools/qmlformat/dumpastvisitor.h +++ b/tools/qmlformat/dumpastvisitor.h @@ -43,7 +43,7 @@ using namespace QQmlJS; class DumpAstVisitor : protected Visitor { public: - DumpAstVisitor(Node *rootNode, CommentAstVisitor *comment); + DumpAstVisitor(QQmlJS::Engine *engine, Node *rootNode, CommentAstVisitor *comment); QString toString() const { return m_result; } @@ -143,6 +143,7 @@ private: QStack<ScopeProperties> m_scope_properties; QString m_result = ""; + QQmlJS::Engine *m_engine; CommentAstVisitor *m_comment; }; diff --git a/tools/qmlformat/main.cpp b/tools/qmlformat/main.cpp index 3e71110cf9..3c032d25d6 100644 --- a/tools/qmlformat/main.cpp +++ b/tools/qmlformat/main.cpp @@ -99,7 +99,7 @@ bool parseFile(const QString& filename, bool inplace, bool verbose, bool sortImp if (verbose) qWarning().noquote() << "Dumping" << filename; - DumpAstVisitor dump(parser.rootNode(), &comment); + DumpAstVisitor dump(&engine, parser.rootNode(), &comment); QString dumpCode = dump.toString(); |