refactor: refresh aw plugin

This commit is contained in:
2024-04-16 18:33:19 +03:00
parent 54acc5f780
commit 397b523180
41 changed files with 251 additions and 305 deletions

View File

@ -36,7 +36,7 @@ QString AWPatternFunctions::expandLambdas(QString _code, AWKeysAggregator *_aggr
for (auto &lambdaKey : _usedKeys)
_code.replace(QString("$%1").arg(lambdaKey), _aggregator->formatter(_metadata[lambdaKey], lambdaKey, false));
qCInfo(LOG_AW) << "Expression" << _code;
QJSValue result = engine.evaluate(_code);
auto result = engine.evaluate(_code);
if (result.isError()) {
qCWarning(LOG_AW) << "Uncaught exception at line" << result.property("lineNumber").toInt() << ":"
<< result.toString();
@ -52,17 +52,17 @@ QString AWPatternFunctions::expandTemplates(QString _code)
qCDebug(LOG_AW) << "Expand templates in" << _code;
// match the following construction $template{{some code here}}
QRegularExpression templatesRegexp(R"(\$template\{\{(?<body>.*?)\}\})");
static QRegularExpression templatesRegexp(R"(\$template\{\{(?<body>.*?)\}\})");
templatesRegexp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = templatesRegexp.globalMatch(_code);
auto it = templatesRegexp.globalMatch(_code);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString body = match.captured("body");
auto match = it.next();
auto body = match.captured("body");
QJSEngine engine;
qCInfo(LOG_AW) << "Expression" << body;
QJSValue result = engine.evaluate(body);
auto result = engine.evaluate(body);
QString templateResult = "";
if (result.isError()) {
qCWarning(LOG_AW) << "Uncaught exception at line" << result.property("lineNumber").toInt() << ":"
@ -94,20 +94,20 @@ QList<AWPatternFunctions::AWFunction> AWPatternFunctions::findFunctionCalls(cons
regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QList<AWPatternFunctions::AWFunction> foundFunctions;
QRegularExpressionMatchIterator it = regex.globalMatch(_code);
auto it = regex.globalMatch(_code);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
auto match = it.next();
AWPatternFunctions::AWFunction metadata;
// work with args
QString argsString = match.captured("args");
auto argsString = match.captured("args");
if (argsString.isEmpty()) {
metadata.args = QStringList();
} else {
// replace '$,' to 0x1d
argsString.replace("$,", QChar(0x1d));
QStringList args = argsString.split(',');
std::for_each(args.begin(), args.end(), [](QString &arg) { arg.replace(QChar(0x1d), ","); });
auto args = argsString.split(',');
std::for_each(args.begin(), args.end(), [](auto &arg) { arg.replace(QChar(0x1d), ","); });
metadata.args = args;
}
// other variables
@ -127,11 +127,11 @@ QString AWPatternFunctions::insertAllKeys(QString _code, const QStringList &_key
{
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
QList<AWPatternFunctions::AWFunction> found = AWPatternFunctions::findFunctionCalls("aw_all", _code);
auto found = AWPatternFunctions::findFunctionCalls("aw_all", _code);
for (auto &function : found) {
QString separator = function.args.isEmpty() ? "," : function.args.at(0);
QStringList required = _keys.filter(QRegularExpression(function.body));
std::for_each(required.begin(), required.end(), [](QString &value) { value = QString("%1: $%1").arg(value); });
auto separator = function.args.isEmpty() ? "," : function.args.at(0);
auto required = _keys.filter(QRegularExpression(function.body));
std::for_each(required.begin(), required.end(), [](auto &value) { value = QString("%1: $%1").arg(value); });
_code.replace(function.what, required.join(separator));
}
@ -144,9 +144,9 @@ QString AWPatternFunctions::insertKeyCount(QString _code, const QStringList &_ke
{
qCDebug(LOG_AW) << "Looking for count in code" << _code << "using list" << _keys;
QList<AWPatternFunctions::AWFunction> found = AWPatternFunctions::findFunctionCalls("aw_count", _code);
auto found = AWPatternFunctions::findFunctionCalls("aw_count", _code);
for (auto &function : found) {
int count = _keys.filter(QRegularExpression(function.body)).count();
auto count = _keys.filter(QRegularExpression(function.body)).count();
_code.replace(function.what, QString::number(count));
}
@ -159,10 +159,10 @@ QString AWPatternFunctions::insertKeyNames(QString _code, const QStringList &_ke
{
qCDebug(LOG_AW) << "Looking for key names in code" << _code << "using list" << _keys;
QList<AWPatternFunctions::AWFunction> found = AWPatternFunctions::findFunctionCalls("aw_names", _code);
auto found = AWPatternFunctions::findFunctionCalls("aw_names", _code);
for (auto &function : found) {
QString separator = function.args.isEmpty() ? "," : function.args.at(0);
QStringList required = _keys.filter(QRegularExpression(function.body));
auto separator = function.args.isEmpty() ? "," : function.args.at(0);
auto required = _keys.filter(QRegularExpression(function.body));
_code.replace(function.what, required.join(separator));
}
@ -175,11 +175,11 @@ QString AWPatternFunctions::insertKeys(QString _code, const QStringList &_keys)
{
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
QList<AWPatternFunctions::AWFunction> found = AWPatternFunctions::findFunctionCalls("aw_keys", _code);
auto found = AWPatternFunctions::findFunctionCalls("aw_keys", _code);
for (auto &function : found) {
QString separator = function.args.isEmpty() ? "," : function.args.at(0);
QStringList required = _keys.filter(QRegularExpression(function.body));
std::for_each(required.begin(), required.end(), [](QString &value) { value = QString("$%1").arg(value); });
auto separator = function.args.isEmpty() ? "," : function.args.at(0);
auto required = _keys.filter(QRegularExpression(function.body));
std::for_each(required.begin(), required.end(), [](auto &value) { value = QString("$%1").arg(value); });
_code.replace(function.what, required.join(separator));
}
@ -192,26 +192,25 @@ QString AWPatternFunctions::insertMacros(QString _code)
{
qCDebug(LOG_AW) << "Looking for macros in code" << _code;
QList<AWPatternFunctions::AWFunction> found = AWPatternFunctions::findFunctionCalls("aw_macro", _code);
auto found = AWPatternFunctions::findFunctionCalls("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();
auto name = macro.args.takeFirst();
// find macro usage
QList<AWPatternFunctions::AWFunction> macroUsage
= AWPatternFunctions::findFunctionCalls(QString("aw_macro_%1").arg(name), _code);
auto macroUsage = AWPatternFunctions::findFunctionCalls(QString("aw_macro_%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;
std::for_each(macro.args.cbegin(), macro.args.cend(), [&result, macro, function](const QString &arg) {
int index = macro.args.indexOf(arg);
auto result = macro.body;
std::for_each(macro.args.cbegin(), macro.args.cend(), [&result, macro, function](auto &arg) {
auto index = macro.args.indexOf(arg);
result.replace(QString("$%1").arg(arg), function.args.at(index));
});
// do replace
@ -231,7 +230,7 @@ QStringList AWPatternFunctions::findKeys(const QString &_code, const QStringList
qCDebug(LOG_AW) << "Looking for keys in code" << _code << "using list" << _keys;
QStringList selectedKeys;
QString replacedCode = _code;
auto replacedCode = _code;
for (auto &key : _keys)
if ((key.startsWith("bar") == _isBars) && (replacedCode.contains(QString("$%1").arg(key)))) {
qCInfo(LOG_AW) << "Found key" << key << "with bar enabled" << _isBars;
@ -251,13 +250,13 @@ QStringList AWPatternFunctions::findLambdas(const QString &_code)
QStringList selectedKeys;
// match the following construction ${{some code here}}
QRegularExpression lambdaRegexp(R"(\$\{\{(?<body>.*?)\}\})");
static QRegularExpression lambdaRegexp(R"(\$\{\{(?<body>.*?)\}\})");
lambdaRegexp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator it = lambdaRegexp.globalMatch(_code);
auto it = lambdaRegexp.globalMatch(_code);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString lambda = match.captured("body");
auto match = it.next();
auto lambda = match.captured("body");
// append
qCInfo(LOG_AW) << "Found lambda" << lambda;