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) diff --git a/src/plugins/lua/meta/lsp.lua b/src/plugins/lua/meta/lsp.lua index 37a3c2671e5..10c42c6e68b 100644 --- a/src/plugins/lua/meta/lsp.lua +++ b/src/plugins/lua/meta/lsp.lua @@ -11,6 +11,7 @@ local lsp = {} ---@field languageFilter LanguageFilter The language filter deciding which files to open with the language server. ---@field startBehavior? "AlwaysOn"|"RequiresFile"|"RequiresProject" ---@field initializationOptions? function|table|string The initialization options to pass to the language server, either a JSON string, a table, or a function that returns either. +---@field initializationOptionsAsync? function A callback that will return the initialization options as a JSON String or a table. Inside the callback you can use Async functions. ---@field settings? AspectContainer The settings object to associate with the language server. ---@field onStartFailed? function This callback is called when client failed to start. ---@field showInSettings? boolean Whether the client should show up in the general Language Server list.