Skip to content

Commit

Permalink
feat(lsp): add supports_method to lsp client
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasia committed Jun 30, 2024
1 parent 7969e0a commit 062e9ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lua/nio/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, fun(opts: { bufnr : number }): boolean> 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)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 062e9ab

Please sign in to comment.