1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "luaengine.h"
#include "luapluginspec.h"
#include "luatr.h"
#include <coreplugin/icore.h>
#include <coreplugin/ioutputpane.h>
#include <coreplugin/jsexpander.h>
#include <coreplugin/messagemanager.h>
#include <extensionsystem/iplugin.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>
#include <utils/macroexpander.h>
#include <utils/qtcprocess.h>
#include <utils/theme/theme.h>
#include <QDebug>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QListView>
#include <QStringListModel>
#include <QStyledItemDelegate>
using namespace Core;
using namespace Utils;
using namespace ExtensionSystem;
namespace Lua::Internal {
void setupActionModule();
void setupCoreModule();
void setupFetchModule();
void setupGuiModule();
void setupHookModule();
void setupInstallModule();
void setupJsonModule();
void setupLocalSocketModule();
void setupMacroModule();
void setupMessageManagerModule();
void setupProcessModule();
void setupProjectModule();
void setupQtModule();
void setupSettingsModule();
void setupTextEditorModule();
void setupTranslateModule();
void setupUtilsModule();
void setupLuaExpander(MacroExpander *expander);
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();
}
};
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();
label->setText(text);
label->setFont(option.font);
label->setTextInteractionFlags(
Qt::TextInteractionFlag::TextSelectableByMouse
| Qt::TextInteractionFlag::TextSelectableByKeyboard);
label->setAutoFillBackground(true);
label->setSelection(0, text.size());
return label;
}
};
class LuaReplView : public QListView
{
Q_OBJECT
std::unique_ptr<LuaState> m_luaState;
sol::function m_readCallback;
QStringListModel m_model;
public:
LuaReplView(QWidget *parent = nullptr)
: QListView(parent)
{
setModel(&m_model);
setItemDelegate(new ItemDelegate(this));
}
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());
m_luaState = runScript(ilua, "ilua.lua", [this](sol::state &lua) {
lua["print"] = [this](sol::variadic_args va) {
const QString msgs = variadicToStringList(va).join("\t").replace("\r\n", "\n");
m_model.setStringList(m_model.stringList() << msgs);
scrollToBottom();
};
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};
LuaReplView *m_terminal{nullptr};
public:
LuaPane(QObject *parent = nullptr)
: Core::IOutputPane(parent)
{
setId("LuaPane");
setDisplayName(Tr::tr("Lua"));
setPriorityInStatusBar(-20);
}
QWidget *outputWidget(QWidget *parent) override
{
using namespace Layouting;
if (!m_ui && parent) {
m_terminal = new LuaReplView;
LineEdit *inputEdit = new LineEdit;
QLabel *prompt = new QLabel;
// clang-format off
m_ui = Column {
noMargin,
spacing(0),
m_terminal,
Row { prompt, inputEdit },
}.emerge();
// clang-format on
inputEdit->setReadOnly(true);
inputEdit->setHistoryCompleter(Utils::Key("LuaREPL.InputHistory"), false, 200);
connect(inputEdit, &QLineEdit::returnPressed, this, [this, inputEdit] {
inputEdit->setReadOnly(true);
m_terminal->handleRequestResult(inputEdit->text());
inputEdit->onEditingFinished();
inputEdit->clear();
});
connect(
m_terminal,
&LuaReplView::inputRequested,
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 {}; }
};
class LuaPlugin : public IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Lua.json")
private:
LuaPane *m_pane = nullptr;
public:
LuaPlugin() {}
void initialize() final
{
setupLuaEngine(this);
registerProvider("async", ":/lua/scripts/async.lua");
registerProvider("inspect", ":/lua/scripts/inspect.lua");
setupActionModule();
setupCoreModule();
setupFetchModule();
setupGuiModule();
setupHookModule();
setupInstallModule();
setupJsonModule();
setupLocalSocketModule();
setupMacroModule();
setupMessageManagerModule();
setupProcessModule();
setupProjectModule();
setupQtModule();
setupSettingsModule();
setupTextEditorModule();
setupTranslateModule();
setupUtilsModule();
Core::JsExpander::registerGlobalObject("Lua", [] { return new LuaJsExtension(); });
setupLuaExpander(globalMacroExpander());
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;
});
m_pane = new LuaPane(this);
}
bool delayedInitialize() final
{
scanForPlugins(PluginManager::pluginPaths());
return true;
}
void scanForPlugins(const FilePaths &pluginPaths)
{
QSet<PluginSpec *> plugins;
for (const FilePath &path : pluginPaths) {
FilePaths folders = path.dirEntries(FileFilter({}, QDir::Dirs | QDir::NoDotAndDotDot));
for (const FilePath &folder : folders) {
const FilePath script = folder / (folder.baseName() + ".lua");
if (!script.exists())
continue;
const expected_str<LuaPluginSpec *> result = loadPlugin(script);
if (!result) {
qWarning() << "Failed to load plugin" << script << ":" << result.error();
MessageManager::writeFlashing(Tr::tr("Failed to load plugin %1: %2")
.arg(script.toUserOutput())
.arg(result.error()));
continue;
}
plugins.insert(*result);
}
}
PluginManager::addPlugins({plugins.begin(), plugins.end()});
PluginManager::loadPluginsAtRuntime(plugins);
}
};
} // namespace Lua::Internal
#include "luaplugin.moc"
|