aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp
blob: ab46d4ea9f244ef1c57ac731903c6e89942d8875 (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
// 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 <coreplugin/editormanager/editormanager.h>

#include <languageclient/languageclientinterface.h>
#include <languageclient/languageclientmanager.h>
#include <languageclient/languageclientsettings.h>
#include <languageclient/languageclienttr.h>

#include <lua/bindings/inheritance.h>
#include <lua/luaengine.h>

#include <lua/bindings/async.h>

#include <extensionsystem/iplugin.h>
#include <extensionsystem/pluginmanager.h>

#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>

#include <utils/commandline.h>
#include <utils/guardedcallback.h>
#include <utils/layoutbuilder.h>

#include <QJsonDocument>

using namespace Utils;
using namespace Core;
using namespace TextEditor;
using namespace ProjectExplorer;
using namespace std::string_view_literals;

namespace {

class RequestWithResponse : public LanguageServerProtocol::JsonRpcMessage
{
    sol::main_function m_callback;
    LanguageServerProtocol::MessageId m_id;

public:
    RequestWithResponse(const QJsonObject &obj, const sol::function &cb)
        : LanguageServerProtocol::JsonRpcMessage(obj)
        , m_callback(cb)
    {
        m_id = LanguageServerProtocol::MessageId(obj["id"]);
    }

    std::optional<LanguageServerProtocol::ResponseHandler> responseHandler() const override
    {
        if (!m_id.isValid()) {
            qWarning() << "Invalid 'id' in request:" << toJsonObject();
            return std::nullopt;
        }

        return LanguageServerProtocol::ResponseHandler{
            m_id, [callback = m_callback](const JsonRpcMessage &msg) {
                if (!callback.valid()) {
                    qWarning() << "Invalid Lua callback";
                    return;
                }

                auto result = ::Lua::void_safe_call(
                    callback, ::Lua::toTable(callback.lua_state(), msg.toJsonObject()));
                QTC_CHECK_RESULT(result);
            }};
    }
};

} // anonymous namespace

namespace LanguageClient::Lua {

static void registerLuaApi();

class LuaClient : public LanguageClient::Client
{
    Q_OBJECT

public:
    Utils::Id m_settingsId;

    LuaClient(BaseClientInterface *interface, Utils::Id settingsId)
        : LanguageClient::Client(interface)
        , m_settingsId(settingsId)
    {}
};

class LuaLanguageClientPlugin final : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "LuaLanguageClient.json")

public:
    LuaLanguageClientPlugin() {}

private:
    void initialize() final { registerLuaApi(); }
};

class LuaLocalSocketClientInterface : public LocalSocketClientInterface
{
public:
    LuaLocalSocketClientInterface(const CommandLine &cmd, const QString &serverName)
        : LocalSocketClientInterface(serverName)
        , m_cmd(cmd)
        , m_logFile("lua-lspclient.XXXXXX.log")

    {}

    void startImpl() override
    {
        if (m_process) {
            QTC_CHECK(!m_process->isRunning());
            delete m_process;
        }
        m_process = new Process;
        m_process->setProcessMode(ProcessMode::Writer);
        connect(
            m_process,
            &Process::readyReadStandardError,
            this,
            &LuaLocalSocketClientInterface::readError);
        connect(
            m_process,
            &Process::readyReadStandardOutput,
            this,
            &LuaLocalSocketClientInterface::readOutput);
        connect(m_process, &Process::started, this, [this]() {
            this->LocalSocketClientInterface::startImpl();
            emit started();
        });
        connect(m_process, &Process::done, this, [this] {
            if (m_process->result() != ProcessResult::FinishedWithSuccess)
                emit error(QString("%1 (see logs in \"%2\")")
                               .arg(m_process->exitMessage())
                               .arg(m_logFile.fileName()));
            emit finished();
        });
        m_logFile.write(
            QString("Starting server: %1\nOutput:\n\n").arg(m_cmd.toUserOutput()).toUtf8());
        m_process->setCommand(m_cmd);
        m_process->setWorkingDirectory(m_workingDirectory);
        if (m_env.hasChanges())
            m_process->setEnvironment(m_env);
        m_process->start();
    }

    void setWorkingDirectory(const FilePath &workingDirectory)
    {
        m_workingDirectory = workingDirectory;
    }

    FilePath serverDeviceTemplate() const override { return m_cmd.executable(); }

