mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 15:37:23 +00:00
add macros support
Macros should be declared as `aw_macro<name,arg1,arg2,...>{{macro body}}` and then they may be used as `aw_name<arg1,arg2,...>{{}}`. It just puts `macro body` to pattern and replaces arguments to provided ones according to macro call. E.g.: ``` $aw_macro<test,phrase>{{phrase}}$aw_test<hello world>{{}} ``` will transform into ``` hello world ```
This commit is contained in:
parent
8e8ac2f3c7
commit
2f88c7ae60
@ -342,6 +342,7 @@ void AWKeyOperations::reinitKeys()
|
||||
m_pattern = AWPatternFunctions::insertKeyCount(m_pattern, allKeys);
|
||||
m_pattern = AWPatternFunctions::insertKeyNames(m_pattern, allKeys);
|
||||
m_pattern = AWPatternFunctions::insertKeys(m_pattern, allKeys);
|
||||
m_pattern = AWPatternFunctions::insertMacros(m_pattern);
|
||||
// wrap templates
|
||||
m_pattern = AWPatternFunctions::expandTemplates(m_pattern);
|
||||
|
||||
|
@ -212,6 +212,48 @@ QString AWPatternFunctions::insertKeys(QString code, const QStringList keys)
|
||||
}
|
||||
|
||||
|
||||
QString AWPatternFunctions::insertMacros(QString code)
|
||||
{
|
||||
qCDebug(LOG_AW) << "Looking for macros in code" << code;
|
||||
|
||||
QList<AWPatternFunctions::AWFunction> found
|
||||
= AWPatternFunctions::findFunctionCalls(QString("aw_macro"), code);
|
||||
for (auto macro : found) {
|
||||
// get macro params
|
||||
if (macro.args.isEmpty()) {
|
||||
qCWarning(LOG_AW) << "No macro name found for" << macro.what;
|
||||
continue;
|
||||
}
|
||||
QString name = macro.args.takeFirst();
|
||||
// find macro usage
|
||||
QList<AWPatternFunctions::AWFunction> macroUsage
|
||||
= AWPatternFunctions::findFunctionCalls(QString("aw_%1").arg(name),
|
||||
code);
|
||||
for (auto function : macroUsage) {
|
||||
if (function.args.count() != macro.args.count()) {
|
||||
qCWarning(LOG_AW) << "Invalid args count found for call"
|
||||
<< function.what << "with macro"
|
||||
<< macro.what;
|
||||
continue;
|
||||
}
|
||||
// generate body to replace
|
||||
QString result = macro.body;
|
||||
for (auto arg : macro.args) {
|
||||
int index = macro.args.indexOf(arg);
|
||||
result.replace(arg, function.args.at(index));
|
||||
}
|
||||
// do replace
|
||||
code.replace(function.what, result);
|
||||
}
|
||||
|
||||
// remove macro from source pattern
|
||||
code.remove(macro.what);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
QStringList AWPatternFunctions::findKeys(const QString code,
|
||||
const QStringList keys,
|
||||
const bool isBars)
|
||||
|
@ -43,6 +43,7 @@ QString insertAllKeys(QString code, const QStringList keys);
|
||||
QString insertKeyCount(QString code, const QStringList keys);
|
||||
QString insertKeyNames(QString code, const QStringList keys);
|
||||
QString insertKeys(QString code, const QStringList keys);
|
||||
QString insertMacros(QString code);
|
||||
// find methods
|
||||
QStringList findKeys(const QString code, const QStringList keys,
|
||||
const bool isBars);
|
||||
|
Loading…
Reference in New Issue
Block a user