From a428f309119086dc78dd4b19306d2d67be884eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cas=C3=ADa?= Date: Thu, 11 Jul 2024 16:17:05 +0200 Subject: [PATCH] feat(lsp): add supports_method to lsp client (#21) --- lua/nio/lsp.lua | 11 ++++++++++- tests/lsp_spec.lua | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lua/nio/lsp.lua b/lua/nio/lsp.lua index 4692aba..ea18f3e 100644 --- a/lua/nio/lsp.lua +++ b/lua/nio/lsp.lua @@ -12,6 +12,7 @@ nio.lsp = {} ---@field request nio.lsp.RequestClient Interface to all requests that can be sent by the client ---@field notify nio.lsp.NotifyClient Interface to all notifications that can be sent by the client ---@field server_capabilities nio.lsp.types.ServerCapabilities +---@field supports_method table Check if a method is supported by the client local async_request = tasks.wrap(function(client, method, params, bufnr, request_id_future, cb) local success, req_id = client.request(method, params, cb, bufnr) @@ -58,7 +59,7 @@ function nio.lsp.get_client_by_id(id) end --- Create an async client for the given client ----@param client table Neovim core LSP client object +---@param client vim.lsp.Client Neovim core LSP client object ---@return nio.lsp.Client function nio.lsp.convert_client(client) local n = require("nio") @@ -70,6 +71,14 @@ function nio.lsp.convert_client(client) return { server_capabilities = client.server_capabilities, + supports_method = setmetatable({}, { + __index = function(_, method) + method = convert_method(method) + return function(opts) + return client.supports_method(method, opts) + end + end, + }), notify = setmetatable({}, { __index = function(_, method) method = convert_method(method) diff --git a/tests/lsp_spec.lua b/tests/lsp_spec.lua index a975b86..57d7fc0 100644 --- a/tests/lsp_spec.lua +++ b/tests/lsp_spec.lua @@ -93,4 +93,22 @@ describe("lsp client", function() assert.False(success) assert.Not.Nil(string.find(err, "Client 1 has shut down")) end) + + a.it("returns a response for whether a method is supported", function() + local expected_result = false + local expected_method = "textDocument/diagnostic" + local expected_opts = { a = "b" } + vim.lsp.get_client_by_id = function(id) + return { + supports_method = function(method, opts) + assert.equals(expected_method, method) + assert.same(expected_opts, opts) + return expected_result + end, + } + end + local client = nio.lsp.get_client_by_id(1) + local result = client.supports_method.textDocument_diagnostic(expected_opts) + assert.equals(expected_result, result) + end) end)