    void readError()
    {
        QTC_ASSERT(m_process, return);

        const QByteArray stdErr = m_process->readAllRawStandardError();
        m_logFile.write(stdErr);
    }

    void readOutput()
    {
        QTC_ASSERT(m_process, return);
        const QByteArray &out = m_process->readAllRawStandardOutput();
        parseData(out);
    }

private:
    Utils::CommandLine m_cmd;
    Utils::FilePath m_workingDirectory;
    Utils::Process *m_process = nullptr;
    Utils::Environment m_env;
    QTemporaryFile m_logFile;
};

class LuaClientWrapper;

class LuaClientSettings : public BaseSettings
{
public:
    LuaClientSettings() {}
    LuaClientSettings(const std::weak_ptr<LuaClientWrapper> &wrapper);
    ~LuaClientSettings() override = default;

    bool applyFromSettingsWidget(QWidget *widget) override;

    void toMap(Utils::Store &map) const override;
    void fromMap(const Utils::Store &map) override;

    QWidget *createSettingsWidget(QWidget *parent = nullptr) const override;

    BaseSettings *create() const override { return new LuaClientSettings; }
    BaseSettings *copy() const override;

protected:
    Client *createClient(BaseClientInterface *interface) const final;

    BaseClientInterface *createInterface(ProjectExplorer::BuildConfiguration *bc) const override;

private:
    std::weak_ptr<LuaClientWrapper> m_wrapper;
    QObject guard;
};

enum class TransportType { StdIO, LocalSocket };

class LuaClientWrapper : public QObject
{
    Q_OBJECT
public:
    TransportType m_transportType{TransportType::StdIO};
    std::function<Result<>(CommandLine &)> m_cmdLineCallback;
    std::function<Result<>(QString &)> m_initOptionsCallback;
    sol::main_function m_asyncInitOptions;
    bool m_isUpdatingAsyncOptions{false};
    AspectContainer *m_aspects{nullptr};
    QString m_name;
    Utils::Id m_settingsTypeId;
    QString m_clientSettingsId;
    QString m_initializationOptions;
    CommandLine m_cmdLine;
    QString m_serverName;
    LanguageFilter m_languageFilter;
    bool m_showInSettings;
    bool m_activatable;
    BaseSettings::StartBehavior m_startBehavior = BaseSettings::RequiresFile;

    std::optional<sol::protected_function> m_onInstanceStart;
    std::optional<sol::protected_function> m_startFailedCallback;
    QMap<QString, sol::protected_function> m_messageCallbacks;

public:
    static BaseSettings::StartBehavior startBehaviorFromString(const QString &str)
    {
        if (str == "RequiresProject")
            return BaseSettings::RequiresProject;
        if (str == "RequiresFile")
            return BaseSettings::RequiresFile;
        if (str == "AlwaysOn")
            return BaseSettings::AlwaysOn;

        throw sol::error("Unknown start behavior: " + str.toStdString());
    }

    LuaClientWrapper(const sol::table &options)
    {
        m_cmdLineCallback = addValue<CommandLine>(
            options,
            "cmd",
            m_cmdLine,
            [](const sol::protected_function_result &res) -> Result<CommandLine> {
                if (res.get_type(0) != sol::type::table)
                    return make_unexpected(QString("cmd callback did not return a table"));
                return cmdFromTable(res.get<sol::table>());
            });

        m_initOptionsCallback = addValue<QString>(
            options,
            "initializationOptions",
            m_initializationOptions,
            [](const sol::protected_function_result &res) -> Result<QString> {
                if (res.get_type(0) == sol::type::table)
                    return ::Lua::toJsonString(res.get<sol::table>());
                else if (res.get_type(0) == sol::type::string)
                    return res.get<QString>(0);
                return make_unexpected(QString("init callback did not return a table or string"));
            });

        if (auto initOptionsTable = options.get<sol::optional<sol::table>>(
                "initializationOptions"sv))
            m_initializationOptions = ::Lua::toJsonString(*initOptionsTable);
        else if (auto initOptionsString = options.get<sol::optional<QString>>("initializationOptions"sv))
            m_initializationOptions = *initOptionsString;

        m_name = options.get<QString>("name"sv);
        m_settingsTypeId = Utils::Id::fromString(QString("Lua_%1").arg(m_name));
        m_serverName = options.get_or<QString>("serverName"sv, "");

        m_startBehavior = startBehaviorFromString(
            options.get_or<QString>("startBehavior"sv, "AlwaysOn"));

        m_startFailedCallback = options.get<sol::protected_function>("onStartFailed"sv);

        QString transportType = options.get_or<QString>("transport"sv, "stdio");
        if (transportType == "stdio")
            m_transportType = TransportType::StdIO;
        else if (transportType == "localsocket")
            m_transportType = TransportType::LocalSocket;
        else
            qWarning() << "Unknown transport type:" << transportType;

        auto languageFilter = options.get<std::optional<sol::table>>("languageFilter"sv);
        if (languageFilter) {
            auto patterns = languageFilter->get<std::optional<sol::table>>("patterns");
            auto mimeTypes = languageFilter->get<std::optional<sol::table>>("mimeTypes");

            if (patterns)
                for (auto [_, v] : *patterns)
                    m_languageFilter.filePattern.push_back(v.as<QString>());

            if (mimeTypes)
                for (auto [_, v] : *mimeTypes)
                    m_languageFilter.mimeTypes.push_back(v.as<QString>());
        }

        m_showInSettings = options.get<std::optional<bool>>("showInSettings"sv).value_or(true);
        m_activatable = options.get<std::optional<bool>>("activatable"sv).value_or(true);

        // get<sol::optional<>> because on MSVC, get_or(..., nullptr) fails to compile
        m_aspects = options.get<sol::optional<AspectContainer *>>("settings"sv).value_or(nullptr);

        if (m_aspects) {
            connect(m_aspects, &AspectContainer::applied, this, [this] {
                updateOptions();
                auto settings = Utils::findOr(
                    LanguageClientManager::currentSettings(), nullptr, [this](BaseSettings *s) {
                        return s->m_id == m_clientSettingsId;
                    });

                if (settings) {
                    LanguageClientManager::applySettings(settings);
                    LanguageClientManager::writeSettings();
                } else {
                    LanguageClientManager::applySettings();
                }
            });
        }

        connect(
            LanguageClientManager::instance(),
            &LanguageClientManager::clientInitialized,
            this,
            [this](Client *c) {
                auto luaClient = qobject_cast<LuaClient *>(c);
                if (luaClient && luaClient->m_settingsId == m_settingsTypeId && m_onInstanceStart) {
                    QTC_CHECK(::Lua::void_safe_call(*m_onInstanceStart, c));
                    updateMessageCallbacks();
                }
            });

        connect(
            LanguageClientManager::instance(),
            &LanguageClientManager::clientRemoved,
            this,
            &LuaClientWrapper::onClientRemoved);

        if (auto asyncInit = options.get<sol::optional<sol::main_function>>(
                "initializationOptionsAsync")) {
            m_asyncInitOptions = *asyncInit;
            QMetaObject::invokeMethod(
                this, &LuaClientWrapper::updateAsyncOptions, Qt::QueuedConnection);
        }
    }

    void onClientRemoved(Client *c, bool unexpected)
    {
        auto luaClient = qobject_cast<LuaClient *>(c);
        if (!luaClient || luaClient->m_settingsId != m_settingsTypeId)
            return;

        if (unexpected && m_startFailedCallback) {
            QTC_CHECK_RESULT(::Lua::void_safe_call(*m_startFailedCallback));
        }
    }

    TransportType transportType() { return m_transportType; }

    void applySettings()
    {
        if (m_aspects)
            m_aspects->apply();

        updateOptions();
    }

    void fromMap(const Utils::Store &map)
    {
        if (m_aspects)
            m_aspects->fromMap(map);
        updateOptions();
    }

    void toMap(Utils::Store &map) const
    {
        if (m_aspects)
            m_aspects->toMap(map);
    }

    Layouting::LayoutModifier settingsLayout()
    {
        if (m_aspects)
            return [this](Layouting::Layout *iface) { m_aspects->addToLayoutImpl(*iface); };
        return {};
    }

    void registerMessageCallback(const QString &msg, const sol::main_function &callback)
    {
        if (m_messageCallbacks.contains(msg))
            qWarning() << "Overwriting existing callback for message:" << msg;

        m_messageCallbacks.insert(msg, callback);
        updateMessageCallbacks();
    }

