From d4f0da4c354d67f89bf55faf4a73e8faecf95918 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 15 Aug 2024 09:32:02 +0200 Subject: [PATCH] LuaLS: Improve error reporting Changes LSP.Client:documentVersion() and LSP.Client:hostPathToServerUri() to return ok and error message in case of errors. Also fixes the binding to take a self parameter so it fits to the function documentation. Change-Id: I605b7bacba2822c3efd5291d1f7bacf1ecb863d5 Reviewed-by: David Schulz --- .../lua-plugins/ai_asistant/init.lua | 12 ++++----- .../lualanguageclient/lualanguageclient.cpp | 27 +++++++++---------- src/plugins/lua/meta/lsp.lua | 6 +++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/share/qtcreator/lua-plugins/ai_asistant/init.lua b/share/qtcreator/lua-plugins/ai_asistant/init.lua index e6ab2f4a611..afd6c743da3 100644 --- a/share/qtcreator/lua-plugins/ai_asistant/init.lua +++ b/share/qtcreator/lua-plugins/ai_asistant/init.lua @@ -277,15 +277,15 @@ local function buildRequest() local document = editor:document() local filePath = document:file() - local doc_version = Client.documentVersion(filePath) - if doc_version == -1 then - print("No document version found") + local ok, doc_version = Client:documentVersion(filePath) + if not ok then + print("No document version found:", doc_version) return end - local doc_uri = Client.hostPathToServerUri(filePath) - if doc_uri == nil or doc_uri == "" then - print("No document uri found") + local ok, doc_uri = Client:hostPathToServerUri(filePath) + if not ok then + print("No document uri found", doc_uri) return end diff --git a/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp b/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp index c78be12ac4b..82f4a091dc2 100644 --- a/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp +++ b/src/plugins/languageclient/lualanguageclient/lualanguageclient.cpp @@ -695,25 +695,24 @@ static void registerLuaApi() return luaClientWrapper; }, "documentVersion", - [](const Utils::FilePath &path) -> int { - auto client = LanguageClientManager::clientForFilePath(path); - if (!client) { - qWarning() << "documentVersion(). No client for file path:" << path; - return -1; - } + [](LuaClientWrapper *wrapper, + const Utils::FilePath &path) -> std::tuple> { + auto clients = wrapper->clientsForDocument( + TextEditor::TextDocument::textDocumentForFilePath(path)); + if (clients.empty()) + return {false, "No client found."}; - return client->documentVersion(path); + return {true, clients.first()->documentVersion(path)}; }, "hostPathToServerUri", - [](const Utils::FilePath &path) -> QString { - auto client = LanguageClientManager::clientForFilePath(path); - if (!client) { - qWarning() << "hostPathToServerUri(). No client for file path:" << path; - return {}; - } + [](LuaClientWrapper *wrapper, const Utils::FilePath &path) -> std::tuple { + auto clients = wrapper->clientsForDocument( + TextEditor::TextDocument::textDocumentForFilePath(path)); + if (clients.empty()) + return {false, "No client found."}; - return client->hostPathToServerUri(path).toString(); + return {true, clients.first()->hostPathToServerUri(path).toString()}; }); wrapperClass["sendMessageWithIdForDocument"] diff --git a/src/plugins/lua/meta/lsp.lua b/src/plugins/lua/meta/lsp.lua index 5186afe2349..37a3c2671e5 100644 --- a/src/plugins/lua/meta/lsp.lua +++ b/src/plugins/lua/meta/lsp.lua @@ -46,11 +46,13 @@ function lsp.Client:sendMessageForDocument(document, msg) end function lsp.Client:sendMessageWithIdForDocument(document, msg) end ---@param filePath FilePath to get the version of. ----@return integer Returns -1 on error, otherwise current document version. +---@return boolean ok Returns false on error, otherwise true. +---@return integer|string error_or_version Current document version, or an error message. function lsp.Client:documentVersion(filePath) end --- ---@param filePath table file path to get the uri of. ----@return string Returns empty string on error, otherwise the server URI string. +---@return boolean ok Returns false on error, otherwise true. +---@return string error_or_uri The server URI string, or an error message. function lsp.Client:hostPathToServerUri(filePath) end ---Creates a new Language Client.