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 <orgads@gmail.com>
This commit is contained in:
Tobias Hunger
2015-09-14 10:56:10 +02:00
parent 61e590a971
commit af7f098366
3 changed files with 29 additions and 17 deletions

View File

@@ -33,6 +33,9 @@
#include "cppfilesettingspage.h" #include "cppfilesettingspage.h"
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include <utils/fileutils.h>
#include <utils/macroexpander.h>
namespace CppTools { namespace CppTools {
AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager) : AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager) :
@@ -53,7 +56,14 @@ void AbstractEditorSupport::notifyAboutUpdatedContents() const
QString AbstractEditorSupport::licenseTemplate(const QString &file, const QString &className) 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 } // namespace CppTools

View File

@@ -145,28 +145,26 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const
// Replacements of special license template keywords. // Replacements of special license template keywords.
static bool keyWordReplacement(const QString &keyWord, static bool keyWordReplacement(const QString &keyWord,
const QString &file,
const QString &className,
QString *value) QString *value)
{ {
if (keyWord == QLatin1String("%YEAR%")) { if (keyWord == QLatin1String("%YEAR%")) {
*value = QString::number(QDate::currentDate().year()); *value = QLatin1String("%{CurrentDate:yyyy}");
return true; return true;
} }
if (keyWord == QLatin1String("%MONTH%")) { if (keyWord == QLatin1String("%MONTH%")) {
*value = QString::number(QDate::currentDate().month()); *value = QLatin1String("%{CurrentDate:M}");
return true; return true;
} }
if (keyWord == QLatin1String("%DAY%")) { if (keyWord == QLatin1String("%DAY%")) {
*value = QString::number(QDate::currentDate().day()); *value = QLatin1String("%{CurrentDate:d}");
return true; return true;
} }
if (keyWord == QLatin1String("%CLASS%")) { if (keyWord == QLatin1String("%CLASS%")) {
*value = className; *value = QLatin1String("%{Cpp:License:ClassName}");
return true; return true;
} }
if (keyWord == QLatin1String("%FILENAME%")) { if (keyWord == QLatin1String("%FILENAME%")) {
*value = Utils::FileName::fromString(file).fileName(); *value = QLatin1String("%{Cpp:License:FileName}");
return true; return true;
} }
if (keyWord == QLatin1String("%DATE%")) { if (keyWord == QLatin1String("%DATE%")) {
@@ -179,17 +177,17 @@ static bool keyWordReplacement(const QString &keyWord,
if (format.count(ypsilon) == 2) if (format.count(ypsilon) == 2)
format.insert(format.indexOf(ypsilon), QString(2, ypsilon)); format.insert(format.indexOf(ypsilon), QString(2, ypsilon));
} }
*value = QDate::currentDate().toString(format); *value = QString::fromLatin1("%{CurrentDate:") + format + QLatin1Char('}');
return true; return true;
} }
if (keyWord == QLatin1String("%USER%")) { if (keyWord == QLatin1String("%USER%")) {
*value = Utils::Environment::systemEnvironment().userName(); *value = QLatin1String("%{Env:USER}");
return true; return true;
} }
// Environment variables (for example '%$EMAIL%'). // Environment variables (for example '%$EMAIL%').
if (keyWord.startsWith(QLatin1String("%$"))) { if (keyWord.startsWith(QLatin1String("%$"))) {
const QString varName = keyWord.mid(2, keyWord.size() - 3); const QString varName = keyWord.mid(2, keyWord.size() - 3);
*value = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit())); *value = QString::fromLatin1("%{Env:") + varName + QLatin1Char('}');
return true; return true;
} }
return false; return false;
@@ -197,10 +195,11 @@ static bool keyWordReplacement(const QString &keyWord,
// Parse a license template, scan for %KEYWORD% and replace if known. // Parse a license template, scan for %KEYWORD% and replace if known.
// Replace '%%' by '%'. // Replace '%%' by '%'.
static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, const QString &className) static void parseLicenseTemplatePlaceholders(QString *t)
{ {
int pos = 0; int pos = 0;
const QChar placeHolder = QLatin1Char('%'); const QChar placeHolder = QLatin1Char('%');
bool isCompatibilityStyle = false;
do { do {
const int placeHolderPos = t->indexOf(placeHolder, pos); const int placeHolderPos = t->indexOf(placeHolder, pos);
if (placeHolderPos == -1) if (placeHolderPos == -1)
@@ -214,7 +213,8 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co
} else { } else {
const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos); const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos);
QString replacement; QString replacement;
if (keyWordReplacement(keyWord, file, className, &replacement)) { if (keyWordReplacement(keyWord, &replacement)) {
isCompatibilityStyle = true;
t->replace(placeHolderPos, keyWord.size(), replacement); t->replace(placeHolderPos, keyWord.size(), replacement);
pos = placeHolderPos + replacement.size(); pos = placeHolderPos + replacement.size();
} else { } else {
@@ -223,12 +223,14 @@ static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, co
} }
} }
} while (pos < t->size()); } while (pos < t->size());
if (isCompatibilityStyle)
t->replace(QLatin1Char('\\'), QLatin1String("\\\\"));
} }
// Convenience that returns the formatted license template. // 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(); const QSettings *s = Core::ICore::settings();
QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP); QString key = QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP);
key += QLatin1Char('/'); key += QLatin1Char('/');
@@ -247,7 +249,7 @@ QString CppFileSettings::licenseTemplate(const QString &fileName, const QString
licenseStream.setAutoDetectUnicode(true); licenseStream.setAutoDetectUnicode(true);
QString license = licenseStream.readAll(); 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 // Ensure at least one newline at the end of the license template to separate it from the code
const QChar newLine = QLatin1Char('\n'); const QChar newLine = QLatin1Char('\n');

View File

@@ -65,7 +65,7 @@ struct CppFileSettings
// Convenience to return a license template completely formatted. // Convenience to return a license template completely formatted.
// Currently made public in // 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 equals(const CppFileSettings &rhs) const;
bool operator==(const CppFileSettings &s) const { return equals(s); } bool operator==(const CppFileSettings &s) const { return equals(s); }