    void updateMessageCallbacks()
    {
        for (Client *c : LanguageClientManager::clientsForSettingId(m_clientSettingsId)) {
            if (!c)
                continue;
            for (const auto &[msg, func] : m_messageCallbacks.asKeyValueRange()) {
                c->registerCustomMethod(
                    msg,
                    [self = QPointer<LuaClientWrapper>(this),
                     name = msg](const LanguageServerProtocol::JsonRpcMessage &m) {
                        if (!self)
                            return false;

                        auto func = self->m_messageCallbacks.value(name);
                        auto table = ::Lua::toTable(func.lua_state(), m.toJsonObject());
                        auto result = func.call(table);
                        if (!result.valid()) {
                            qWarning() << "Error calling message callback for:" << name << ":"
                                       << (result.get<sol::error>().what());
                            return false;
                        }
                        if (result.get_type() != sol::type::boolean) {
                            qWarning() << "Callback for:" << name << " did not return a boolean";
                            return false;
                        }

                        return result.get<bool>();
                    });
            }
        }
    }

    void sendMessage(const sol::table &message)
    {
        const QJsonValue messageValue = ::Lua::toJson(message);
        if (!messageValue.isObject())
            throw sol::error("Message is not an object");

        const LanguageServerProtocol::JsonRpcMessage request(messageValue.toObject());
        for (Client *c : LanguageClientManager::clientsForSettingId(m_clientSettingsId)) {
            if (c)
                c->sendMessage(request);
        }
    }

    QList<Client *> clientsForDocument(Core::IDocument *document)
    {
        QList<Client *> result;
        if (m_startBehavior == BaseSettings::RequiresProject) {
            Project *project = ProjectManager::projectForFile(document->filePath());
            const auto clients = LanguageClientManager::clientsForSettingId(m_clientSettingsId);
            result = Utils::filtered(clients, [project](Client *c) {
                return c && c->project() && c->project() == project;
            });
        }
        else
            result = LanguageClientManager::clientsForSettingId(m_clientSettingsId);

        return Utils::filtered(result, [](Client *c) { return c->reachable(); });
    }

    void sendMessageForDocument(Core::IDocument *document, const sol::table &message)
    {
        const QJsonValue messageValue = ::Lua::toJson(message);
        if (!messageValue.isObject())
            throw sol::error("Message is not an object");

        const LanguageServerProtocol::JsonRpcMessage request(messageValue.toObject());

        const QList<Client *> clients = clientsForDocument(document);
        QTC_CHECK(clients.size() == 1);

        for (Client *c : clients) {
            if (c)
                c->sendMessage(request);
        }
    }

    QString sendMessageWithIdForDocument_cb(
        TextEditor::TextDocument *document, const sol::table &message, const sol::main_function callback)
    {
        const QJsonValue messageValue = ::Lua::toJson(message);
        if (!messageValue.isObject())
            throw sol::error("Message is not an object");

        QJsonObject obj = messageValue.toObject();
        const auto id = QUuid::createUuid().toString();
        obj["id"] = id;

        const RequestWithResponse request{obj, callback};

        auto clients = clientsForDocument(document);

        QTC_ASSERT(clients.size() != 0, throw sol::error("No client for document found"));
        QTC_ASSERT(clients.size() == 1, throw sol::error("Multiple clients for document found"));
        QTC_ASSERT(clients.front(), throw sol::error("Client is null"));

        clients.front()->sendMessage(request);
        return id;
    }

    void cancelRequest(const QString &id)
    {
        for (Client *c : LanguageClientManager::clientsForSettingId(m_clientSettingsId)) {
            if (c)
                c->cancelRequest(LanguageServerProtocol::MessageId(id));
        }
    }

    void updateAsyncOptions()
    {
        if (m_isUpdatingAsyncOptions)
            return;
        QTC_ASSERT(m_asyncInitOptions, return);
        m_isUpdatingAsyncOptions = true;
        std::function<void(sol::object)> cb = guardedCallback(this, [this](sol::object options) {
            if (options.is<sol::table>())
                m_initializationOptions = ::Lua::toJsonString(options.as<sol::table>());
            else if (options.is<QString>())
                m_initializationOptions = options.as<QString>();

            emit optionsChanged();
            m_isUpdatingAsyncOptions = false;
        });

        ::Lua::Async::start<sol::object>(m_asyncInitOptions, cb);
    }

