mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
feat(lsp): add vim.lsp.buf.format (#18193)
This commit is contained in:

committed by
GitHub

parent
eecc6535eb
commit
5b04e46d23
@@ -2773,4 +2773,65 @@ describe('LSP', function()
|
||||
}
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("vim.lsp.buf.format", function()
|
||||
it("Aborts with notify if no client matches filter", function()
|
||||
local client
|
||||
test_rpc_server {
|
||||
test_name = "basic_init",
|
||||
on_init = function(c)
|
||||
client = c
|
||||
end,
|
||||
on_handler = function()
|
||||
local notify_msg = exec_lua([[
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
|
||||
local notify_msg
|
||||
local notify = vim.notify
|
||||
vim.notify = function(msg, log_level)
|
||||
notify_msg = msg
|
||||
end
|
||||
vim.lsp.buf.format({ name = 'does-not-exist' })
|
||||
vim.notify = notify
|
||||
return notify_msg
|
||||
]])
|
||||
eq("[LSP] Format request failed, no matching language servers.", notify_msg)
|
||||
client.stop()
|
||||
end,
|
||||
}
|
||||
end)
|
||||
it("Sends textDocument/formatting request to format buffer", function()
|
||||
local expected_handlers = {
|
||||
{NIL, {}, {method="shutdown", client_id=1}};
|
||||
{NIL, {}, {method="start", client_id=1}};
|
||||
}
|
||||
local client
|
||||
test_rpc_server {
|
||||
test_name = "basic_formatting",
|
||||
on_init = function(c)
|
||||
client = c
|
||||
end,
|
||||
on_handler = function(_, _, ctx)
|
||||
table.remove(expected_handlers)
|
||||
if ctx.method == "start" then
|
||||
local notify_msg = exec_lua([[
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
|
||||
local notify_msg
|
||||
local notify = vim.notify
|
||||
vim.notify = function(msg, log_level)
|
||||
notify_msg = msg
|
||||
end
|
||||
vim.lsp.buf.format({ bufnr = bufnr })
|
||||
vim.notify = notify
|
||||
return notify_msg
|
||||
]])
|
||||
eq(NIL, notify_msg)
|
||||
elseif ctx.method == "shutdown" then
|
||||
client.stop()
|
||||
end
|
||||
end,
|
||||
}
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
Reference in New Issue
Block a user