Commit 95b0dc91 authored by Tobias Hunger's avatar Tobias Hunger

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: default avatarDaniel Teske <daniel.teske@theqtcompany.com>
parent b899684a
......@@ -196,6 +196,7 @@ tmp/
/tests/auto/treeviewfind/tst_treeviewfind
/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler
/tests/auto/utils/fileutils/tst_fileutils
/tests/auto/utils/templateengine/tst_templateengine
/tests/auto/utils_stringutils/tst_utils_stringutils
/tests/auto/valgrind/callgrind/tst_callgrindparsertests
/tests/auto/valgrind/memcheck/modeldemo
......
......@@ -28,20 +28,30 @@
**
****************************************************************************/
#ifndef CUSTOMWIZARDPREPROCESSOR_H
#define CUSTOMWIZARDPREPROCESSOR_H
#ifndef TEMPLATEENGINE_H
#define TEMPLATEENGINE_H
#include "utils_global.h"
#include "macroexpander.h"
#include <QString>
QT_FORWARD_DECLARE_CLASS(QJSEngine)
QT_FORWARD_DECLARE_CLASS(QJSEngine);
namespace Utils {
class QTCREATOR_UTILS_EXPORT TemplateEngine {
public:
static bool preprocessText(const QString &input, QString *output, QString *errorMessage);
static QString processText(MacroExpander *expander, const QString &input,
QString *errorMessage);
namespace ProjectExplorer {
namespace Internal {
static bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression,
bool *result, QString *errorMessage);
};
bool customWizardPreprocess(const QString &in, QString *out, QString *errorMessage);
/* Helper to evaluate an expression. */
bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage);
} // namespace Internal
} // namespace ProjectExplorer
} // namespace Utils
#endif // CUSTOMWIZARDPREPROCESSOR_H
#endif // TEMPLATEENGINE_H
......@@ -16,6 +16,7 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/shellcommandpage.cpp \
$$PWD/settingsselector.cpp \
$$PWD/stringutils.cpp \
$$PWD/templateengine.cpp \
$$PWD/textfieldcheckbox.cpp \
$$PWD/textfieldcombobox.cpp \
$$PWD/filesearch.cpp \
......@@ -107,6 +108,7 @@ HEADERS += \
$$PWD/shellcommand.h \
$$PWD/shellcommandpage.h \
$$PWD/stringutils.h \
$$PWD/templateengine.h \
$$PWD/textfieldcheckbox.h \
$$PWD/textfieldcombobox.h \
$$PWD/filesearch.h \
......
QT += gui network
QT += gui network qml
include(../../qtcreatorlibrary.pri)
include(utils-lib.pri)
......
......@@ -28,7 +28,7 @@ QtcLibrary {
cpp.frameworks: ["Foundation"]
}
Depends { name: "Qt"; submodules: ["widgets", "network", "script", "concurrent"] }
Depends { name: "Qt"; submodules: ["gui", "network", "qml"] }
Depends { name: "app_version_header" }
files: [
......
HEADERS += $$PWD/customwizard.h \
$$PWD/customwizardparameters.h \
$$PWD/customwizardpage.h \
customwizard/customwizardpreprocessor.h \
customwizard/customwizardscriptgenerator.h
$$PWD/customwizardscriptgenerator.h
SOURCES += $$PWD/customwizard.cpp \
$$PWD/customwizardparameters.cpp \
$$PWD/customwizardpage.cpp \
customwizard/customwizardpreprocessor.cpp \
customwizard/customwizardscriptgenerator.cpp
$$PWD/customwizardscriptgenerator.cpp
......@@ -29,13 +29,14 @@
****************************************************************************/
#include "customwizardparameters.h"
#include "customwizardpreprocessor.h"
#include "customwizardscriptgenerator.h"
#include <coreplugin/icore.h>
#include <cpptools/cpptoolsconstants.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/macroexpander.h>
#include <utils/templateengine.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
......@@ -194,7 +195,7 @@ bool CustomWizardValidationRule::validate(QJSEngine &engine, const QMap<QString,
CustomWizardContext::replaceFields(replacementMap, &cond);
bool valid = false;
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",
qPrintable(cond), qPrintable(errorMessage));
return false;
......@@ -954,7 +955,7 @@ QString CustomWizardContext::processFile(const FieldReplacementMap &fm, QString
QString out;
QString errorMessage;
if (!customWizardPreprocess(in, &out, &errorMessage)) {
if (!Utils::TemplateEngine::preprocessText(in, &out, &errorMessage)) {
qWarning("Error preprocessing custom widget file: %s\nFile:\n%s",
qPrintable(errorMessage), qPrintable(in));
return QString();
......
......@@ -34,7 +34,6 @@
#include "../project.h"
#include "../projectexplorer.h"
#include "../customwizard/customwizardpreprocessor.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/messagemanager.h>
......@@ -202,51 +201,6 @@ QHash<QString, QVariant> JsonWizard::variables() const
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()
{
auto page = qobject_cast<Utils::WizardPage *>(currentPage());
......
......@@ -86,9 +86,6 @@ public:
QHash<QString, QVariant> variables() const override;
static QString processText(Utils::MacroExpander *expander, const QString &input,
QString *errorMessage);
signals:
void preGenerateFiles(); // emitted before files are generated (can happen several times!)
void postGenerateFiles(const JsonWizard::GeneratorFiles &files); // emitted after commitToFileList was called.
......
......@@ -39,6 +39,7 @@
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/macroexpander.h>
#include <utils/templateengine.h>
#include <QCoreApplication>
#include <QDir>
......@@ -164,8 +165,8 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
});
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
gf.setContents(JsonWizard::processText(&nested, QString::fromUtf8(reader.data()),
errorMessage));
gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
errorMessage));
if (!errorMessage->isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
.arg(sourcePath, *errorMessage);
......
......@@ -30,7 +30,6 @@
#include "jsonwizardscannergenerator.h"
#include "../customwizard/customwizardpreprocessor.h"
#include "../projectexplorer.h"
#include "../iprojectmanager.h"
#include "jsonwizard.h"
......
......@@ -219,9 +219,6 @@ private slots:
void testAbiFromTargetTriplet();
void testDeviceManager();
void testCustomWizardPreprocessor_data();
void testCustomWizardPreprocessor();
#endif
};
......
......@@ -192,7 +192,6 @@ QtcPlugin {
"customwizard.cpp", "customwizard.h",
"customwizardpage.cpp", "customwizardpage.h",
"customwizardparameters.cpp", "customwizardparameters.h",
"customwizardpreprocessor.cpp", "customwizardpreprocessor.h",
"customwizardscriptgenerator.cpp", "customwizardscriptgenerator.h"
]
}
......
QTC_LIB_DEPENDS += utils
include(../../qttest.pri)
DEFINES -= QT_USE_FAST_OPERATOR_PLUS QT_USE_FAST_CONCATENATION
SOURCES += tst_templateengine.cpp
import qbs
QtcAutotest {
name: "StringUtils autotest"
Depends { name: "Utils" }
files: "tst_templateengine.cpp"
}
/****************************************************************************
**
** 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 \
ansiescapecodehandler \
stringutils \
templateengine \
treemodel
......@@ -6,6 +6,7 @@ Project {
"fileutils/fileutils.qbs",
"ansiescapecodehandler/ansiescapecodehandler.qbs",
"stringutils/stringutils.qbs",
"templateengine/templateengine.qbs",
"treemodel/treemodel.qbs",
]
}
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