    void updateOptions()
    {
        if (m_cmdLineCallback) {
            auto result = m_cmdLineCallback(m_cmdLine);
            if (!result)
                qWarning() << "Error applying option callback:" << result.error();
        }
        if (m_initOptionsCallback) {
            Result<> result = m_initOptionsCallback(m_initializationOptions);
            if (!result)
                qWarning() << "Error applying init option callback:" << result.error();

            // Right now there is only one option that needs to be mirrored to the LSP Settings,
            // so we only emit optionsChanged() here. If another setting should need to be dynamic
            // optionsChanged() needs to be called for it as well, but only once per updateOptions()
            emit optionsChanged();
        }
        if (m_asyncInitOptions)
            updateAsyncOptions();
    }

    static CommandLine cmdFromTable(const sol::table &tbl)
    {
        CommandLine cmdLine;
        cmdLine.setExecutable(FilePath::fromUserInput(tbl.get<QString>(1)));

        for (size_t i = 2; i < tbl.size() + 1; i++)
            cmdLine.addArg(tbl.get<QString>(i));

        return cmdLine;
    }

    template<typename T>
    std::function<Result<>(T &)> addValue(
        const sol::table &options,
        const char *fieldName,
        T &dest,
        std::function<Result<T>(const sol::protected_function_result &)> transform)
    {
        auto fixed = options.get<sol::optional<sol::table>>(fieldName);
        auto cb = options.get<sol::optional<sol::protected_function>>(fieldName);

        if (fixed) {
            dest = fixed.value().get<T>(1);
        } else if (cb) {
            std::function<Result<>(T &)> callback =
                [cb, transform](T &dest) -> Result<> {
                auto res = cb.value().call();
                if (!res.valid()) {
                    sol::error err = res;
                    return Utils::make_unexpected(QString::fromLocal8Bit(err.what()));
                }

                Result<T> trResult = transform(res);
                if (!trResult)
                    return make_unexpected(trResult.error());

                dest = *trResult;
                return {};
            };

            QTC_CHECK_RESULT(callback(dest));
            return callback;
        }
        return {};
    }

    BaseClientInterface *createInterface(BuildConfiguration *bc)
    {
        if (m_transportType == TransportType::StdIO) {
            auto interface = new StdIOClientInterface;
            interface->setCommandLine(m_cmdLine);
            if (bc)
                interface->setWorkingDirectory(bc->project()->projectDirectory());
            return interface;
        } else if (m_transportType == TransportType::LocalSocket) {
            if (m_serverName.isEmpty())
                return nullptr;

            auto interface = new LuaLocalSocketClientInterface(m_cmdLine, m_serverName);
            if (bc)
                interface->setWorkingDirectory(bc->project()->projectDirectory());
            return interface;
        }
        return nullptr;
    }

signals:
    void optionsChanged();
};

BaseSettings *LuaClientSettings::copy() const
{
    auto other = static_cast<LuaClientSettings *>(BaseSettings::copy());

    other->m_wrapper = m_wrapper;
    if (auto w = other->m_wrapper.lock()) {
        QObject::connect(w.get(), &LuaClientWrapper::optionsChanged, &other->guard, [other] {
            if (auto w = other->m_wrapper.lock())
                other->initializationOptions.setValue(w->m_initializationOptions);
        });
    }

    return other;
}

LuaClientSettings::LuaClientSettings(const std::weak_ptr<LuaClientWrapper> &wrapper)
    : m_wrapper(wrapper)
{
    if (auto w = m_wrapper.lock()) {
        name.setValue(w->m_name);
        m_settingsTypeId = w->m_settingsTypeId;
        m_languageFilter = w->m_languageFilter;
        initializationOptions.setValue(w->m_initializationOptions);
        m_startBehavior = w->m_startBehavior;
        showInSettings.setValue(w->m_showInSettings);
        activatable.setValue(w->m_activatable);
        QObject::connect(w.get(), &LuaClientWrapper::optionsChanged, &guard, [this] {
            if (auto w = m_wrapper.lock())
                initializationOptions.setValue(w->m_initializationOptions);
        });
    }
}

bool LuaClientSettings::applyFromSettingsWidget(QWidget *widget)
{
    BaseSettings::applyFromSettingsWidget(widget);

    if (auto w = m_wrapper.lock()) {
        w->m_name = name();
        if (!w->m_initOptionsCallback)
            w->m_initializationOptions = initializationOptions();
        w->m_languageFilter = m_languageFilter;
        w->m_startBehavior = m_startBehavior;
        w->applySettings();
    }

    return true;
}

void LuaClientSettings::toMap(Store &store) const
{
    BaseSettings::toMap(store);
    if (auto w = m_wrapper.lock())
        w->toMap(store);
}

