Commit 08ca3eb4 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

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: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent ec2e01fa
......@@ -32,6 +32,8 @@
#include <coreplugin/id.h>
#include <utils/templateengine.h>
#include <QLatin1Char>
#include <QLatin1String>
#include <QTextDocument>
......@@ -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;
}
......
......@@ -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) { }
......
......@@ -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();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment