From 2f88c7ae607e7506c594d7327ce3ffefa19d8136 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Fri, 1 Jul 2016 02:21:48 +0300 Subject: [PATCH] add macros support Macros should be declared as `aw_macro{{macro body}}` and then they may be used as `aw_name{{}}`. It just puts `macro body` to pattern and replaces arguments to provided ones according to macro call. E.g.: ``` $aw_macro{{phrase}}$aw_test{{}} ``` will transform into ``` hello world ``` --- .../awesome-widget/plugin/awkeyoperations.cpp | 1 + .../plugin/awpatternfunctions.cpp | 42 +++++++++++++++++++ .../plugin/awpatternfunctions.h | 1 + 3 files changed, 44 insertions(+) diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index ea14d0c..3ce11df 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -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); diff --git a/sources/awesome-widget/plugin/awpatternfunctions.cpp b/sources/awesome-widget/plugin/awpatternfunctions.cpp index 1b1950f..ff40215 100644 --- a/sources/awesome-widget/plugin/awpatternfunctions.cpp +++ b/sources/awesome-widget/plugin/awpatternfunctions.cpp @@ -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 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 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) diff --git a/sources/awesome-widget/plugin/awpatternfunctions.h b/sources/awesome-widget/plugin/awpatternfunctions.h index 01575dd..b5544b0 100644 --- a/sources/awesome-widget/plugin/awpatternfunctions.h +++ b/sources/awesome-widget/plugin/awpatternfunctions.h @@ -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);