From e7e1aceb0ea897b517307f348075a3c05caef469 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 22 Aug 2024 08:37:33 +0200 Subject: [PATCH] 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: Reviewed-by: Cristian Adam --- src/plugins/lua/bindings/macro.cpp | 22 +++++++++++++++++++--- src/plugins/lua/bindings/settings.cpp | 7 ++++++- src/plugins/lua/luaengine.cpp | 5 +++++ src/plugins/lua/luaengine.h | 3 +++ src/plugins/lua/meta/macro.lua | 23 ++++++++++++++++++++++- src/plugins/lua/meta/settings.lua | 2 ++ src/plugins/lua/meta/simpletypes.lua | 14 ++++++++++++-- 7 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/plugins/lua/bindings/macro.cpp b/src/plugins/lua/bindings/macro.cpp index 04cc5095d7d..e0f36486d81 100644 --- a/src/plugins/lua/bindings/macro.cpp +++ b/src/plugins/lua/bindings/macro.cpp @@ -12,12 +12,28 @@ void setupMacroModule() registerProvider("Macro", [](sol::state_view lua) { sol::table module = lua.create_table(); - module.set_function("expand", [](const QString &s) -> QString { + module.new_usertype( + "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); }); - module.set_function("value", [](const QString &s) -> QString { - return Utils::globalMacroExpander()->value(s.toUtf8()); + module.set_function("value", [](const QString &s) { + bool found = false; + const QString res = Utils::globalMacroExpander()->value(s.toUtf8(), &found); + return std::make_pair(found, res); }); return module; diff --git a/src/plugins/lua/bindings/settings.cpp b/src/plugins/lua/bindings/settings.cpp index 36104215a8e..92501076056 100644 --- a/src/plugins/lua/bindings/settings.cpp +++ b/src/plugins/lua/bindings/settings.cpp @@ -112,7 +112,12 @@ void baseAspectCreate(BaseAspect *aspect, const std::string &key, const sol::obj [func = value.as()] { void_safe_call(func); }); } else if (key == "enabler") aspect->setEnabler(value.as()); - else + else if (key == "macroExpander") { + if (value.is()) + aspect->setMacroExpander(nullptr); + else + aspect->setMacroExpander(value.as()); + } else qWarning() << "Unknown key:" << key.c_str(); } diff --git a/src/plugins/lua/luaengine.cpp b/src/plugins/lua/luaengine.cpp index 359f68309ad..f3966a40eae 100644 --- a/src/plugins/lua/luaengine.cpp +++ b/src/plugins/lua/luaengine.cpp @@ -421,6 +421,11 @@ void setupLuaEngine(QObject *guard) { QTC_ASSERT(!d, return); d = new LuaInterfaceImpl(guard); + + autoRegister([](sol::state_view lua) { + lua.new_usertype("NullType", sol::no_constructor); + lua.set("Null", Null{}); + }); } } // namespace Lua diff --git a/src/plugins/lua/luaengine.h b/src/plugins/lua/luaengine.h index 2be08f5ed0e..22dad891fdb 100644 --- a/src/plugins/lua/luaengine.h +++ b/src/plugins/lua/luaengine.h @@ -109,4 +109,7 @@ sol::protected_function_result runFunction( void setupLuaEngine(QObject *guard); +class Null +{}; + } // namespace Lua diff --git a/src/plugins/lua/meta/macro.lua b/src/plugins/lua/meta/macro.lua index b99877cd3de..a61618f6f7b 100644 --- a/src/plugins/lua/meta/macro.lua +++ b/src/plugins/lua/meta/macro.lua @@ -2,13 +2,34 @@ -- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 ---@meta 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). +---@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 ---Returns globalExpander():expand(stringWithVariables). +---@param stringWithVariables string The string with variables to expand. +---@return string The expanded string. function macro.expand(stringWithVariables) end return macro diff --git a/src/plugins/lua/meta/settings.lua b/src/plugins/lua/meta/settings.lua index 97f60d53b7e..1821b338262 100644 --- a/src/plugins/lua/meta/settings.lua +++ b/src/plugins/lua/meta/settings.lua @@ -1,6 +1,7 @@ ---@meta Settings ---@module 'Qt' +---@module 'SimpleTypes' 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 onValueChanged? function () Called when the 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 = {} ---The base class of most typed aspects. diff --git a/src/plugins/lua/meta/simpletypes.lua b/src/plugins/lua/meta/simpletypes.lua index 5071efa9119..8009f5afc11 100644 --- a/src/plugins/lua/meta/simpletypes.lua +++ b/src/plugins/lua/meta/simpletypes.lua @@ -1,4 +1,4 @@ ----@meta +---@meta SimpleTypes ---@class QRect ---@field x integer The x position of the rectangle. @@ -28,9 +28,19 @@ QPointF = {} ---@field height number The height of the floating point size. QSizeF = {} ----@class QRectF +---@class QRectF A rectangle with floating point coordinates. ---@field x number The x 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 height number The height of the floating point rectangle. 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()