mirror of
https://github.com/neovim/neovim.git
synced 2025-09-29 14:38:32 +00:00
test(lsp): make async format test work properly (#35794)
Overriding vim.lsp.handlers['textDocument/formatting'] doesn't work here
because fake_lsp_server_setup() uses a table with __index to specify
client handlers, which takes priority over vim.lsp.handlers[], and as a
result the overridden handler is never called, and the test ends before
the vim.wait() even finishes.
Instead, set a global variable from the handler that is actually reached
(by vim.rpcrequest() from client handler), and avoid stopping the event
loop too early.
(cherry picked from commit 8d5476691c
)
This commit is contained in:

committed by
github-actions[bot]
![github-actions[bot]](/assets/img/avatar_default.png)
parent
777551c599
commit
d86658f9d9
@@ -199,32 +199,35 @@ function M.test_rpc_server(config)
|
|||||||
})
|
})
|
||||||
--- @type integer, integer
|
--- @type integer, integer
|
||||||
local code, signal
|
local code, signal
|
||||||
|
local busy = 0
|
||||||
|
local exited = false
|
||||||
local function on_request(method, args)
|
local function on_request(method, args)
|
||||||
if method == 'setup' then
|
busy = busy + 1
|
||||||
if config.on_setup then
|
if method == 'setup' and config.on_setup then
|
||||||
config.on_setup()
|
config.on_setup()
|
||||||
end
|
end
|
||||||
return NIL
|
if method == 'init' and config.on_init then
|
||||||
end
|
|
||||||
if method == 'init' then
|
|
||||||
if config.on_init then
|
|
||||||
config.on_init(client, unpack(args))
|
config.on_init(client, unpack(args))
|
||||||
end
|
end
|
||||||
return NIL
|
if method == 'handler' and config.on_handler then
|
||||||
end
|
|
||||||
if method == 'handler' then
|
|
||||||
if config.on_handler then
|
|
||||||
config.on_handler(unpack(args))
|
config.on_handler(unpack(args))
|
||||||
end
|
end
|
||||||
|
busy = busy - 1
|
||||||
|
if busy == 0 and exited then
|
||||||
|
stop()
|
||||||
end
|
end
|
||||||
return NIL
|
return NIL
|
||||||
end
|
end
|
||||||
local function on_notify(method, args)
|
local function on_notify(method, args)
|
||||||
if method == 'exit' then
|
if method == 'exit' then
|
||||||
code, signal = unpack(args)
|
code, signal = unpack(args)
|
||||||
return stop()
|
exited = true
|
||||||
|
if busy == 0 then
|
||||||
|
stop()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return NIL
|
||||||
|
end
|
||||||
-- TODO specify timeout?
|
-- TODO specify timeout?
|
||||||
-- run(on_request, on_notify, nil, 1000)
|
-- run(on_request, on_notify, nil, 1000)
|
||||||
run(on_request, on_notify, nil)
|
run(on_request, on_notify, nil)
|
||||||
|
@@ -5132,22 +5132,18 @@ describe('LSP', function()
|
|||||||
notify_msg = msg
|
notify_msg = msg
|
||||||
end
|
end
|
||||||
|
|
||||||
local handler = vim.lsp.handlers['textDocument/formatting']
|
_G.handler_called = false
|
||||||
local handler_called = false
|
|
||||||
vim.lsp.handlers['textDocument/formatting'] = function()
|
|
||||||
handler_called = true
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.lsp.buf.format({ bufnr = bufnr, async = true })
|
vim.lsp.buf.format({ bufnr = bufnr, async = true })
|
||||||
vim.wait(1000, function()
|
vim.wait(1000, function()
|
||||||
return handler_called
|
return _G.handler_called
|
||||||
end)
|
end)
|
||||||
|
|
||||||
vim.notify = notify
|
vim.notify = notify
|
||||||
vim.lsp.handlers['textDocument/formatting'] = handler
|
return { notify_msg = notify_msg, handler_called = _G.handler_called }
|
||||||
return { notify = notify_msg, handler_called = handler_called }
|
|
||||||
end)
|
end)
|
||||||
eq({ handler_called = true }, result)
|
eq({ handler_called = true }, result)
|
||||||
|
elseif ctx.method == 'textDocument/formatting' then
|
||||||
|
exec_lua('_G.handler_called = true')
|
||||||
elseif ctx.method == 'shutdown' then
|
elseif ctx.method == 'shutdown' then
|
||||||
client:stop()
|
client:stop()
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user