Commit af7f0983 authored by Tobias Hunger's avatar Tobias Hunger

LicenseTemplate: Use macro expansion

Convert existing licensetemplate syntax to macro expansion. That
is way more powerful than what we used to have.

Do return the (converted) license template and do not try to fill
in information since that is not available when querying the
template.

Change-Id: Ia4f45ccf055772b21b0c2ce3c083cf49c45ae7d8
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 61e590a9
......@@ -33,6 +33,9 @@
#include "cppfilesettingspage.h"
#include "cppmodelmanager.h"
#include <utils/fileutils.h>
#include <utils/macroexpander.h>
namespace CppTools {
AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager) :
......@@ -53,7 +56,14 @@ void AbstractEditorSupport::notifyAboutUpdatedContents() const
QString AbstractEditorSupport::licenseTemplate(const QString &file, const QString &className)
{
return Internal::CppFileSettings::licenseTemplate(file, className);
const QString license = Internal::CppFileSettings::licenseTemplate();
Utils::MacroExpander expander;
expander.registerVariable("Cpp:License:FileName", tr("The file name."),
[file]() { return Utils::FileName::fromString(file).fileName(); });
expander.registerVariable("Cpp:License:ClassName", tr("The class name"),
[className]() { return className; });
return expander.expand(license);
}
} // namespace CppTools
......
......@@ -145,28 +145,26 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const
// Replacements of special license template keywords.
static bool keyWordReplacement(const QString &keyWord,
const QString &file,
const QString &className,
QString *value)
{
if (keyWord == QLatin1String("%YEAR%")) {
*value = QString::number(QDate::currentDate().year());
*value = QLatin1String("%{CurrentDate:yyyy}");
return true;
}
if (keyWord == QLatin1String("%MONTH%")) {
*value = QString::number(QDate::currentDate().month());
*value = QLatin1String("%{CurrentDate:M}");
return true;
}
if (keyWord == QLatin1String("%DAY%")) {
*value = QString::number(QDate::currentDate().day());
*value = QLatin1String("%{CurrentDate:d}");
return true;
}
if (keyWord == QLatin1String("%CLASS%")) {
*value = className;
*value = QLatin1String("%{Cpp:License:ClassName}");
return true;
}
if (keyWord == QLatin1String("%FILENAME%")) {
*value = Utils::FileName::fromString(file).fileName();
*value = QLatin1String("%{Cpp:License:FileName}");
return true;
}
if (keyWord == QLatin1String("%DATE%")) {
......@@ -179,17 +177,17 @@ static bool keyWordReplacement(const QString &keyWord,
if (format.count(ypsilon) == 2)
format.insert(format.indexOf(ypsilon), QString(2, ypsilon));
}
*value = QDate::currentDate().toString(format);
*value = QString::fromLatin1("%{CurrentDate:") + format + QLatin1Char('}');
return true;
}
if (keyWord == QLatin1String("%USER%")) {
*value = Utils::Environment::systemEnvironment().userName();
*value = QLatin1String("%{Env:USER}");
return true;
}
// Environment variables (for example '%$EMAIL%').
if (keyWord.startsWith(QLatin1String("%$"))) {
const QString varName = keyWord.mid(2, keyWord.size() - 3);
*value = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit()));
*value = QString::fromLatin1("%{Env:") + varName + QLatin1Char('}');
return true;
}
return false;
......@@ -197,10 +195,11 @@ static bool keyWordReplacement(const QString &keyWord,
// Parse a license template, scan for %KEYWORD% and replace if known.
// Replace '%%' by '%'.
static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, const QString &className)
static void parseLicenseTemplatePlaceholders(QString *t)
{
int pos = 0;
const QChar placeHolder = QLatin1Char('%');
bool isCompatibilityStyle = false;
do {
const int placeHolderPos = t->indexOf(placeHolder, pos);
if (placeHolderPos == -1)
......@@ -214,7 +213,8 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co
} else {
const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos);
QString replacement;
if (keyWordReplacement(keyWord, file, className, &replacement)) {
if (keyWordReplacement(keyWord, &replacement)) {
isCompatibilityStyle = true;
t->replace(placeHolderPos, keyWord.size(), replacement);
pos = placeHolderPos + replacement.size();
} else {
......@@ -223,12 +223,14 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co
}
}
} while (pos < t->size());
if (isCompatibilityStyle)
t->replace(QLatin1Char('\\'), QLatin1String("\\\\"));
}
// Convenience that returns the formatted license template.
QString CppFileSettings::licenseTemplate(const QString &fileName, const QString &className)
QString CppFileSettings::licenseTemplate()
{
const QSettings *s = Core::ICore::settings();
QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP);
key += QLatin1Char('/');
......@@ -247,7 +249,7 @@ QString CppFileSettings::licenseTemplate(const QString &fileName, const QString
licenseStream.setAutoDetectUnicode(true);
QString license = licenseStream.readAll();
parseLicenseTemplatePlaceholders(&license, fileName, className);
parseLicenseTemplatePlaceholders(&license);
// Ensure at least one newline at the end of the license template to separate it from the code
const QChar newLine = QLatin1Char('\n');
......
......@@ -65,7 +65,7 @@ struct CppFileSettings
// Convenience to return a license template completely formatted.
// Currently made public in
static QString licenseTemplate(const QString &file = QString(), const QString &className = QString());
static QString licenseTemplate();
bool equals(const CppFileSettings &rhs) const;
bool operator==(const CppFileSettings &s) const { return equals(s); }
......
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