From e03aa820b43e7ca400f64c83977dfd9bf01354b6 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 26 Sep 2024 12:52:11 +0200 Subject: Lua LSP: Add parameter for async options Allows using async code to create the options of a language client. This is useful for instance if you need to get values from a SecrectAspect Change-Id: Ica23b7f0df00bff3af7520524905a59ceb5b8e8c Reviewed-by: David Schulz --- .../lualanguageclient/lualanguageclient.cpp | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/plugins/languageclient/lualanguageclient') diff --git a/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp b/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp index 82f4a091dc2..ebc1e522fad 100644 --- a/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp +++ b/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp @@ -11,6 +11,8 @@ #include #include +#include + #include #include @@ -18,6 +20,7 @@ #include #include +#include #include #include @@ -207,6 +210,8 @@ public: TransportType m_transportType{TransportType::StdIO}; std::function(CommandLine &)> m_cmdLineCallback; std::function(QString &)> m_initOptionsCallback; + sol::function m_asyncInitOptions; + bool m_isUpdatingAsyncOptions{false}; AspectContainer *m_aspects{nullptr}; QString m_name; Utils::Id m_settingsTypeId; @@ -324,6 +329,13 @@ public: &LanguageClientManager::clientRemoved, this, &LuaClientWrapper::onClientRemoved); + + if (auto asyncInit = options.get>( + "initializationOptionsAsync")) { + m_asyncInitOptions = *asyncInit; + QMetaObject::invokeMethod( + this, &LuaClientWrapper::updateAsyncOptions, Qt::QueuedConnection); + } } void onClientRemoved(Client *c, bool unexpected) @@ -462,6 +474,25 @@ public: clients.front()->sendMessage(request); } + void updateAsyncOptions() + { + if (m_isUpdatingAsyncOptions) + return; + QTC_ASSERT(m_asyncInitOptions, return); + m_isUpdatingAsyncOptions = true; + std::function cb = guardedCallback(this, [this](sol::object options) { + if (options.is()) + m_initializationOptions = ::Lua::toJsonString(options.as()); + else if (options.is()) + m_initializationOptions = options.as(); + + emit optionsChanged(); + m_isUpdatingAsyncOptions = false; + }); + + ::Lua::Async::start(m_asyncInitOptions, cb); + } + void updateOptions() { if (m_cmdLineCallback) { @@ -479,6 +510,8 @@ public: // 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) -- cgit v1.2.3