Skip to content

Commit 3c4ca98

Browse files
authored
client: dynamically detect method style calls (#231)
1 parent 00421b9 commit 3c4ca98

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

lua/null-ls/client.lua

+24-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ local loop = require("null-ls.loop")
99
local api = vim.api
1010
local lsp = vim.lsp
1111

12+
-- Inspired by upstream neovim:
13+
-- <https://github.com/neovim/neovim/blob/43d552c56648bc3125c7509b3d708b6bf6c0c09c/runtime/lua/vim/lsp/client.lua#L234-L249>.
14+
-- This can go away sometime after neovim drops support for the legacy way
15+
-- of invoking these functions.
16+
-- Alternatively, we could get out of the business of monkeypatching Neovim
17+
-- functions, see <https://github.com/nvimtools/none-ls.nvim/discussions/229>.
18+
--- @param obj table<string,any>
19+
--- @param cls table<string,function>
20+
--- @param name string
21+
local function method_wrapper(obj, cls, name)
22+
local func = assert(obj[name], "couldn't find " .. name .. " function")
23+
obj[name] = function(maybe_self, ...)
24+
if maybe_self and getmetatable(maybe_self) == cls then
25+
-- First argument is self. Drop it and call `func` directly.
26+
return func(...)
27+
end
28+
vim.deprecate("client." .. name, "client:" .. name, "0.13")
29+
-- First argument is not self, include it when calling `func`.
30+
return func(maybe_self, ...)
31+
end
32+
end
33+
1234
---@type vim.lsp.Client?, integer?
1335
local client, id
1436

@@ -75,12 +97,9 @@ local on_init = function(new_client, initialize_result)
7597
return methods.lsp[method] ~= nil
7698
end
7799

100+
new_client.supports_method = supports_method
78101
if vim.fn.has("nvim-0.11") == 1 then
79-
new_client.supports_method = function(_, method)
80-
return supports_method(method)
81-
end
82-
else
83-
new_client.supports_method = supports_method
102+
method_wrapper(new_client, vim.lsp.client, "supports_method")
84103
end
85104

86105
if c.get().on_init then

test/spec/client_spec.lua

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("client", function()
2424
resolved_capabilities = {},
2525
server_capabilities = {},
2626
}
27+
setmetatable(mock_client, vim.lsp.client)
2728
lsp.start_client.returns(mock_client_id)
2829
sources.get_filetypes.returns(mock_filetypes)
2930
end)
@@ -131,6 +132,16 @@ describe("client", function()
131132
assert.equals(is_supported, true)
132133
end)
133134

135+
if vim.fn.has("nvim-0.11") == 1 then
136+
it("can still use legacy . syntax", function()
137+
can_run.returns(true)
138+
local is_supported = mock_client.supports_method(methods.lsp.CODE_ACTION)
139+
140+
assert.stub(can_run).was_called_with(vim.bo.filetype, methods.internal.CODE_ACTION)
141+
assert.equals(is_supported, true)
142+
end)
143+
end
144+
134145
it("should return result of methods.is_supported if no corresponding internal method", function()
135146
local is_supported = supports_method(methods.lsp.SHUTDOWN)
136147

0 commit comments

Comments
 (0)