summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/snippets/snippet.cpp
diff options
context:
space:
mode:
authorTobias Hunger <tobias.hunger@theqtcompany.com>2015-10-14 13:03:20 +0200
committerTobias Hunger <tobias.hunger@theqtcompany.com>2015-10-14 12:05:20 +0000
commitfe8e14eb4c3c16375180186a52cc098f518fcc61 (patch)
tree0459eeaf83d33f27a115409143b7055861887e06 /src/plugins/texteditor/snippets/snippet.cpp
parent8641277121632de574e1bc76a76d8727e9b7cd03 (diff)
downloadqt-creator-fe8e14eb4c3c16375180186a52cc098f518fcc61.tar.gz
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 <david.schulz@theqtcompany.com>
Diffstat (limited to 'src/plugins/texteditor/snippets/snippet.cpp')
-rw-r--r--src/plugins/texteditor/snippets/snippet.cpp42
1 files changed, 32 insertions, 10 deletions
diff --git a/src/plugins/texteditor/snippets/snippet.cpp b/src/plugins/texteditor/snippets/snippet.cpp
index 03ab97c76c..b23b1015ae 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<int>()) << (QList<int>()) << (QList<Core::Id>());
+ QTest::newRow("newline only")
+ << QString::fromLatin1("\n") << QString::fromLatin1("\n") << true
+ << (QList<int>()) << (QList<int>()) << (QList<Core::Id>());
QTest::newRow("simple identifier")
<< QString::fromLatin1("$tESt$") << QString::fromLatin1("tESt") << true
@@ -317,13 +322,17 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
<< (QList<Core::Id>() << TCMANGLER_ID);
QTest::newRow("escaped string")
- << QString::fromLatin1("\\$test\\$") << QString::fromLatin1("$test$") << true
+ << QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("$test$") << true
<< (QList<int>()) << (QList<int>())
<< (QList<Core::Id>());
QTest::newRow("escaped escape")
- << QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("\\test\\") << true
- << (QList<int>() << 1) << (QList<int>() << 5)
+ << QString::fromLatin1("\\\\\\\\$test$\\\\\\\\") << QString::fromLatin1("\\test\\") << true
+ << (QList<int>() << 1) << (QList<int>() << 4)
<< (QList<Core::Id>() << NOMANGLER_ID);
+ QTest::newRow("broken escape")
+ << QString::fromLatin1("\\\\$test\\\\\\\\$\\\\") << QString::fromLatin1("\\$test\\\\$\\") << false
+ << (QList<int>()) << (QList<int>())
+ << (QList<Core::Id>());
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<int>() << 4 << 4 << 4 << 4 << 4)
<< (QList<Core::Id>() << NOMANGLER_ID << NOMANGLER_ID << NOMANGLER_ID << TCMANGLER_ID << NOMANGLER_ID);
- QTest::newRow("broken escape")
- << QString::fromLatin1("\\\\$test\\\\$\\") << QString::fromLatin1("\\\\$test\\\\$\\") << false
- << (QList<int>()) << (QList<int>())
- << (QList<Core::Id>());
QTest::newRow("open identifier")
<< QString::fromLatin1("$test") << QString::fromLatin1("$test") << false
<< (QList<int>()) << (QList<int>())
@@ -360,6 +365,23 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
<< (QList<int>() << 6 << 25)
<< (QList<int>() << 4 << 4)
<< (QList<Core::Id>() << 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<int>() << 6 << 23)
+ << (QList<int>() << 4 << 4)
+ << (QList<Core::Id>() << NOMANGLER_ID << NOMANGLER_ID);
+
}
void Internal::TextEditorPlugin::testSnippetParsing()