2024-04-12 14:36:37 +02:00
|
|
|
// Copyright (C) 2024 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
|
|
#include "../luaengine.h"
|
|
|
|
|
|
2024-07-10 12:47:23 +02:00
|
|
|
#include "documents.h"
|
2024-04-12 14:36:37 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2024-07-10 12:47:23 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
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) {
|
2024-07-10 12:47:23 +02:00
|
|
|
auto connection
|
|
|
|
|
= lua.new_usertype<QMetaObject::Connection>("QMetaConnection", sol::no_constructor);
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
auto hook = lua.new_usertype<Hook>(
|
|
|
|
|
"Hook",
|
|
|
|
|
sol::no_constructor,
|
|
|
|
|
"connect",
|
2024-04-19 14:03:25 +02:00
|
|
|
[](Hook *hook, const sol::function &func) -> QMetaObject::Connection {
|
2024-04-12 14:36:37 +02:00
|
|
|
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); });
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-07 13:59:46 +02:00
|
|
|
LuaEngine::registerHook("editors.documentOpened", [](const sol::protected_function &func) {
|
|
|
|
|
QObject::connect(
|
|
|
|
|
Core::EditorManager::instance(),
|
|
|
|
|
&Core::EditorManager::documentOpened,
|
|
|
|
|
[func](Core::IDocument *document) {
|
2024-07-10 12:47:23 +02:00
|
|
|
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)));
|
|
|
|
|
}
|
2024-05-07 13:59:46 +02:00
|
|
|
});
|
2024-04-12 14:36:37 +02:00
|
|
|
});
|
2024-07-10 12:47:23 +02:00
|
|
|
|
2024-05-07 13:59:46 +02:00
|
|
|
LuaEngine::registerHook("editors.documentClosed", [](const sol::protected_function &func) {
|
|
|
|
|
QObject::connect(
|
|
|
|
|
Core::EditorManager::instance(),
|
|
|
|
|
&Core::EditorManager::documentClosed,
|
|
|
|
|
[func](Core::IDocument *document) {
|
2024-07-10 12:47:23 +02:00
|
|
|
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)));
|
|
|
|
|
}
|
2024-05-07 13:59:46 +02:00
|
|
|
});
|
2024-04-12 14:36:37 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
} // namespace Lua
|
|
|
|
|
|
|
|
|
|
#include "hook.moc"
|