+83
-2
lines changedFilter options
+83
-2
lines changed Original file line number Diff line number Diff line change
@@ -1112,9 +1112,9 @@ local text_document_did_change_handler
1112
1112
do
1113
1113
text_document_did_change_handler = function(_, bufnr, changedtick, firstline, lastline, new_lastline)
1114
1114
1115
-
-- Don't do anything if there are no clients attached.
1115
+
-- Detach (nvim_buf_attach) via returning True to on_lines if no clients are attached
1116
1116
if tbl_isempty(all_buffer_active_clients[bufnr] or {}) then
1117
-
return
1117
+
return true
1118
1118
end
1119
1119
util.buf_versions[bufnr] = changedtick
1120
1120
local compute_change_and_notify = changetracking.prepare(bufnr, firstline, lastline, new_lastline)
@@ -1220,6 +1220,50 @@ function lsp.buf_attach_client(bufnr, client_id)
1220
1220
return true
1221
1221
end
1222
1222
1223
+
--- Detaches client from the specified buffer.
1224
+
--- Note: While the server is notified that the text document (buffer)
1225
+
--- was closed, it is still able to send notifications should it ignore this notification.
1226
+
---
1227
+
---@param bufnr number Buffer handle, or 0 for current
1228
+
---@param client_id number Client id
1229
+
function lsp.buf_detach_client(bufnr, client_id)
1230
+
validate {
1231
+
bufnr = {bufnr, 'n', true};
1232
+
client_id = {client_id, 'n'};
1233
+
}
1234
+
bufnr = resolve_bufnr(bufnr)
1235
+
1236
+
local client = lsp.get_client_by_id(client_id)
1237
+
if not client or not client.attached_buffers[bufnr] then
1238
+
vim.notify(
1239
+
string.format('Buffer (id: %d) is not attached to client (id: %d). Cannot detach.', client_id, bufnr)
1240
+
)
1241
+
return
1242
+
end
1243
+
1244
+
changetracking.reset_buf(client, bufnr)
1245
+
1246
+
if client.resolved_capabilities.text_document_open_close then
1247
+
local uri = vim.uri_from_bufnr(bufnr)
1248
+
local params = { textDocument = { uri = uri; } }
1249
+
client.notify('textDocument/didClose', params)
1250
+
end
1251
+
1252
+
client.attached_buffers[bufnr] = nil
1253
+
util.buf_versions[bufnr] = nil
1254
+
1255
+
all_buffer_active_clients[bufnr][client_id] = nil
1256
+
if #vim.tbl_keys(all_buffer_active_clients[bufnr]) == 0 then
1257
+
all_buffer_active_clients[bufnr] = nil
1258
+
end
1259
+
1260
+
local namespace = vim.lsp.diagnostic.get_namespace(client_id)
1261
+
vim.diagnostic.reset(namespace, bufnr)
1262
+
1263
+
vim.notify(string.format('Detached buffer (id: %d) from client (id: %d)', bufnr, client_id))
1264
+
1265
+
end
1266
+
1223
1267
--- Checks if a buffer is attached for a particular client.
1224
1268
---
1225
1269
---@param bufnr (number) Buffer handle, or 0 for current
Original file line number Diff line number Diff line change
@@ -301,6 +301,43 @@ describe('LSP', function()
301
301
}
302
302
end)
303
303
304
+
it('should detach buffer in response to nvim_buf_detach', function()
305
+
local expected_handlers = {
306
+
{NIL, {}, {method="shutdown", client_id=1}};
307
+
{NIL, {}, {method="finish", client_id=1}};
308
+
}
309
+
local client
310
+
test_rpc_server {
311
+
test_name = "basic_finish";
312
+
on_setup = function()
313
+
exec_lua [[
314
+
BUFFER = vim.api.nvim_create_buf(false, true)
315
+
]]
316
+
eq(true, exec_lua("return lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)"))
317
+
eq(true, exec_lua("return lsp.buf_is_attached(BUFFER, TEST_RPC_CLIENT_ID)"))
318
+
exec_lua [[
319
+
vim.api.nvim_command(BUFFER.."bwipeout")
320
+
]]
321
+
end;
322
+
on_init = function(_client)
323
+
client = _client
324
+
client.notify('finish')
325
+
end;
326
+
on_exit = function(code, signal)
327
+
eq(0, code, "exit code", fake_lsp_logfile)
328
+
eq(0, signal, "exit signal", fake_lsp_logfile)
329
+
end;
330
+
on_handler = function(err, result, ctx)
331
+
eq(table.remove(expected_handlers), {err, result, ctx}, "expected handler")
332
+
if ctx.method == 'finish' then
333
+
exec_lua("return lsp.buf_detach_client(BUFFER, TEST_RPC_CLIENT_ID)")
334
+
eq(false, exec_lua("return lsp.buf_is_attached(BUFFER, TEST_RPC_CLIENT_ID)"))
335
+
client.stop()
336
+
end
337
+
end;
338
+
}
339
+
end)
340
+
304
341
it('client should return settings via workspace/configuration handler', function()
305
342
local expected_handlers = {
306
343
{NIL, {}, {method="shutdown", client_id=1}};
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4