diff --git a/src/plugins/texteditor/snippets/snippet.cpp b/src/plugins/texteditor/snippets/snippet.cpp index 8c63c60911e..03ab97c76c7 100644 --- a/src/plugins/texteditor/snippets/snippet.cpp +++ b/src/plugins/texteditor/snippets/snippet.cpp @@ -32,6 +32,8 @@ #include +#include + #include #include #include @@ -192,9 +194,19 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) static TitlecaseMangler tcMangler; Snippet::ParsedSnippet result; - result.success = true; - const int count = snippet.count(); + QString errorMessage; + QString preprocessedSnippet + = Utils::TemplateEngine::processText(Utils::globalMacroExpander(), snippet, + &errorMessage); + + result.success = errorMessage.isEmpty(); + if (!result.success) { + result.errorMessage = errorMessage; + return result; + } + + const int count = preprocessedSnippet.count(); bool success = true; int start = -1; NameMangler *mangler = 0; @@ -202,8 +214,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) result.text.reserve(count); for (int i = 0; i < count; ++i) { - QChar current = snippet.at(i); - QChar next = (i + 1) < count ? snippet.at(i + 1) : QChar(); + QChar current = preprocessedSnippet.at(i); + QChar next = (i + 1) < count ? preprocessedSnippet.at(i + 1) : QChar(); if (current == Snippet::kVariableDelimiter) { if (start < 0) { @@ -242,12 +254,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet) continue; } - if (current == QLatin1Char('\\')) { - if (next.isNull()) { - success = false; - break; - } - result.text.append(next); + if (current == QLatin1Char('\\') && next == QLatin1Char('$')) { + result.text.append(QLatin1Char('$')); ++i; continue; } diff --git a/src/plugins/texteditor/snippets/snippet.h b/src/plugins/texteditor/snippets/snippet.h index 53d61908489..0ee74fcfb52 100644 --- a/src/plugins/texteditor/snippets/snippet.h +++ b/src/plugins/texteditor/snippets/snippet.h @@ -83,6 +83,7 @@ public: class ParsedSnippet { public: QString text; + QString errorMessage; bool success; struct Range { Range(int s, int l, NameMangler *m) : start(s), length(l), mangler(m) { } diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index ea968f8e6a6..081ad2e7571 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -2464,6 +2464,14 @@ void TextEditorWidget::insertCodeSnippet(const QTextCursor &cursor_arg, const QS { Snippet::ParsedSnippet data = Snippet::parse(snippet); + if (!data.success) { + QString message = QString::fromLatin1("Cannot parse snippet \"%1\".").arg(snippet); + if (!data.errorMessage.isEmpty()) + message += QLatin1String("\nParse error: ") + data.errorMessage; + QMessageBox::warning(this, QLatin1String("Snippet Parse Error"), message); + return; + } + QTextCursor cursor = cursor_arg; cursor.beginEditBlock(); cursor.removeSelectedText();