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 <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-08-15 09:32:02 +02:00
parent 318e1ccc09
commit d4f0da4c35
3 changed files with 23 additions and 22 deletions

View File

@@ -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

View File

@@ -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<bool, std::variant<int, QString>> {
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<bool, QString> {
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"]

View File

@@ -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.