Lua: Cleanup Hooks

Change-Id: I8e3a1d66ea6125d2cc5ee9c8f8442cca0913fec6
Reviewed-by: <lie@spyro-soft.com>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-07-16 16:07:57 +02:00
parent 0e75f3f4f3
commit d2e14ffe86
12 changed files with 181 additions and 190 deletions

View File

@@ -3,61 +3,22 @@
#include "../luaengine.h"
#include "documents.h"
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/textdocument.h>
namespace Lua {
class Hook : public QObject
{
Q_OBJECT
public:
Hook(QObject *source);
signals:
void trigger(sol::table &args);
};
Hook::Hook(QObject *source)
: QObject(source)
{}
namespace Internal {
void addHookModule()
{
LuaEngine::autoRegister([](sol::state_view lua) {
auto connection
= lua.new_usertype<QMetaObject::Connection>("QMetaConnection", sol::no_constructor);
auto hook = lua.new_usertype<Hook>(
"Hook",
sol::no_constructor,
"connect",
[](Hook *hook, const sol::function &func) -> QMetaObject::Connection {
QMetaObject::Connection con
= QObject::connect(hook, &Hook::trigger, [func](sol::table args) {
auto res = LuaEngine::void_safe_call(func, args);
QTC_CHECK_EXPECTED(res);
});
return con;
},
"disconnect",
[](Hook *, QMetaObject::Connection con) { QObject::disconnect(con); });
});
LuaEngine::registerHook("editors.documentOpened", [](const sol::protected_function &func) {
QObject::connect(
Core::EditorManager::instance(),
&Core::EditorManager::documentOpened,
[func](Core::IDocument *document) {
auto text_document = qobject_cast<TextEditor::TextDocument *>(document);
if (text_document) {
QTC_CHECK_EXPECTED(LuaEngine::void_safe_call(
func, std::make_unique<LuaTextDocument>(text_document)));
}
Utils::expected_str<void> res = LuaEngine::void_safe_call(func, document);
QTC_CHECK_EXPECTED(res);
});
});
@@ -66,11 +27,8 @@ void addHookModule()
Core::EditorManager::instance(),
&Core::EditorManager::documentClosed,
[func](Core::IDocument *document) {
auto text_document = qobject_cast<TextEditor::TextDocument *>(document);
if (text_document) {
QTC_CHECK_EXPECTED(LuaEngine::void_safe_call(
func, std::make_unique<LuaTextDocument>(text_document)));
}
Utils::expected_str<void> res = LuaEngine::void_safe_call(func, document);
QTC_CHECK_EXPECTED(res);
});
});
}
@@ -78,5 +36,3 @@ void addHookModule()
} // namespace Internal
} // namespace Lua
#include "hook.moc"