Skip to content

Commit

Permalink
refactor(lsp): inline BufWritePost and VimLeavePre callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ii14 committed Jul 17, 2022
1 parent ed25bac commit 99aec6e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 57 deletions.
97 changes: 48 additions & 49 deletions runtime/lua/vim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,9 @@ do
end
end

-- Buffer lifecycle handler for textDocument/didSave
function lsp._text_document_did_save_handler(bufnr)
---@private
---Buffer lifecycle handler for textDocument/didSave
local function text_document_did_save_handler(bufnr)
bufnr = resolve_bufnr(bufnr)
local uri = vim.uri_from_bufnr(bufnr)
local text = once(buf_get_full_text)
Expand Down Expand Up @@ -1449,10 +1450,10 @@ function lsp.buf_attach_client(bufnr, client_id)
api.nvim_create_autocmd('BufWritePost', {
group = api.nvim_create_augroup(augroup, { clear = true }),
buffer = bufnr,
callback = function()
lsp._text_document_did_save_handler(0)
end,
desc = 'vim.lsp: textDocument/didSave handler',
callback = function(ctx)
text_document_did_save_handler(ctx.buf)
end,
})
-- First time, so attach and set up stuff.
api.nvim_buf_attach(bufnr, false, {
Expand Down Expand Up @@ -1636,62 +1637,60 @@ function lsp.get_active_clients(filter)
return clients
end

function lsp._vim_exit_handler()
log.info('exit_handler', active_clients)
for _, client in pairs(uninitialized_clients) do
client.stop(true)
end
-- TODO handle v:dying differently?
if tbl_isempty(active_clients) then
return
end
for _, client in pairs(active_clients) do
client.stop()
end
api.nvim_create_autocmd('VimLeavePre', {
desc = 'vim.lsp: exit handler',
callback = function()
log.info('exit_handler', active_clients)
for _, client in pairs(uninitialized_clients) do
client.stop(true)
end
-- TODO handle v:dying differently?
if tbl_isempty(active_clients) then
return
end
for _, client in pairs(active_clients) do
client.stop()
end

local timeouts = {}
local max_timeout = 0
local send_kill = false
local timeouts = {}
local max_timeout = 0
local send_kill = false

for client_id, client in pairs(active_clients) do
local timeout = if_nil(client.config.flags.exit_timeout, 500)
if timeout then
send_kill = true
timeouts[client_id] = timeout
max_timeout = math.max(timeout, max_timeout)
for client_id, client in pairs(active_clients) do
local timeout = if_nil(client.config.flags.exit_timeout, 500)
if timeout then
send_kill = true
timeouts[client_id] = timeout
max_timeout = math.max(timeout, max_timeout)
end
end
end

local poll_time = 50
local poll_time = 50

---@private
local function check_clients_closed()
for client_id, timeout in pairs(timeouts) do
timeouts[client_id] = timeout - poll_time
end
---@private
local function check_clients_closed()
for client_id, timeout in pairs(timeouts) do
timeouts[client_id] = timeout - poll_time
end

for client_id, _ in pairs(active_clients) do
if timeouts[client_id] ~= nil and timeouts[client_id] > 0 then
return false
for client_id, _ in pairs(active_clients) do
if timeouts[client_id] ~= nil and timeouts[client_id] > 0 then
return false
end
end
return true
end
return true
end

if send_kill then
if not vim.wait(max_timeout, check_clients_closed, poll_time) then
for client_id, client in pairs(active_clients) do
if timeouts[client_id] ~= nil then
client.stop(true)
if send_kill then
if not vim.wait(max_timeout, check_clients_closed, poll_time) then
for client_id, client in pairs(active_clients) do
if timeouts[client_id] ~= nil then
client.stop(true)
end
end
end
end
end
end

api.nvim_create_autocmd('VimLeavePre', {
callback = lsp._vim_exit_handler,
desc = 'vim.lsp: exit handler',
end,
})

--- Sends an async request for all active clients attached to the
Expand Down
2 changes: 1 addition & 1 deletion test/functional/plugin/lsp/diagnostic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('vim.lsp.diagnostic', function()
local diags = vim.diagnostic.get(diagnostic_bufnr)
vim.lsp.stop_client(client_id)
vim.lsp._vim_exit_handler()
vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
return diags
]], line)
eq(1, #result)
Expand Down
14 changes: 7 additions & 7 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ local function test_rpc_server(config)
end
stop()
if config.test_name then
exec_lua("lsp._vim_exit_handler()")
exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })")
end
end

Expand Down Expand Up @@ -172,7 +172,7 @@ describe('LSP', function()
end)

after_each(function()
exec_lua("lsp._vim_exit_handler()")
exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })")
-- exec_lua("lsp.stop_all_clients(true)")
end)

Expand Down Expand Up @@ -249,7 +249,7 @@ describe('LSP', function()
after_each(function()
stop()
exec_lua("lsp.stop_client(lsp.get_active_clients(), true)")
exec_lua("lsp._vim_exit_handler()")
exec_lua("vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })")
end)

it('should run correctly', function()
Expand Down Expand Up @@ -505,7 +505,7 @@ describe('LSP', function()
}
end)

it('_text_document_did_save_handler sends didSave with bool textDocumentSync.save', function()
it('BufWritePost sends didSave with bool textDocumentSync.save', function()
local expected_handlers = {
{NIL, {}, {method="shutdown", client_id=1}};
{NIL, {}, {method="start", client_id=1}};
Expand All @@ -526,7 +526,7 @@ describe('LSP', function()
exec_lua([=[
BUFFER = vim.api.nvim_get_current_buf()
lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)
lsp._text_document_did_save_handler(BUFFER)
vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false })
]=])
else
client.stop()
Expand All @@ -535,7 +535,7 @@ describe('LSP', function()
}
end)

it('_text_document_did_save_handler sends didSave including text if server capability is set', function()
it('BufWritePost sends didSave including text if server capability is set', function()
local expected_handlers = {
{NIL, {}, {method="shutdown", client_id=1}};
{NIL, {}, {method="start", client_id=1}};
Expand All @@ -557,7 +557,7 @@ describe('LSP', function()
BUFFER = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(BUFFER, 0, -1, true, {"help me"})
lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)
lsp._text_document_did_save_handler(BUFFER)
vim.api.nvim_exec_autocmds('BufWritePost', { buffer = BUFFER, modeline = false })
]=])
else
client.stop()
Expand Down

0 comments on commit 99aec6e

Please sign in to comment.