forked from qt-creator/qt-creator
Utils: Move template file processing from projectexplorer to utils
I want to use it e.g. for snippets and the TextEditor plugin may not depend on the ProjectExplorer, so the code has to move. This adds a dependency on QtQml to Utils, but that does not really matter since that is loaded into QtCreator anyway. Change-Id: Iada9f40b2966a1fc41631ab33da09812ad67d967 Reviewed-by: Daniel Teske <daniel.teske@theqtcompany.com>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -196,6 +196,7 @@ tmp/
|
|||||||
/tests/auto/treeviewfind/tst_treeviewfind
|
/tests/auto/treeviewfind/tst_treeviewfind
|
||||||
/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler
|
/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler
|
||||||
/tests/auto/utils/fileutils/tst_fileutils
|
/tests/auto/utils/fileutils/tst_fileutils
|
||||||
|
/tests/auto/utils/templateengine/tst_templateengine
|
||||||
/tests/auto/utils_stringutils/tst_utils_stringutils
|
/tests/auto/utils_stringutils/tst_utils_stringutils
|
||||||
/tests/auto/valgrind/callgrind/tst_callgrindparsertests
|
/tests/auto/valgrind/callgrind/tst_callgrindparsertests
|
||||||
/tests/auto/valgrind/memcheck/modeldemo
|
/tests/auto/valgrind/memcheck/modeldemo
|
||||||
|
|||||||
@@ -28,25 +28,17 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "customwizardpreprocessor.h"
|
#include "templateengine.h"
|
||||||
#ifdef WITH_TESTS
|
|
||||||
# include "../projectexplorer.h"
|
|
||||||
# include <QTest>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <utils/qtcassert.h>
|
#include "qtcassert.h"
|
||||||
|
|
||||||
#include <QStringList>
|
|
||||||
#include <QStack>
|
|
||||||
#include <QRegExp>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QJSEngine>
|
#include <QJSEngine>
|
||||||
|
#include <QStack>
|
||||||
|
|
||||||
|
namespace Utils {
|
||||||
|
|
||||||
namespace ProjectExplorer {
|
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
enum { debug = 0 };
|
|
||||||
|
|
||||||
// Preprocessor: Conditional section type.
|
// Preprocessor: Conditional section type.
|
||||||
enum PreprocessorSection {
|
enum PreprocessorSection {
|
||||||
IfSection,
|
IfSection,
|
||||||
@@ -58,7 +50,8 @@ enum PreprocessorSection {
|
|||||||
|
|
||||||
// Preprocessor: Section stack entry containing nested '@if' section
|
// Preprocessor: Section stack entry containing nested '@if' section
|
||||||
// state.
|
// state.
|
||||||
struct PreprocessStackEntry {
|
class PreprocessStackEntry {
|
||||||
|
public:
|
||||||
PreprocessStackEntry(PreprocessorSection section = OtherSection,
|
PreprocessStackEntry(PreprocessorSection section = OtherSection,
|
||||||
bool parentEnabled = true,
|
bool parentEnabled = true,
|
||||||
bool condition = false,
|
bool condition = false,
|
||||||
@@ -70,11 +63,9 @@ struct PreprocessStackEntry {
|
|||||||
bool anyIfClauseMatched; // Determines if 'else' triggers
|
bool anyIfClauseMatched; // Determines if 'else' triggers
|
||||||
};
|
};
|
||||||
|
|
||||||
PreprocessStackEntry::PreprocessStackEntry(PreprocessorSection s,
|
PreprocessStackEntry::PreprocessStackEntry(PreprocessorSection s, bool p, bool c, bool a) :
|
||||||
bool p, bool c, bool a) :
|
|
||||||
section(s), parentEnabled(p), condition(c), anyIfClauseMatched(a)
|
section(s), parentEnabled(p), condition(c), anyIfClauseMatched(a)
|
||||||
{
|
{ }
|
||||||
}
|
|
||||||
|
|
||||||
// Context for preprocessing.
|
// Context for preprocessing.
|
||||||
class PreprocessContext {
|
class PreprocessContext {
|
||||||
@@ -102,13 +93,14 @@ PreprocessContext::PreprocessContext() :
|
|||||||
m_elsePattern(QLatin1String("^[\\s]*@[\\s]*else.*$")),
|
m_elsePattern(QLatin1String("^[\\s]*@[\\s]*else.*$")),
|
||||||
m_endifPattern(QLatin1String("^[\\s]*@[\\s]*endif.*$"))
|
m_endifPattern(QLatin1String("^[\\s]*@[\\s]*endif.*$"))
|
||||||
{
|
{
|
||||||
QTC_ASSERT(m_ifPattern.isValid() && m_elsifPattern.isValid()
|
QTC_CHECK(m_ifPattern.isValid() && m_elsifPattern.isValid()
|
||||||
&& m_elsePattern.isValid() &&m_endifPattern.isValid(), return);
|
&& m_elsePattern.isValid() && m_endifPattern.isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void PreprocessContext::reset()
|
void PreprocessContext::reset()
|
||||||
{
|
{
|
||||||
m_sectionStack.clear(); // Add a default, enabled section.
|
m_sectionStack.clear();
|
||||||
|
// Add a default, enabled section.
|
||||||
m_sectionStack.push(PreprocessStackEntry(OtherSection, true, true));
|
m_sectionStack.push(PreprocessStackEntry(OtherSection, true, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,35 +127,6 @@ PreprocessorSection PreprocessContext::preprocessorLine(const QString &in,
|
|||||||
return OtherSection;
|
return OtherSection;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate an expression within an 'if'/'elsif' to a bool via QJSEngine
|
|
||||||
bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage)
|
|
||||||
{
|
|
||||||
errorMessage->clear();
|
|
||||||
*result = false;
|
|
||||||
const QJSValue value = engine.evaluate(expression);
|
|
||||||
if (value.isError()) {
|
|
||||||
*errorMessage = QString::fromLatin1("Error in \"%1\": %2").
|
|
||||||
arg(expression, value.toString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Try to convert to bool, be that an int or whatever.
|
|
||||||
if (value.isBool()) {
|
|
||||||
*result = value.toBool();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (value.isNumber()) {
|
|
||||||
*result = !qFuzzyCompare(value.toNumber(), 0);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (value.isString()) {
|
|
||||||
*result = !value.toString().isEmpty();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
*errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.").
|
|
||||||
arg(expression, value.toString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline QString msgEmptyStack(int line)
|
static inline QString msgEmptyStack(int line)
|
||||||
{
|
{
|
||||||
return QString::fromLatin1("Unmatched '@endif' at line %1.").arg(line);
|
return QString::fromLatin1("Unmatched '@endif' at line %1.").arg(line);
|
||||||
@@ -184,6 +147,7 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
|
|||||||
for (int l = 0; l < lineCount; l++) {
|
for (int l = 0; l < lineCount; l++) {
|
||||||
// Check for element of the stack (be it dummy, else something is wrong).
|
// Check for element of the stack (be it dummy, else something is wrong).
|
||||||
if (m_sectionStack.isEmpty()) {
|
if (m_sectionStack.isEmpty()) {
|
||||||
|
if (errorMessage)
|
||||||
*errorMessage = msgEmptyStack(l);
|
*errorMessage = msgEmptyStack(l);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -195,32 +159,33 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
|
|||||||
case IfSection:
|
case IfSection:
|
||||||
// '@If': Push new section
|
// '@If': Push new section
|
||||||
if (top.condition) {
|
if (top.condition) {
|
||||||
if (!evaluateBooleanJavaScriptExpression(m_scriptEngine, expression, &expressionValue, errorMessage)) {
|
if (!TemplateEngine::evaluateBooleanJavaScriptExpression(m_scriptEngine, expression,
|
||||||
*errorMessage = QString::fromLatin1("Error in @if at %1: %2").
|
&expressionValue, errorMessage)) {
|
||||||
arg(l + 1).arg(*errorMessage);
|
if (errorMessage)
|
||||||
|
*errorMessage = QString::fromLatin1("Error in @if at %1: %2")
|
||||||
|
.arg(l + 1).arg(*errorMessage);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (debug)
|
|
||||||
qDebug("'%s' : expr='%s' -> %d", qPrintable(lines.at(l)), qPrintable(expression), expressionValue);
|
|
||||||
m_sectionStack.push(PreprocessStackEntry(IfSection,
|
m_sectionStack.push(PreprocessStackEntry(IfSection,
|
||||||
top.condition, expressionValue, expressionValue));
|
top.condition, expressionValue, expressionValue));
|
||||||
break;
|
break;
|
||||||
case ElsifSection: // '@elsif': Check condition.
|
case ElsifSection: // '@elsif': Check condition.
|
||||||
if (top.section != IfSection && top.section != ElsifSection) {
|
if (top.section != IfSection && top.section != ElsifSection) {
|
||||||
*errorMessage = QString::fromLatin1("No preceding @if found for @elsif at %1").
|
if (errorMessage)
|
||||||
arg(l + 1);
|
*errorMessage = QString::fromLatin1("No preceding @if found for @elsif at %1")
|
||||||
|
.arg(l + 1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (top.parentEnabled) {
|
if (top.parentEnabled) {
|
||||||
if (!evaluateBooleanJavaScriptExpression(m_scriptEngine, expression, &expressionValue, errorMessage)) {
|
if (!TemplateEngine::evaluateBooleanJavaScriptExpression(m_scriptEngine, expression,
|
||||||
*errorMessage = QString::fromLatin1("Error in @elsif at %1: %2").
|
&expressionValue, errorMessage)) {
|
||||||
arg(l + 1).arg(*errorMessage);
|
if (errorMessage)
|
||||||
|
*errorMessage = QString::fromLatin1("Error in @elsif at %1: %2")
|
||||||
|
.arg(l + 1).arg(*errorMessage);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (debug)
|
|
||||||
qDebug("'%s' : expr='%s' -> %d", qPrintable(lines.at(l)), qPrintable(expression), expressionValue);
|
|
||||||
top.section = ElsifSection;
|
top.section = ElsifSection;
|
||||||
// ignore consecutive '@elsifs' once something matched
|
// ignore consecutive '@elsifs' once something matched
|
||||||
if (top.anyIfClauseMatched) {
|
if (top.anyIfClauseMatched) {
|
||||||
@@ -232,13 +197,12 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
|
|||||||
break;
|
break;
|
||||||
case ElseSection: // '@else': Check condition.
|
case ElseSection: // '@else': Check condition.
|
||||||
if (top.section != IfSection && top.section != ElsifSection) {
|
if (top.section != IfSection && top.section != ElsifSection) {
|
||||||
*errorMessage = QString::fromLatin1("No preceding @if/@elsif found for @else at %1").
|
if (errorMessage)
|
||||||
arg(l + 1);
|
*errorMessage = QString::fromLatin1("No preceding @if/@elsif found for @else at %1")
|
||||||
|
.arg(l + 1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
expressionValue = top.parentEnabled && !top.anyIfClauseMatched;
|
expressionValue = top.parentEnabled && !top.anyIfClauseMatched;
|
||||||
if (debug)
|
|
||||||
qDebug("%s -> %d", qPrintable(lines.at(l)), expressionValue);
|
|
||||||
top.section = ElseSection;
|
top.section = ElseSection;
|
||||||
top.condition = expressionValue;
|
top.condition = expressionValue;
|
||||||
break;
|
break;
|
||||||
@@ -257,97 +221,96 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
} // namespace Internal
|
||||||
Implements a custom wizard preprocessor based on JavaScript expressions.
|
|
||||||
|
|
||||||
Preprocesses a string using a simple syntax:
|
bool TemplateEngine::preprocessText(const QString &in, QString *out, QString *errorMessage)
|
||||||
\code
|
|
||||||
Text
|
|
||||||
@if <JavaScript-expression>
|
|
||||||
Bla...
|
|
||||||
@elsif <JavaScript-expression2>
|
|
||||||
Blup
|
|
||||||
@endif
|
|
||||||
\endcode
|
|
||||||
|
|
||||||
The JavaScript-expressions must evaluate to integers or boolean, like
|
|
||||||
\c '2 == 1 + 1', \c '"a" == "a"'. The variables of the custom wizard will be
|
|
||||||
expanded before, so \c "%VAR%" should be used for strings and \c %VAR% for integers.
|
|
||||||
|
|
||||||
\sa ProjectExplorer::CustomWizard
|
|
||||||
*/
|
|
||||||
|
|
||||||
bool customWizardPreprocess(const QString &in, QString *out, QString *errorMessage)
|
|
||||||
{
|
{
|
||||||
PreprocessContext context;
|
Internal::PreprocessContext context;
|
||||||
return context.process(in, out, errorMessage);
|
return context.process(in, out, errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Internal
|
QString TemplateEngine::processText(MacroExpander *expander, const QString &input,
|
||||||
|
QString *errorMessage)
|
||||||
#ifdef WITH_TESTS // Run qtcreator -test ProjectExplorer
|
|
||||||
|
|
||||||
void ProjectExplorerPlugin::testCustomWizardPreprocessor_data()
|
|
||||||
{
|
{
|
||||||
QTest::addColumn<QString>("input");
|
if (errorMessage)
|
||||||
QTest::addColumn<QString>("expectedOutput");
|
errorMessage->clear();
|
||||||
QTest::addColumn<bool>("expectedSuccess");
|
|
||||||
QTest::addColumn<QString>("expectedErrorMessage");
|
if (input.isEmpty())
|
||||||
QTest::newRow("if")
|
return input;
|
||||||
<< QString::fromLatin1("@if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
|
|
||||||
<< QString::fromLatin1("line 1")
|
// Recursively expand macros:
|
||||||
<< true << QString();
|
QString in = input;
|
||||||
QTest::newRow("elsif")
|
QString oldIn;
|
||||||
<< QString::fromLatin1("@if 0\nline 1\n@elsif 1\nline 2\n@else\nline 3\n@endif\n")
|
for (int i = 0; i < 5 && in != oldIn; ++i) {
|
||||||
<< QString::fromLatin1("line 2")
|
oldIn = in;
|
||||||
<< true << QString();
|
in = expander->expand(oldIn);
|
||||||
QTest::newRow("else")
|
}
|
||||||
<< QString::fromLatin1("@if 0\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
|
|
||||||
<< QString::fromLatin1("line 3")
|
QString out;
|
||||||
<< true << QString();
|
if (!preprocessText(in, &out, errorMessage))
|
||||||
QTest::newRow("nested-if")
|
return QString();
|
||||||
<< QString::fromLatin1("@if 1\n"
|
|
||||||
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
|
// Expand \n, \t and handle line continuation:
|
||||||
"@else\n"
|
QString result;
|
||||||
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
|
result.reserve(out.count());
|
||||||
"@endif\n")
|
bool isEscaped = false;
|
||||||
<< QString::fromLatin1("line 1")
|
for (int i = 0; i < out.count(); ++i) {
|
||||||
<< true << QString();
|
const QChar c = out.at(i);
|
||||||
QTest::newRow("nested-else")
|
|
||||||
<< QString::fromLatin1("@if 0\n"
|
if (isEscaped) {
|
||||||
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
|
if (c == QLatin1Char('n'))
|
||||||
"@else\n"
|
result.append(QLatin1Char('\n'));
|
||||||
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
|
else if (c == QLatin1Char('t'))
|
||||||
"@endif\n")
|
result.append(QLatin1Char('\t'));
|
||||||
<< QString::fromLatin1("line 4")
|
else if (c != QLatin1Char('\n'))
|
||||||
<< true << QString();
|
result.append(c);
|
||||||
QTest::newRow("twice-nested-if")
|
isEscaped = false;
|
||||||
<< QString::fromLatin1("@if 0\n"
|
} else {
|
||||||
" @if 1\n"
|
if (c == QLatin1Char('\\'))
|
||||||
" @if 1\nline 1\n@else\nline 2\n@endif\n"
|
isEscaped = true;
|
||||||
" @endif\n"
|
else
|
||||||
"@else\n"
|
result.append(c);
|
||||||
" @if 1\n"
|
}
|
||||||
" @if 1\nline 3\n@else\nline 4\n@endif\n"
|
}
|
||||||
" @endif\n"
|
return result;
|
||||||
"@endif\n")
|
|
||||||
<< QString::fromLatin1("line 3")
|
|
||||||
<< true << QString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectExplorerPlugin::testCustomWizardPreprocessor()
|
bool TemplateEngine::evaluateBooleanJavaScriptExpression(QJSEngine &engine,
|
||||||
|
const QString &expression, bool *result,
|
||||||
|
QString *errorMessage)
|
||||||
{
|
{
|
||||||
QFETCH(QString, input);
|
if (errorMessage)
|
||||||
QFETCH(QString, expectedOutput);
|
errorMessage->clear();
|
||||||
QFETCH(bool, expectedSuccess);
|
if (result)
|
||||||
QFETCH(QString, expectedErrorMessage);
|
*result = false;
|
||||||
|
const QJSValue value = engine.evaluate(expression);
|
||||||
|
if (value.isError()) {
|
||||||
|
if (errorMessage)
|
||||||
|
*errorMessage = QString::fromLatin1("Error in \"%1\": %2")
|
||||||
|
.arg(expression, value.toString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Try to convert to bool, be that an int or whatever.
|
||||||
|
if (value.isBool()) {
|
||||||
|
if (result)
|
||||||
|
*result = value.toBool();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (value.isNumber()) {
|
||||||
|
if (result)
|
||||||
|
*result = !qFuzzyCompare(value.toNumber(), 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (value.isString()) {
|
||||||
|
if (result)
|
||||||
|
*result = !value.toString().isEmpty();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (errorMessage)
|
||||||
|
*errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.")
|
||||||
|
.arg(expression, value.toString());
|
||||||
|
|
||||||
QString errorMessage;
|
return false;
|
||||||
QString output;
|
|
||||||
const bool success = Internal::customWizardPreprocess(input, &output, &errorMessage);
|
|
||||||
QCOMPARE(success, expectedSuccess);
|
|
||||||
QCOMPARE(output.trimmed(), expectedOutput.trimmed());
|
|
||||||
QCOMPARE(errorMessage, expectedErrorMessage);
|
|
||||||
}
|
}
|
||||||
#endif // WITH_TESTS
|
|
||||||
} // namespace ProjectExplorer
|
} // namespace Utils
|
||||||
@@ -28,20 +28,30 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef CUSTOMWIZARDPREPROCESSOR_H
|
#ifndef TEMPLATEENGINE_H
|
||||||
#define CUSTOMWIZARDPREPROCESSOR_H
|
#define TEMPLATEENGINE_H
|
||||||
|
|
||||||
|
#include "utils_global.h"
|
||||||
|
|
||||||
|
#include "macroexpander.h"
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
QT_FORWARD_DECLARE_CLASS(QJSEngine)
|
QT_FORWARD_DECLARE_CLASS(QJSEngine);
|
||||||
|
|
||||||
namespace ProjectExplorer {
|
namespace Utils {
|
||||||
namespace Internal {
|
|
||||||
|
|
||||||
bool customWizardPreprocess(const QString &in, QString *out, QString *errorMessage);
|
class QTCREATOR_UTILS_EXPORT TemplateEngine {
|
||||||
/* Helper to evaluate an expression. */
|
public:
|
||||||
bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage);
|
static bool preprocessText(const QString &input, QString *output, QString *errorMessage);
|
||||||
} // namespace Internal
|
|
||||||
} // namespace ProjectExplorer
|
|
||||||
|
|
||||||
#endif // CUSTOMWIZARDPREPROCESSOR_H
|
static QString processText(MacroExpander *expander, const QString &input,
|
||||||
|
QString *errorMessage);
|
||||||
|
|
||||||
|
static bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression,
|
||||||
|
bool *result, QString *errorMessage);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Utils
|
||||||
|
|
||||||
|
#endif // TEMPLATEENGINE_H
|
||||||
@@ -16,6 +16,7 @@ SOURCES += $$PWD/environment.cpp \
|
|||||||
$$PWD/shellcommandpage.cpp \
|
$$PWD/shellcommandpage.cpp \
|
||||||
$$PWD/settingsselector.cpp \
|
$$PWD/settingsselector.cpp \
|
||||||
$$PWD/stringutils.cpp \
|
$$PWD/stringutils.cpp \
|
||||||
|
$$PWD/templateengine.cpp \
|
||||||
$$PWD/textfieldcheckbox.cpp \
|
$$PWD/textfieldcheckbox.cpp \
|
||||||
$$PWD/textfieldcombobox.cpp \
|
$$PWD/textfieldcombobox.cpp \
|
||||||
$$PWD/filesearch.cpp \
|
$$PWD/filesearch.cpp \
|
||||||
@@ -107,6 +108,7 @@ HEADERS += \
|
|||||||
$$PWD/shellcommand.h \
|
$$PWD/shellcommand.h \
|
||||||
$$PWD/shellcommandpage.h \
|
$$PWD/shellcommandpage.h \
|
||||||
$$PWD/stringutils.h \
|
$$PWD/stringutils.h \
|
||||||
|
$$PWD/templateengine.h \
|
||||||
$$PWD/textfieldcheckbox.h \
|
$$PWD/textfieldcheckbox.h \
|
||||||
$$PWD/textfieldcombobox.h \
|
$$PWD/textfieldcombobox.h \
|
||||||
$$PWD/filesearch.h \
|
$$PWD/filesearch.h \
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
QT += gui network
|
QT += gui network qml
|
||||||
|
|
||||||
include(../../qtcreatorlibrary.pri)
|
include(../../qtcreatorlibrary.pri)
|
||||||
include(utils-lib.pri)
|
include(utils-lib.pri)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ QtcLibrary {
|
|||||||
cpp.frameworks: ["Foundation"]
|
cpp.frameworks: ["Foundation"]
|
||||||
}
|
}
|
||||||
|
|
||||||
Depends { name: "Qt"; submodules: ["widgets", "network", "script", "concurrent"] }
|
Depends { name: "Qt"; submodules: ["gui", "network", "qml"] }
|
||||||
Depends { name: "app_version_header" }
|
Depends { name: "app_version_header" }
|
||||||
|
|
||||||
files: [
|
files: [
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
HEADERS += $$PWD/customwizard.h \
|
HEADERS += $$PWD/customwizard.h \
|
||||||
$$PWD/customwizardparameters.h \
|
$$PWD/customwizardparameters.h \
|
||||||
$$PWD/customwizardpage.h \
|
$$PWD/customwizardpage.h \
|
||||||
customwizard/customwizardpreprocessor.h \
|
$$PWD/customwizardscriptgenerator.h
|
||||||
customwizard/customwizardscriptgenerator.h
|
|
||||||
SOURCES += $$PWD/customwizard.cpp \
|
SOURCES += $$PWD/customwizard.cpp \
|
||||||
$$PWD/customwizardparameters.cpp \
|
$$PWD/customwizardparameters.cpp \
|
||||||
$$PWD/customwizardpage.cpp \
|
$$PWD/customwizardpage.cpp \
|
||||||
customwizard/customwizardpreprocessor.cpp \
|
$$PWD/customwizardscriptgenerator.cpp
|
||||||
customwizard/customwizardscriptgenerator.cpp
|
|
||||||
|
|||||||
@@ -29,13 +29,14 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "customwizardparameters.h"
|
#include "customwizardparameters.h"
|
||||||
#include "customwizardpreprocessor.h"
|
|
||||||
#include "customwizardscriptgenerator.h"
|
#include "customwizardscriptgenerator.h"
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <cpptools/cpptoolsconstants.h>
|
#include <cpptools/cpptoolsconstants.h>
|
||||||
|
|
||||||
#include <utils/mimetypes/mimedatabase.h>
|
#include <utils/mimetypes/mimedatabase.h>
|
||||||
|
#include <utils/macroexpander.h>
|
||||||
|
#include <utils/templateengine.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
@@ -194,7 +195,7 @@ bool CustomWizardValidationRule::validate(QJSEngine &engine, const QMap<QString,
|
|||||||
CustomWizardContext::replaceFields(replacementMap, &cond);
|
CustomWizardContext::replaceFields(replacementMap, &cond);
|
||||||
bool valid = false;
|
bool valid = false;
|
||||||
QString errorMessage;
|
QString errorMessage;
|
||||||
if (!evaluateBooleanJavaScriptExpression(engine, cond, &valid, &errorMessage)) {
|
if (!Utils::TemplateEngine::evaluateBooleanJavaScriptExpression(engine, cond, &valid, &errorMessage)) {
|
||||||
qWarning("Error in custom wizard validation expression '%s': %s",
|
qWarning("Error in custom wizard validation expression '%s': %s",
|
||||||
qPrintable(cond), qPrintable(errorMessage));
|
qPrintable(cond), qPrintable(errorMessage));
|
||||||
return false;
|
return false;
|
||||||
@@ -954,7 +955,7 @@ QString CustomWizardContext::processFile(const FieldReplacementMap &fm, QString
|
|||||||
|
|
||||||
QString out;
|
QString out;
|
||||||
QString errorMessage;
|
QString errorMessage;
|
||||||
if (!customWizardPreprocess(in, &out, &errorMessage)) {
|
if (!Utils::TemplateEngine::preprocessText(in, &out, &errorMessage)) {
|
||||||
qWarning("Error preprocessing custom widget file: %s\nFile:\n%s",
|
qWarning("Error preprocessing custom widget file: %s\nFile:\n%s",
|
||||||
qPrintable(errorMessage), qPrintable(in));
|
qPrintable(errorMessage), qPrintable(in));
|
||||||
return QString();
|
return QString();
|
||||||
|
|||||||
@@ -34,7 +34,6 @@
|
|||||||
|
|
||||||
#include "../project.h"
|
#include "../project.h"
|
||||||
#include "../projectexplorer.h"
|
#include "../projectexplorer.h"
|
||||||
#include "../customwizard/customwizardpreprocessor.h"
|
|
||||||
|
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/messagemanager.h>
|
#include <coreplugin/messagemanager.h>
|
||||||
@@ -202,51 +201,6 @@ QHash<QString, QVariant> JsonWizard::variables() const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString JsonWizard::processText(Utils::MacroExpander *expander, const QString &input,
|
|
||||||
QString *errorMessage)
|
|
||||||
{
|
|
||||||
errorMessage->clear();
|
|
||||||
|
|
||||||
if (input.isEmpty())
|
|
||||||
return input;
|
|
||||||
|
|
||||||
// Recursively expand macros:
|
|
||||||
QString in = input;
|
|
||||||
QString oldIn;
|
|
||||||
for (int i = 0; i < 5 && in != oldIn; ++i) {
|
|
||||||
oldIn = in;
|
|
||||||
in = expander->expand(oldIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString out;
|
|
||||||
if (!Internal::customWizardPreprocess(in, &out, errorMessage))
|
|
||||||
return QString();
|
|
||||||
|
|
||||||
// Expand \n, \t and handle line continuation:
|
|
||||||
QString result;
|
|
||||||
result.reserve(out.count());
|
|
||||||
bool isEscaped = false;
|
|
||||||
for (int i = 0; i < out.count(); ++i) {
|
|
||||||
const QChar c = out.at(i);
|
|
||||||
|
|
||||||
if (isEscaped) {
|
|
||||||
if (c == QLatin1Char('n'))
|
|
||||||
result.append(QLatin1Char('\n'));
|
|
||||||
else if (c == QLatin1Char('t'))
|
|
||||||
result.append(QLatin1Char('\t'));
|
|
||||||
else if (c != QLatin1Char('\n'))
|
|
||||||
result.append(c);
|
|
||||||
isEscaped = false;
|
|
||||||
} else {
|
|
||||||
if (c == QLatin1Char('\\'))
|
|
||||||
isEscaped = true;
|
|
||||||
else
|
|
||||||
result.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonWizard::accept()
|
void JsonWizard::accept()
|
||||||
{
|
{
|
||||||
auto page = qobject_cast<Utils::WizardPage *>(currentPage());
|
auto page = qobject_cast<Utils::WizardPage *>(currentPage());
|
||||||
|
|||||||
@@ -86,9 +86,6 @@ public:
|
|||||||
|
|
||||||
QHash<QString, QVariant> variables() const override;
|
QHash<QString, QVariant> variables() const override;
|
||||||
|
|
||||||
static QString processText(Utils::MacroExpander *expander, const QString &input,
|
|
||||||
QString *errorMessage);
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void preGenerateFiles(); // emitted before files are generated (can happen several times!)
|
void preGenerateFiles(); // emitted before files are generated (can happen several times!)
|
||||||
void postGenerateFiles(const JsonWizard::GeneratorFiles &files); // emitted after commitToFileList was called.
|
void postGenerateFiles(const JsonWizard::GeneratorFiles &files); // emitted after commitToFileList was called.
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/macroexpander.h>
|
#include <utils/macroexpander.h>
|
||||||
|
#include <utils/templateengine.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
@@ -164,7 +165,7 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
|
|||||||
});
|
});
|
||||||
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
|
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
|
||||||
|
|
||||||
gf.setContents(JsonWizard::processText(&nested, QString::fromUtf8(reader.data()),
|
gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
|
||||||
errorMessage));
|
errorMessage));
|
||||||
if (!errorMessage->isEmpty()) {
|
if (!errorMessage->isEmpty()) {
|
||||||
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
||||||
|
|||||||
@@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
#include "jsonwizardscannergenerator.h"
|
#include "jsonwizardscannergenerator.h"
|
||||||
|
|
||||||
#include "../customwizard/customwizardpreprocessor.h"
|
|
||||||
#include "../projectexplorer.h"
|
#include "../projectexplorer.h"
|
||||||
#include "../iprojectmanager.h"
|
#include "../iprojectmanager.h"
|
||||||
#include "jsonwizard.h"
|
#include "jsonwizard.h"
|
||||||
|
|||||||
@@ -219,9 +219,6 @@ private slots:
|
|||||||
void testAbiFromTargetTriplet();
|
void testAbiFromTargetTriplet();
|
||||||
|
|
||||||
void testDeviceManager();
|
void testDeviceManager();
|
||||||
|
|
||||||
void testCustomWizardPreprocessor_data();
|
|
||||||
void testCustomWizardPreprocessor();
|
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -192,7 +192,6 @@ QtcPlugin {
|
|||||||
"customwizard.cpp", "customwizard.h",
|
"customwizard.cpp", "customwizard.h",
|
||||||
"customwizardpage.cpp", "customwizardpage.h",
|
"customwizardpage.cpp", "customwizardpage.h",
|
||||||
"customwizardparameters.cpp", "customwizardparameters.h",
|
"customwizardparameters.cpp", "customwizardparameters.h",
|
||||||
"customwizardpreprocessor.cpp", "customwizardpreprocessor.h",
|
|
||||||
"customwizardscriptgenerator.cpp", "customwizardscriptgenerator.h"
|
"customwizardscriptgenerator.cpp", "customwizardscriptgenerator.h"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
6
tests/auto/utils/templateengine/templateengine.pro
Normal file
6
tests/auto/utils/templateengine/templateengine.pro
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
QTC_LIB_DEPENDS += utils
|
||||||
|
include(../../qttest.pri)
|
||||||
|
|
||||||
|
DEFINES -= QT_USE_FAST_OPERATOR_PLUS QT_USE_FAST_CONCATENATION
|
||||||
|
|
||||||
|
SOURCES += tst_templateengine.cpp
|
||||||
7
tests/auto/utils/templateengine/templateengine.qbs
Normal file
7
tests/auto/utils/templateengine/templateengine.qbs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import qbs
|
||||||
|
|
||||||
|
QtcAutotest {
|
||||||
|
name: "StringUtils autotest"
|
||||||
|
Depends { name: "Utils" }
|
||||||
|
files: "tst_templateengine.cpp"
|
||||||
|
}
|
||||||
109
tests/auto/utils/templateengine/tst_templateengine.cpp
Normal file
109
tests/auto/utils/templateengine/tst_templateengine.cpp
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2015 The Qt Company Ltd.
|
||||||
|
** Contact: http://www.qt.io/licensing
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms and
|
||||||
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
||||||
|
** use the contact form at http://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU Lesser General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||||
|
** General Public License version 2.1 or version 3 as published by the Free
|
||||||
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
||||||
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
||||||
|
** following information to ensure the GNU Lesser General Public License
|
||||||
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
||||||
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||||
|
**
|
||||||
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
||||||
|
** rights. These rights are described in The Qt Company LGPL Exception
|
||||||
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <utils/templateengine.h>
|
||||||
|
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
//TESTED_COMPONENT=src/libs/utils
|
||||||
|
|
||||||
|
class tst_TemplateEngine : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testTemplateEngine_data();
|
||||||
|
void testTemplateEngine();
|
||||||
|
};
|
||||||
|
|
||||||
|
void tst_TemplateEngine::testTemplateEngine_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("input");
|
||||||
|
QTest::addColumn<QString>("expectedOutput");
|
||||||
|
QTest::addColumn<QString>("expectedErrorMessage");
|
||||||
|
QTest::newRow("if")
|
||||||
|
<< QString::fromLatin1("@if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
|
||||||
|
<< QString::fromLatin1("line 1\n\n")
|
||||||
|
<< QString();
|
||||||
|
QTest::newRow("elsif")
|
||||||
|
<< QString::fromLatin1("@if 0\nline 1\n@elsif 1\nline 2\n@else\nline 3\n@endif\n")
|
||||||
|
<< QString::fromLatin1("line 2\n\n")
|
||||||
|
<< QString();
|
||||||
|
QTest::newRow("else")
|
||||||
|
<< QString::fromLatin1("@if 0\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
|
||||||
|
<< QString::fromLatin1("line 3\n\n")
|
||||||
|
<< QString();
|
||||||
|
QTest::newRow("nested-if")
|
||||||
|
<< QString::fromLatin1("@if 1\n"
|
||||||
|
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
|
||||||
|
"@else\n"
|
||||||
|
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
|
||||||
|
"@endif\n")
|
||||||
|
<< QString::fromLatin1("line 1\n\n")
|
||||||
|
<< QString();
|
||||||
|
QTest::newRow("nested-else")
|
||||||
|
<< QString::fromLatin1("@if 0\n"
|
||||||
|
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
|
||||||
|
"@else\n"
|
||||||
|
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
|
||||||
|
"@endif\n")
|
||||||
|
<< QString::fromLatin1("line 4\n\n")
|
||||||
|
<< QString();
|
||||||
|
QTest::newRow("twice-nested-if")
|
||||||
|
<< QString::fromLatin1("@if 0\n"
|
||||||
|
" @if 1\n"
|
||||||
|
" @if 1\nline 1\n@else\nline 2\n@endif\n"
|
||||||
|
" @endif\n"
|
||||||
|
"@else\n"
|
||||||
|
" @if 1\n"
|
||||||
|
" @if 1\nline 3\n@else\nline 4\n@endif\n"
|
||||||
|
" @endif\n"
|
||||||
|
"@endif\n")
|
||||||
|
<< QString::fromLatin1("line 3\n\n")
|
||||||
|
<< QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_TemplateEngine::testTemplateEngine()
|
||||||
|
{
|
||||||
|
QFETCH(QString, input);
|
||||||
|
QFETCH(QString, expectedOutput);
|
||||||
|
QFETCH(QString, expectedErrorMessage);
|
||||||
|
|
||||||
|
QString errorMessage;
|
||||||
|
QString output = Utils::TemplateEngine::processText(Utils::globalMacroExpander(), input, &errorMessage);
|
||||||
|
|
||||||
|
QCOMPARE(output, expectedOutput);
|
||||||
|
QCOMPARE(errorMessage, expectedErrorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QTEST_MAIN(tst_TemplateEngine)
|
||||||
|
|
||||||
|
#include "tst_templateengine.moc"
|
||||||
@@ -4,4 +4,5 @@ SUBDIRS = \
|
|||||||
fileutils \
|
fileutils \
|
||||||
ansiescapecodehandler \
|
ansiescapecodehandler \
|
||||||
stringutils \
|
stringutils \
|
||||||
|
templateengine \
|
||||||
treemodel
|
treemodel
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ Project {
|
|||||||
"fileutils/fileutils.qbs",
|
"fileutils/fileutils.qbs",
|
||||||
"ansiescapecodehandler/ansiescapecodehandler.qbs",
|
"ansiescapecodehandler/ansiescapecodehandler.qbs",
|
||||||
"stringutils/stringutils.qbs",
|
"stringutils/stringutils.qbs",
|
||||||
|
"templateengine/templateengine.qbs",
|
||||||
"treemodel/treemodel.qbs",
|
"treemodel/treemodel.qbs",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user