2024-04-12 14:36:37 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
2024-05-02 14:33:21 +02:00
|
|
|
#include "luaengine.h"
|
2024-05-06 11:54:53 +02:00
|
|
|
#include "luapluginspec.h"
|
2024-06-13 14:50:37 +02:00
|
|
|
#include "luatr.h"
|
2024-04-12 14:36:37 +02:00
|
|
|
|
2024-05-16 12:56:07 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2024-06-13 14:50:37 +02:00
|
|
|
#include <coreplugin/ioutputpane.h>
|
2024-05-16 12:56:07 +02:00
|
|
|
#include <coreplugin/jsexpander.h>
|
2024-05-06 11:54:53 +02:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
#include <extensionsystem/iplugin.h>
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
2024-06-13 14:50:37 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
2024-07-30 10:30:11 +02:00
|
|
|
#include <utils/macroexpander.h>
|
2024-06-13 14:50:37 +02:00
|
|
|
#include <utils/qtcprocess.h>
|
|
|
|
#include <utils/theme/theme.h>
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
2024-06-25 07:14:05 +02:00
|
|
|
#include <QKeyEvent>
|
2024-06-13 14:50:37 +02:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QListView>
|
2024-09-27 12:20:58 +02:00
|
|
|
#include <QPainter>
|
2024-06-13 14:50:37 +02:00
|
|
|
#include <QStringListModel>
|
2024-06-25 09:19:15 +02:00
|
|
|
#include <QStyledItemDelegate>
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
using namespace Utils;
|
|
|
|
using namespace ExtensionSystem;
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
namespace Lua::Internal {
|
|
|
|
|
2024-07-26 13:31:32 +02:00
|
|
|
void setupActionModule();
|
|
|
|
void setupCoreModule();
|
|
|
|
void setupFetchModule();
|
|
|
|
void setupGuiModule();
|
|
|
|
void setupHookModule();
|
|
|
|
void setupInstallModule();
|
|
|
|
void setupJsonModule();
|
|
|
|
void setupLocalSocketModule();
|
2024-07-30 10:30:11 +02:00
|
|
|
void setupMacroModule();
|
2024-07-26 13:31:32 +02:00
|
|
|
void setupMessageManagerModule();
|
|
|
|
void setupProcessModule();
|
2024-09-18 14:08:36 +02:00
|
|
|
void setupProjectModule();
|
2024-07-26 13:31:32 +02:00
|
|
|
void setupQtModule();
|
|
|
|
void setupSettingsModule();
|
|
|
|
void setupTextEditorModule();
|
|
|
|
void setupTranslateModule();
|
|
|
|
void setupUtilsModule();
|
2024-04-12 14:36:37 +02:00
|
|
|
|
2024-07-30 10:30:11 +02:00
|
|
|
void setupLuaExpander(MacroExpander *expander);
|
|
|
|
|
2024-05-16 12:56:07 +02:00
|
|
|
class LuaJsExtension : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit LuaJsExtension(QObject *parent = nullptr)
|
|
|
|
: QObject(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Q_INVOKABLE QString metaFolder() const
|
|
|
|
{
|
|
|
|
return Core::ICore::resourcePath("lua/meta").toFSPathString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-25 09:19:15 +02:00
|
|
|
class ItemDelegate : public QStyledItemDelegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using QStyledItemDelegate::QStyledItemDelegate;
|
|
|
|
|
|
|
|
QWidget *createEditor(
|
|
|
|
QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
|
|
|
|
{
|
|
|
|
auto label = new QLabel(parent);
|
|
|
|
const QString text = index.data().toString();
|
2024-09-27 12:20:58 +02:00
|
|
|
label->setText(text.startsWith("__ERROR__") ? text.mid(9) : text);
|
2024-06-25 09:19:15 +02:00
|
|
|
label->setFont(option.font);
|
|
|
|
label->setTextInteractionFlags(
|
|
|
|
Qt::TextInteractionFlag::TextSelectableByMouse
|
|
|
|
| Qt::TextInteractionFlag::TextSelectableByKeyboard);
|
|
|
|
label->setAutoFillBackground(true);
|
|
|
|
label->setSelection(0, text.size());
|
|
|
|
return label;
|
|
|
|
}
|
2024-09-27 12:20:58 +02:00
|
|
|
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)
|
|
|
|
const override
|
|
|
|
{
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
bool isError = opt.text.startsWith("__ERROR__");
|
|
|
|
|
|
|
|
if (isError)
|
|
|
|
opt.text = opt.text.mid(9);
|
|
|
|
|
|
|
|
if (opt.state & QStyle::State_Selected) {
|
|
|
|
painter->fillRect(opt.rect, opt.palette.highlight());
|
|
|
|
painter->setPen(opt.palette.highlightedText().color());
|
|
|
|
} else if (isError) {
|
|
|
|
painter->setPen(creatorColor(Theme::Token_Notification_Danger));
|
|
|
|
} else {
|
|
|
|
painter->setPen(opt.palette.text().color());
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->drawText(opt.rect, opt.displayAlignment, opt.text);
|
|
|
|
}
|
2024-06-25 09:19:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class LuaReplView : public QListView
|
2024-06-13 14:50:37 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
std::unique_ptr<LuaState> m_luaState;
|
|
|
|
sol::function m_readCallback;
|
|
|
|
|
|
|
|
QStringListModel m_model;
|
|
|
|
|
|
|
|
public:
|
2024-06-25 09:19:15 +02:00
|
|
|
LuaReplView(QWidget *parent = nullptr)
|
2024-06-13 14:50:37 +02:00
|
|
|
: QListView(parent)
|
|
|
|
{
|
|
|
|
setModel(&m_model);
|
2024-09-26 09:41:05 +02:00
|
|
|
setItemDelegate(new ItemDelegate(this));
|
2024-06-13 14:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void showEvent(QShowEvent *) override
|
|
|
|
{
|
|
|
|
if (m_luaState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resetTerminal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleRequestResult(const QString &result)
|
|
|
|
{
|
|
|
|
auto cb = m_readCallback;
|
|
|
|
m_readCallback = {};
|
|
|
|
cb(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetTerminal()
|
|
|
|
{
|
|
|
|
m_model.setStringList({});
|
|
|
|
m_readCallback = {};
|
|
|
|
|
|
|
|
QFile f(":/lua/scripts/ilua.lua");
|
|
|
|
f.open(QIODevice::ReadOnly);
|
|
|
|
const auto ilua = QString::fromUtf8(f.readAll());
|
2024-07-26 14:49:40 +02:00
|
|
|
m_luaState = runScript(ilua, "ilua.lua", [this](sol::state &lua) {
|
2024-06-13 14:50:37 +02:00
|
|
|
lua["print"] = [this](sol::variadic_args va) {
|
2024-07-26 14:49:40 +02:00
|
|
|
const QString msgs = variadicToStringList(va).join("\t").replace("\r\n", "\n");
|
2024-06-25 09:19:15 +02:00
|
|
|
m_model.setStringList(m_model.stringList() << msgs);
|
2024-06-13 14:50:37 +02:00
|
|
|
scrollToBottom();
|
|
|
|
};
|
2024-09-27 12:20:58 +02:00
|
|
|
lua["LuaCopyright"] = LUA_COPYRIGHT;
|
2024-06-13 14:50:37 +02:00
|
|
|
|
|
|
|
sol::table async = lua.script("return require('async')", "_ilua_").get<sol::table>();
|
|
|
|
sol::function wrap = async["wrap"];
|
|
|
|
|
|
|
|
lua["readline_cb"] = [this](const QString &prompt, sol::function callback) {
|
|
|
|
scrollToBottom();
|
|
|
|
emit inputRequested(prompt);
|
|
|
|
m_readCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
lua["readline"] = wrap(lua["readline_cb"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
QListView::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void inputRequested(const QString &prompt);
|
|
|
|
};
|
|
|
|
|
|
|
|
class LineEdit : public FancyLineEdit
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using FancyLineEdit::FancyLineEdit;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LuaPane : public Core::IOutputPane
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QWidget *m_ui{nullptr};
|
2024-06-25 09:19:15 +02:00
|
|
|
LuaReplView *m_terminal{nullptr};
|
2024-06-13 14:50:37 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
LuaPane(QObject *parent = nullptr)
|
|
|
|
: Core::IOutputPane(parent)
|
|
|
|
{
|
|
|
|
setId("LuaPane");
|
|
|
|
setDisplayName(Tr::tr("Lua"));
|
2024-06-26 07:52:04 +02:00
|
|
|
setPriorityInStatusBar(-20);
|
2024-06-13 14:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *outputWidget(QWidget *parent) override
|
|
|
|
{
|
|
|
|
using namespace Layouting;
|
|
|
|
|
|
|
|
if (!m_ui && parent) {
|
2024-06-25 09:19:15 +02:00
|
|
|
m_terminal = new LuaReplView;
|
2024-06-13 14:50:37 +02:00
|
|
|
LineEdit *inputEdit = new LineEdit;
|
|
|
|
QLabel *prompt = new QLabel;
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
m_ui = Column {
|
2024-09-26 14:19:35 +02:00
|
|
|
noMargin,
|
|
|
|
spacing(0),
|
2024-06-13 14:50:37 +02:00
|
|
|
m_terminal,
|
|
|
|
Row { prompt, inputEdit },
|
|
|
|
}.emerge();
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
inputEdit->setReadOnly(true);
|
2024-08-06 09:10:34 +02:00
|
|
|
inputEdit->setHistoryCompleter(Utils::Key("LuaREPL.InputHistory"), false, 200);
|
2024-06-13 14:50:37 +02:00
|
|
|
|
|
|
|
connect(inputEdit, &QLineEdit::returnPressed, this, [this, inputEdit] {
|
|
|
|
inputEdit->setReadOnly(true);
|
|
|
|
m_terminal->handleRequestResult(inputEdit->text());
|
|
|
|
inputEdit->onEditingFinished();
|
|
|
|
inputEdit->clear();
|
|
|
|
});
|
|
|
|
connect(
|
|
|
|
m_terminal,
|
2024-06-25 09:19:15 +02:00
|
|
|
&LuaReplView::inputRequested,
|
2024-06-13 14:50:37 +02:00
|
|
|
this,
|
|
|
|
[prompt, inputEdit](const QString &p) {
|
|
|
|
prompt->setText(p);
|
|
|
|
inputEdit->setReadOnly(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visibilityChanged(bool) override {};
|
|
|
|
|
|
|
|
void clearContents() override
|
|
|
|
{
|
|
|
|
if (m_terminal)
|
|
|
|
m_terminal->resetTerminal();
|
|
|
|
}
|
|
|
|
void setFocus() override { outputWidget(nullptr)->setFocus(); }
|
|
|
|
bool hasFocus() const override { return true; }
|
|
|
|
bool canFocus() const override { return true; }
|
|
|
|
|
|
|
|
bool canNavigate() const override { return false; }
|
|
|
|
bool canNext() const override { return false; }
|
|
|
|
bool canPrevious() const override { return false; }
|
|
|
|
void goToNext() override {}
|
|
|
|
void goToPrev() override {}
|
|
|
|
|
|
|
|
QList<QWidget *> toolBarWidgets() const override { return {}; }
|
|
|
|
};
|
|
|
|
|
2024-05-06 11:54:53 +02:00
|
|
|
class LuaPlugin : public IPlugin
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Lua.json")
|
|
|
|
|
2024-05-02 14:33:21 +02:00
|
|
|
private:
|
2024-06-13 14:50:37 +02:00
|
|
|
LuaPane *m_pane = nullptr;
|
2024-05-02 14:33:21 +02:00
|
|
|
|
2024-04-12 14:36:37 +02:00
|
|
|
public:
|
2024-05-06 11:54:53 +02:00
|
|
|
LuaPlugin() {}
|
2024-04-12 14:36:37 +02:00
|
|
|
|
2024-06-13 11:23:22 +02:00
|
|
|
void initialize() final
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
2024-07-26 14:49:40 +02:00
|
|
|
setupLuaEngine(this);
|
2024-05-06 11:54:53 +02:00
|
|
|
|
2024-09-27 12:11:32 +02:00
|
|
|
registerProvider("async", ":/lua/scripts/async.lua");
|
|
|
|
registerProvider("inspect", ":/lua/scripts/inspect.lua");
|
|
|
|
|
2024-07-26 13:31:32 +02:00
|
|
|
setupActionModule();
|
|
|
|
setupCoreModule();
|
|
|
|
setupFetchModule();
|
|
|
|
setupGuiModule();
|
|
|
|
setupHookModule();
|
|
|
|
setupInstallModule();
|
|
|
|
setupJsonModule();
|
|
|
|
setupLocalSocketModule();
|
2024-07-30 10:30:11 +02:00
|
|
|
setupMacroModule();
|
2024-07-26 13:31:32 +02:00
|
|
|
setupMessageManagerModule();
|
|
|
|
setupProcessModule();
|
2024-09-18 14:08:36 +02:00
|
|
|
setupProjectModule();
|
2024-07-26 13:31:32 +02:00
|
|
|
setupQtModule();
|
|
|
|
setupSettingsModule();
|
|
|
|
setupTextEditorModule();
|
|
|
|
setupTranslateModule();
|
|
|
|
setupUtilsModule();
|
2024-05-16 12:56:07 +02:00
|
|
|
|
|
|
|
Core::JsExpander::registerGlobalObject("Lua", [] { return new LuaJsExtension(); });
|
2024-06-13 14:50:37 +02:00
|
|
|
|
2024-07-30 10:30:11 +02:00
|
|
|
setupLuaExpander(globalMacroExpander());
|
|
|
|
|
2024-07-25 14:46:14 +02:00
|
|
|
pluginSpecsFromArchiveFactories().push_back([](const FilePath &path) {
|
|
|
|
QList<PluginSpec *> plugins;
|
|
|
|
auto dirs = path.dirEntries(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
for (const auto &dir : dirs) {
|
|
|
|
const auto specFilePath = dir / (dir.fileName() + ".lua");
|
|
|
|
if (specFilePath.exists()) {
|
|
|
|
Utils::expected_str<PluginSpec *> spec = loadPlugin(specFilePath);
|
|
|
|
QTC_CHECK_EXPECTED(spec);
|
|
|
|
if (spec)
|
|
|
|
plugins.push_back(*spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return plugins;
|
|
|
|
});
|
|
|
|
|
2024-06-25 09:19:15 +02:00
|
|
|
m_pane = new LuaPane(this);
|
2024-04-12 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool delayedInitialize() final
|
|
|
|
{
|
2024-06-13 10:52:43 +02:00
|
|
|
scanForPlugins(PluginManager::pluginPaths());
|
2024-04-12 14:36:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-05-06 11:54:53 +02:00
|
|
|
|
2024-06-13 11:23:22 +02:00
|
|
|
void scanForPlugins(const FilePaths &pluginPaths)
|
2024-05-06 11:54:53 +02:00
|
|
|
{
|
|
|
|
QSet<PluginSpec *> plugins;
|
2024-06-13 11:23:22 +02:00
|
|
|
for (const FilePath &path : pluginPaths) {
|
2024-05-30 14:32:34 +02:00
|
|
|
FilePaths folders = path.dirEntries(FileFilter({}, QDir::Dirs | QDir::NoDotAndDotDot));
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
for (const FilePath &folder : folders) {
|
|
|
|
const FilePath script = folder / (folder.baseName() + ".lua");
|
2024-06-13 11:23:22 +02:00
|
|
|
if (!script.exists())
|
|
|
|
continue;
|
|
|
|
|
2024-07-26 14:49:40 +02:00
|
|
|
const expected_str<LuaPluginSpec *> result = loadPlugin(script);
|
2024-05-06 11:54:53 +02:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
qWarning() << "Failed to load plugin" << script << ":" << result.error();
|
2024-07-05 08:25:28 +02:00
|
|
|
MessageManager::writeFlashing(Tr::tr("Failed to load plugin %1: %2")
|
2024-05-06 11:54:53 +02:00
|
|
|
.arg(script.toUserOutput())
|
|
|
|
.arg(result.error()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins.insert(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginManager::addPlugins({plugins.begin(), plugins.end()});
|
|
|
|
PluginManager::loadPluginsAtRuntime(plugins);
|
|
|
|
}
|
2024-04-12 14:36:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Lua::Internal
|
|
|
|
|
|
|
|
#include "luaplugin.moc"
|