From fe8e14eb4c3c16375180186a52cc098f518fcc61 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 14 Oct 2015 13:03:20 +0200 Subject: [PATCH] Snippets: Make it possible again to escape $fields$ Update the unit tests while at it. Add some new tests, rearrange existing ones. Change-Id: Icc2db644f8fe9752c1bf8e66b134738c27b0fb25 Reviewed-by: David Schulz --- src/plugins/texteditor/snippets/snippet.cpp | 42 ++++++++++++++++----- src/plugins/texteditor/snippets/snippet.h | 1 + 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/plugins/texteditor/snippets/snippet.cpp b/src/plugins/texteditor/snippets/snippet.cpp index 03ab97c76c7..b23b1015ae7 100644 --- a/src/plugins/texteditor/snippets/snippet.cpp +++ b/src/plugins/texteditor/snippets/snippet.cpp @@ -80,6 +80,7 @@ public: // -------------------------------------------------------------------- const QChar Snippet::kVariableDelimiter(QLatin1Char('$')); +const QChar Snippet::kEscapeChar(QLatin1Char('\\')); Snippet::Snippet(const QString &groupId, const QString &id) : m_isRemoved(false), m_isModified(false), m_groupId(groupId), m_id(id) @@ -202,6 +203,7 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) result.success = errorMessage.isEmpty(); if (!result.success) { + result.text = snippet; result.errorMessage = errorMessage; return result; } @@ -254,8 +256,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) continue; } - if (current == QLatin1Char('\\') && next == QLatin1Char('$')) { - result.text.append(QLatin1Char('$')); + if (current == kEscapeChar && (next == kEscapeChar || next == kVariableDelimiter)) { + result.text.append(next); ++i; continue; } @@ -270,7 +272,7 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) if (!success) { result.ranges.clear(); - result.text = snippet; + result.text = preprocessedSnippet; } return result; @@ -298,6 +300,9 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() QTest::newRow("empty input") << QString::fromLatin1("") << QString::fromLatin1("") << true << (QList()) << (QList()) << (QList()); + QTest::newRow("newline only") + << QString::fromLatin1("\n") << QString::fromLatin1("\n") << true + << (QList()) << (QList()) << (QList()); QTest::newRow("simple identifier") << QString::fromLatin1("$tESt$") << QString::fromLatin1("tESt") << true @@ -317,13 +322,17 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() << (QList() << TCMANGLER_ID); QTest::newRow("escaped string") - << QString::fromLatin1("\\$test\\$") << QString::fromLatin1("$test$") << true + << QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("$test$") << true << (QList()) << (QList()) << (QList()); QTest::newRow("escaped escape") - << QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("\\test\\") << true - << (QList() << 1) << (QList() << 5) + << QString::fromLatin1("\\\\\\\\$test$\\\\\\\\") << QString::fromLatin1("\\test\\") << true + << (QList() << 1) << (QList() << 4) << (QList() << NOMANGLER_ID); + QTest::newRow("broken escape") + << QString::fromLatin1("\\\\$test\\\\\\\\$\\\\") << QString::fromLatin1("\\$test\\\\$\\") << false + << (QList()) << (QList()) + << (QList()); QTest::newRow("Q_PROPERTY") << QString::fromLatin1("Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed)") @@ -332,10 +341,6 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() << (QList() << 4 << 4 << 4 << 4 << 4) << (QList() << NOMANGLER_ID << NOMANGLER_ID << NOMANGLER_ID << TCMANGLER_ID << NOMANGLER_ID); - QTest::newRow("broken escape") - << QString::fromLatin1("\\\\$test\\\\$\\") << QString::fromLatin1("\\\\$test\\\\$\\") << false - << (QList()) << (QList()) - << (QList()); QTest::newRow("open identifier") << QString::fromLatin1("$test") << QString::fromLatin1("$test") << false << (QList()) << (QList()) @@ -360,6 +365,23 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() << (QList() << 6 << 25) << (QList() << 4 << 4) << (QList() << NOMANGLER_ID << NOMANGLER_ID); + + QTest::newRow("escape sequences") + << QString::fromLatin1("class $name$\\n" + "{\\n" + "public\\\\:\\n" + "\\t$name$() {}\\n" + "};") + << QString::fromLatin1("class name\n" + "{\n" + "public\\:\n" + "\tname() {}\n" + "};") + << true + << (QList() << 6 << 23) + << (QList() << 4 << 4) + << (QList() << NOMANGLER_ID << NOMANGLER_ID); + } void Internal::TextEditorPlugin::testSnippetParsing() diff --git a/src/plugins/texteditor/snippets/snippet.h b/src/plugins/texteditor/snippets/snippet.h index 0ee74fcfb52..6f792bbbfdc 100644 --- a/src/plugins/texteditor/snippets/snippet.h +++ b/src/plugins/texteditor/snippets/snippet.h @@ -79,6 +79,7 @@ public: QString generateTip() const; static const QChar kVariableDelimiter; + static const QChar kEscapeChar; class ParsedSnippet { public: