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>
This commit is contained in:
Tobias Hunger
2015-10-14 13:03:20 +02:00
parent 8641277121
commit fe8e14eb4c
2 changed files with 33 additions and 10 deletions

View File

@@ -80,6 +80,7 @@ public:
// -------------------------------------------------------------------- // --------------------------------------------------------------------
const QChar Snippet::kVariableDelimiter(QLatin1Char('$')); const QChar Snippet::kVariableDelimiter(QLatin1Char('$'));
const QChar Snippet::kEscapeChar(QLatin1Char('\\'));
Snippet::Snippet(const QString &groupId, const QString &id) : Snippet::Snippet(const QString &groupId, const QString &id) :
m_isRemoved(false), m_isModified(false), m_groupId(groupId), m_id(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(); result.success = errorMessage.isEmpty();
if (!result.success) { if (!result.success) {
result.text = snippet;
result.errorMessage = errorMessage; result.errorMessage = errorMessage;
return result; return result;
} }
@@ -254,8 +256,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
continue; continue;
} }
if (current == QLatin1Char('\\') && next == QLatin1Char('$')) { if (current == kEscapeChar && (next == kEscapeChar || next == kVariableDelimiter)) {
result.text.append(QLatin1Char('$')); result.text.append(next);
++i; ++i;
continue; continue;
} }
@@ -270,7 +272,7 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
if (!success) { if (!success) {
result.ranges.clear(); result.ranges.clear();
result.text = snippet; result.text = preprocessedSnippet;
} }
return result; return result;
@@ -298,6 +300,9 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
QTest::newRow("empty input") QTest::newRow("empty input")
<< QString::fromLatin1("") << QString::fromLatin1("") << true << QString::fromLatin1("") << QString::fromLatin1("") << true
<< (QList<int>()) << (QList<int>()) << (QList<Core::Id>()); << (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") QTest::newRow("simple identifier")
<< QString::fromLatin1("$tESt$") << QString::fromLatin1("tESt") << true << QString::fromLatin1("$tESt$") << QString::fromLatin1("tESt") << true
@@ -317,13 +322,17 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
<< (QList<Core::Id>() << TCMANGLER_ID); << (QList<Core::Id>() << TCMANGLER_ID);
QTest::newRow("escaped string") QTest::newRow("escaped string")
<< QString::fromLatin1("\\$test\\$") << QString::fromLatin1("$test$") << true << QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("$test$") << true
<< (QList<int>()) << (QList<int>()) << (QList<int>()) << (QList<int>())
<< (QList<Core::Id>()); << (QList<Core::Id>());
QTest::newRow("escaped escape") QTest::newRow("escaped escape")
<< QString::fromLatin1("\\\\$test\\\\$") << QString::fromLatin1("\\test\\") << true << QString::fromLatin1("\\\\\\\\$test$\\\\\\\\") << QString::fromLatin1("\\test\\") << true
<< (QList<int>() << 1) << (QList<int>() << 5) << (QList<int>() << 1) << (QList<int>() << 4)
<< (QList<Core::Id>() << NOMANGLER_ID); << (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") QTest::newRow("Q_PROPERTY")
<< QString::fromLatin1("Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed)") << 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<int>() << 4 << 4 << 4 << 4 << 4)
<< (QList<Core::Id>() << NOMANGLER_ID << NOMANGLER_ID << NOMANGLER_ID << TCMANGLER_ID << NOMANGLER_ID); << (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") QTest::newRow("open identifier")
<< QString::fromLatin1("$test") << QString::fromLatin1("$test") << false << QString::fromLatin1("$test") << QString::fromLatin1("$test") << false
<< (QList<int>()) << (QList<int>()) << (QList<int>()) << (QList<int>())
@@ -360,6 +365,23 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
<< (QList<int>() << 6 << 25) << (QList<int>() << 6 << 25)
<< (QList<int>() << 4 << 4) << (QList<int>() << 4 << 4)
<< (QList<Core::Id>() << NOMANGLER_ID << NOMANGLER_ID); << (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() void Internal::TextEditorPlugin::testSnippetParsing()

View File

@@ -79,6 +79,7 @@ public:
QString generateTip() const; QString generateTip() const;
static const QChar kVariableDelimiter; static const QChar kVariableDelimiter;
static const QChar kEscapeChar;
class ParsedSnippet { class ParsedSnippet {
public: public: