Lua: Allow clearing an aspects MacroExpander

Adds a value "Null" to simpletypes to allow setting null values in
Tables:

Settings.BoolAspect.create({
  -- ...
  macroExpander = Null
})

Adds Macro.globalExpander() to retrieve the global expander.

Changes the result of MacroExpander.value() to (ok, value).

Change-Id: I2e2577b8d2f6acd447a3701f7ecaf835504768da
Reviewed-by: <lie@spyro-soft.com>
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-08-22 08:37:33 +02:00
parent 5553ce0833
commit e7e1aceb0e
7 changed files with 69 additions and 7 deletions

View File

@@ -12,12 +12,28 @@ void setupMacroModule()
registerProvider("Macro", [](sol::state_view lua) { registerProvider("Macro", [](sol::state_view lua) {
sol::table module = lua.create_table(); sol::table module = lua.create_table();
module.set_function("expand", [](const QString &s) -> QString { module.new_usertype<Utils::MacroExpander>(
"MacroExpander",
sol::no_constructor,
"expand",
[](Utils::MacroExpander *self, const QString &str) { return self->expand(str); },
"value",
[](Utils::MacroExpander *self, const QByteArray &str) {
bool found = false;
const QString res = self->value(str, &found);
return std::make_pair(found, res);
});
module.set_function("globalExpander", [] { return Utils::globalMacroExpander(); });
module.set_function("expand", [](const QString &s) {
return Utils::globalMacroExpander()->expand(s); return Utils::globalMacroExpander()->expand(s);
}); });
module.set_function("value", [](const QString &s) -> QString { module.set_function("value", [](const QString &s) {
return Utils::globalMacroExpander()->value(s.toUtf8()); bool found = false;
const QString res = Utils::globalMacroExpander()->value(s.toUtf8(), &found);
return std::make_pair(found, res);
}); });
return module; return module;

View File

@@ -112,7 +112,12 @@ void baseAspectCreate(BaseAspect *aspect, const std::string &key, const sol::obj
[func = value.as<sol::function>()] { void_safe_call(func); }); [func = value.as<sol::function>()] { void_safe_call(func); });
} else if (key == "enabler") } else if (key == "enabler")
aspect->setEnabler(value.as<BoolAspect *>()); aspect->setEnabler(value.as<BoolAspect *>());
else else if (key == "macroExpander") {
if (value.is<Null>())
aspect->setMacroExpander(nullptr);
else
aspect->setMacroExpander(value.as<MacroExpander *>());
} else
qWarning() << "Unknown key:" << key.c_str(); qWarning() << "Unknown key:" << key.c_str();
} }

View File

@@ -421,6 +421,11 @@ void setupLuaEngine(QObject *guard)
{ {
QTC_ASSERT(!d, return); QTC_ASSERT(!d, return);
d = new LuaInterfaceImpl(guard); d = new LuaInterfaceImpl(guard);
autoRegister([](sol::state_view lua) {
lua.new_usertype<Null>("NullType", sol::no_constructor);
lua.set("Null", Null{});
});
} }
} // namespace Lua } // namespace Lua

View File

@@ -109,4 +109,7 @@ sol::protected_function_result runFunction(
void setupLuaEngine(QObject *guard); void setupLuaEngine(QObject *guard);
class Null
{};
} // namespace Lua } // namespace Lua

View File

@@ -2,13 +2,34 @@
-- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
---@meta Macro ---@meta Macro
local macro = {} local macro = {}
---@class MacroExpander
local MacroExpander = {}
---Expands all variables in the given string.
---@param stringWithVariables string The string with variables to expand.
---@return string The expanded string.
function MacroExpander:expand(stringWithVariables) end
---Returns the value of the given variable.
---@param variableName string The name of the variable.
---@return boolean ok Whether the variable was found.
---@return string value The value of the variable.
function MacroExpander:value(variableName) end
---Returns the global Macro expander
---@return MacroExpander
function macro.globalExpander() end
---Returns globalExpander():value(variableName). ---Returns globalExpander():value(variableName).
---@param variableName string The name of the variable.
---@return boolean ok Whether the variable was found.
---@return string value The value of the variable.
function macro.value(variableName) end function macro.value(variableName) end
---Returns globalExpander():expand(stringWithVariables). ---Returns globalExpander():expand(stringWithVariables).
---@param stringWithVariables string The string with variables to expand.
---@return string The expanded string.
function macro.expand(stringWithVariables) end function macro.expand(stringWithVariables) end
return macro return macro

View File

@@ -1,6 +1,7 @@
---@meta Settings ---@meta Settings
---@module 'Qt' ---@module 'Qt'
---@module 'SimpleTypes'
local settings = {} local settings = {}
@@ -19,6 +20,7 @@ function settings.BaseAspect:apply() end
---@field enabler? BoolAspect Enable / Disable this aspect based on the state of the `enabler`. ---@field enabler? BoolAspect Enable / Disable this aspect based on the state of the `enabler`.
---@field onValueChanged? function () Called when the value of the aspect changes. ---@field onValueChanged? function () Called when the value of the aspect changes.
---@field onVolatileValueChanged? function () Called when the volatile value of the aspect changes. ---@field onVolatileValueChanged? function () Called when the volatile value of the aspect changes.
---@field macroExpander? MacroExpander|NullType The macro expander to use, or nil to disable macro expansion.
local AspectCreate = {} local AspectCreate = {}
---The base class of most typed aspects. ---The base class of most typed aspects.

View File

@@ -1,4 +1,4 @@
---@meta ---@meta SimpleTypes
---@class QRect ---@class QRect
---@field x integer The x position of the rectangle. ---@field x integer The x position of the rectangle.
@@ -28,9 +28,19 @@ QPointF = {}
---@field height number The height of the floating point size. ---@field height number The height of the floating point size.
QSizeF = {} QSizeF = {}
---@class QRectF ---@class QRectF A rectangle with floating point coordinates.
---@field x number The x position of the floating point rectangle. ---@field x number The x position of the floating point rectangle.
---@field y number The y position of the floating point rectangle. ---@field y number The y position of the floating point rectangle.
---@field width number The width of the floating point rectangle. ---@field width number The width of the floating point rectangle.
---@field height number The height of the floating point rectangle. ---@field height number The height of the floating point rectangle.
QRectF = {} QRectF = {}
---@class NullType
NullType = {}
---Just a workaround to let "Null" show the correct type in the documentation.
---@return NullType null
local function null() end
---A special object to represent a nullptr value.
Null = null()