void LuaClientSettings::fromMap(const Store &map)
{
    BaseSettings::fromMap(map);
    if (auto w = m_wrapper.lock()) {
        w->m_name = name();
        if (!w->m_initOptionsCallback)
            w->m_initializationOptions = initializationOptions();
        w->m_languageFilter = m_languageFilter;
        w->m_startBehavior = m_startBehavior;
        w->fromMap(map);
    }
}

QWidget *LuaClientSettings::createSettingsWidget(QWidget *parent) const
{
    using namespace Layouting;

    if (auto w = m_wrapper.lock())
        return new BaseSettingsWidget(this, parent, w->settingsLayout());

    return new BaseSettingsWidget(this, parent);
}

Client *LuaClientSettings::createClient(BaseClientInterface *interface) const
{
    Client *client = new LuaClient(interface, m_settingsTypeId);
    return client;
}

BaseClientInterface *LuaClientSettings::createInterface(BuildConfiguration *bc) const
{
    if (auto w = m_wrapper.lock())
        return w->createInterface(bc);

    return nullptr;
}

static void registerLuaApi()
{
    ::Lua::registerProvider("LSP", [](sol::state_view lua) -> sol::object {
        sol::table async = lua.script("return require('async')", "_process_").get<sol::table>();
        sol::function wrap = async["wrap"];

        sol::table result = lua.create_table();

        auto wrapperClass = result.new_usertype<LuaClientWrapper>(
            "Client",
            "on_instance_start",
            sol::property(
                [](const LuaClientWrapper *c) -> sol::function {
                    if (!c->m_onInstanceStart)
                        return sol::lua_nil;
                    return *c->m_onInstanceStart;
                },
                [](LuaClientWrapper *c, const sol::main_function &f) { c->m_onInstanceStart = f; }),
            "registerMessage",
            &LuaClientWrapper::registerMessageCallback,
            "sendMessage",
            &LuaClientWrapper::sendMessage,
            "sendMessageForDocument",
            &LuaClientWrapper::sendMessageForDocument,
            "sendMessageWithIdForDocument_cb",
            &LuaClientWrapper::sendMessageWithIdForDocument_cb,
            "cancelRequest",
            &LuaClientWrapper::cancelRequest,
            "create",
            [](const sol::main_table &options) -> std::shared_ptr<LuaClientWrapper> {
                auto luaClientWrapper = std::make_shared<LuaClientWrapper>(options);
                auto clientSettings = new LuaClientSettings(luaClientWrapper);

                // The order is important!
                // First restore the settings ...
                const QList<Utils::Store> savedSettings
                    = LanguageClientSettings::storesBySettingsType(
                        luaClientWrapper->m_settingsTypeId);

                if (!savedSettings.isEmpty())
                    clientSettings->fromMap(savedSettings.first());

                // ... then register the settings.
                LanguageClientManager::registerClientSettings(clientSettings);
                luaClientWrapper->m_clientSettingsId = clientSettings->m_id;

                // and the client type.
                ClientType type;
                type.id = clientSettings->m_settingsTypeId;
                type.name = luaClientWrapper->m_name;
                type.userAddable = false;
                LanguageClientSettings::registerClientType(type);

                return luaClientWrapper;
            },
            "documentVersion",
            [](LuaClientWrapper *wrapper,
               const Utils::FilePath &path) -> std::tuple<bool, std::variant<int, QString>> {
                auto clients = wrapper->clientsForDocument(
                    TextEditor::TextDocument::textDocumentForFilePath(path));
                if (clients.empty())
                    return {false, "No client found."};

                return {true, clients.first()->documentVersion(path)};
            },

            "hostPathToServerUri",
            [](LuaClientWrapper *wrapper, const Utils::FilePath &path) -> std::tuple<bool, QString> {
                auto clients = wrapper->clientsForDocument(
                    TextEditor::TextDocument::textDocumentForFilePath(path));
                if (clients.empty())
                    return {false, "No client found."};

                return {true, clients.first()->hostPathToServerUri(path).toString()};
            });

        wrapperClass["sendMessageWithIdForDocument"]
            = wrap(wrapperClass["sendMessageWithIdForDocument_cb"].get<sol::function>())
                  .get<sol::function>();

        return result;
    });
}

} // namespace LanguageClient::Lua

#include "lualanguageclient.moc"