Snippet: Use Template engine to pre-process snippets

This enables macros in snippets as well as if/else to select/deselect
lines to have in the snippet.

Change-Id: Ic88fb3277a0f5ac803bcab486b245c688c00822a
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
Tobias Hunger
2015-09-25 10:31:28 +02:00
parent ec2e01faec
commit 08ca3eb480
3 changed files with 27 additions and 10 deletions

View File

@@ -32,6 +32,8 @@
#include <coreplugin/id.h> #include <coreplugin/id.h>
#include <utils/templateengine.h>
#include <QLatin1Char> #include <QLatin1Char>
#include <QLatin1String> #include <QLatin1String>
#include <QTextDocument> #include <QTextDocument>
@@ -192,9 +194,19 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
static TitlecaseMangler tcMangler; static TitlecaseMangler tcMangler;
Snippet::ParsedSnippet result; 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; bool success = true;
int start = -1; int start = -1;
NameMangler *mangler = 0; NameMangler *mangler = 0;
@@ -202,8 +214,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
result.text.reserve(count); result.text.reserve(count);
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
QChar current = snippet.at(i); QChar current = preprocessedSnippet.at(i);
QChar next = (i + 1) < count ? snippet.at(i + 1) : QChar(); QChar next = (i + 1) < count ? preprocessedSnippet.at(i + 1) : QChar();
if (current == Snippet::kVariableDelimiter) { if (current == Snippet::kVariableDelimiter) {
if (start < 0) { if (start < 0) {
@@ -242,12 +254,8 @@ Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
continue; continue;
} }
if (current == QLatin1Char('\\')) { if (current == QLatin1Char('\\') && next == QLatin1Char('$')) {
if (next.isNull()) { result.text.append(QLatin1Char('$'));
success = false;
break;
}
result.text.append(next);
++i; ++i;
continue; continue;
} }

View File

@@ -83,6 +83,7 @@ public:
class ParsedSnippet { class ParsedSnippet {
public: public:
QString text; QString text;
QString errorMessage;
bool success; bool success;
struct Range { struct Range {
Range(int s, int l, NameMangler *m) : start(s), length(l), mangler(m) { } Range(int s, int l, NameMangler *m) : start(s), length(l), mangler(m) { }

View File

@@ -2464,6 +2464,14 @@ void TextEditorWidget::insertCodeSnippet(const QTextCursor &cursor_arg, const QS
{ {
Snippet::ParsedSnippet data = Snippet::parse(snippet); 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; QTextCursor cursor = cursor_arg;
cursor.beginEditBlock(); cursor.beginEditBlock();
cursor.removeSelectedText(); cursor.removeSelectedText();