Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit de7bc3e

Browse files
committed
Support multiple clients
1 parent ee203c2 commit de7bc3e

4 files changed

Lines changed: 240 additions & 22 deletions

File tree

‎.luacheckrc‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
ignore = {
22
"631", -- max_line_length
33
}
4-
read_globals = {
4+
globals = {
55
"vim",
66
}
7+
read_globals = {
8+
"describe",
9+
"it",
10+
"before_each",
11+
"after_each",
12+
"assert"
13+
}

‎lua/lsp_compl.lua‎

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ if vim.fn.has('nvim-0.7') ~= 1 then
1616
return
1717
end
1818

19+
--- @class lsp_compl.client_opts
20+
--- @field server_side_fuzzy_completion boolean
21+
--- @field leading_debounce number
22+
--- @field subsequent_debounce number|nil
23+
--- @field trigger_on_delete boolean
1924

25+
--- @class lsp_compl.client
26+
--- @field num_attached number
27+
--- @field opts lsp_compl.client_opts
28+
29+
--- @type table<number, lsp_compl.client>
2030
local clients = {}
2131
local completion_ctx
2232
completion_ctx = {
@@ -236,42 +246,94 @@ end
236246
local compute_new_average = exp_avg(10, 10)
237247

238248

249+
local function request(opts, callback)
250+
local attached_clients = {}
251+
for client_id, _ in pairs(clients) do
252+
local client = vim.lsp.get_client_by_id(client_id)
253+
if client and lsp.buf_is_attached(opts.bufnr, client.id) then
254+
table.insert(attached_clients, client)
255+
end
256+
end
257+
local results = {}
258+
local request_ids = {}
259+
local remaining_results = #attached_clients
260+
for _, client in pairs(attached_clients) do
261+
local params = lsp.util.make_position_params(opts.win, client.offset_encoding)
262+
local ok, request_id = client.request('textDocument/completion', params, function(err, result)
263+
results[client.id] = { err = err, result = result }
264+
remaining_results = remaining_results - 1
265+
if remaining_results == 0 then
266+
callback(results)
267+
end
268+
end)
269+
if ok then
270+
request_ids[client.id] = request_id
271+
end
272+
end
273+
return function()
274+
for client_id, request_id in pairs(request_ids) do
275+
local client = vim.lsp.get_client_by_id(client_id)
276+
if client then
277+
client.cancel_request(request_id)
278+
end
279+
end
280+
end
281+
end
282+
283+
239284
function M.trigger_completion()
240285
reset_timer()
241286
completion_ctx.cancel_pending()
242-
local lnum, cursor_pos = unpack(api.nvim_win_get_cursor(0))
287+
local win = api.nvim_get_current_win()
288+
local bufnr = api.nvim_get_current_buf()
289+
local lnum, cursor_pos = unpack(api.nvim_win_get_cursor(win))
243290
local line = api.nvim_get_current_line()
244291
local line_to_cursor = line:sub(1, cursor_pos)
245292
local col = vim.fn.match(line_to_cursor, '\\k*$') + 1
246-
local params = lsp.util.make_position_params()
247293
local start = vim.loop.hrtime()
248294
completion_ctx.last_request = start
249-
local _, cancel_req = lsp.buf_request(0, 'textDocument/completion', params, function(err, result, ctx)
295+
296+
local cancel_req = request({ bufnr = bufnr, win = win }, function(responses)
250297
local end_ = vim.loop.hrtime()
251298
rtt_ms = compute_new_average((end_ - start) * ns_to_ms)
252-
local client_id = ctx.client_id
253299
completion_ctx.pending_requests = {}
254-
assert(not err, vim.inspect(err))
255-
if not result then
256-
print('No completion result')
257-
return
258-
end
259-
completion_ctx.isIncomplete = result.isIncomplete
260-
local line_changed = api.nvim_win_get_cursor(0)[1] ~= lnum
300+
completion_ctx.isIncomplete = false
301+
302+
local line_changed = api.nvim_win_get_cursor(win)[1] ~= lnum
261303
local mode = api.nvim_get_mode()['mode']
262304
if line_changed or not (mode == 'i' or mode == 'ic') then
263305
return
264306
end
265-
local client = vim.lsp.get_client_by_id(client_id)
266-
local items = get_completion_items(result)
267-
local encoding = client and client.offset_encoding or 'utf-16'
268-
local startbyte = adjust_start_col(lnum, line, items, encoding) or col
269-
local opts = (clients[client_id] or {}).opts
270-
local matches = M.text_document_completion_list_to_complete_items(
271-
result,
272-
opts.server_side_fuzzy_completion
273-
)
274-
vim.fn.complete(startbyte, matches)
307+
308+
local all_matches = {}
309+
local startbyte
310+
for client_id, response in pairs(responses) do
311+
assert(not response.err, vim.inspect(response.err))
312+
local result = response.result
313+
if result then
314+
completion_ctx.isIncomplete = completion_ctx.isIncomplete or result.isIncomplete
315+
local client = vim.lsp.get_client_by_id(client_id)
316+
local items = get_completion_items(result)
317+
local encoding = client and client.offset_encoding or 'utf-16'
318+
local current_startbyte = adjust_start_col(lnum, line, items, encoding)
319+
if startbyte == nil then
320+
startbyte = current_startbyte
321+
elseif current_startbyte ~= nil and current_startbyte ~= startbyte then
322+
startbyte = col
323+
end
324+
local opts = (clients[client_id] or {}).opts
325+
local matches = M.text_document_completion_list_to_complete_items(
326+
result,
327+
opts.server_side_fuzzy_completion
328+
)
329+
vim.list_extend(all_matches, matches)
330+
end
331+
end
332+
if next(all_matches) then
333+
vim.fn.complete(startbyte or col, all_matches)
334+
else
335+
print('No completion result')
336+
end
275337
end)
276338
table.insert(completion_ctx.pending_requests, cancel_req)
277339
end

‎tests/lsp_compl_spec.lua‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
local api = vim.api
2+
local compl = require('lsp_compl')
3+
4+
local messages = {}
5+
6+
local function new_server(completion_result)
7+
local function server(dispatchers)
8+
local closing = false
9+
local srv = {}
10+
11+
function srv.request(method, params, callback)
12+
table.insert(messages, {
13+
method = method,
14+
params = params,
15+
})
16+
if method == 'initialize' then
17+
callback(nil, {
18+
capabilities = {
19+
completionProvider = {
20+
triggerCharacters = {'.'}
21+
}
22+
}
23+
})
24+
elseif method == 'textDocument/completion' then
25+
callback(nil, completion_result)
26+
elseif method == 'shutdown' then
27+
callback(nil, nil)
28+
end
29+
end
30+
31+
function srv.notify(method, _)
32+
if method == 'exit' then
33+
dispatchers.on_exit(0, 15)
34+
end
35+
end
36+
37+
function srv.is_closing()
38+
return closing
39+
end
40+
41+
function srv.terminate()
42+
closing = true
43+
end
44+
45+
return srv
46+
end
47+
return server
48+
end
49+
50+
51+
local function wait(condition, msg)
52+
vim.wait(100, condition)
53+
local result = condition()
54+
assert.are_not.same(false, result, msg)
55+
assert.are_not.same(nil, result, msg)
56+
end
57+
58+
59+
describe('lsp_compl', function()
60+
local buf = api.nvim_create_buf(false, true)
61+
local win = api.nvim_get_current_win()
62+
local capture = {}
63+
vim.fn.complete = function(col, matches)
64+
capture.col = col
65+
capture.matches = matches
66+
end
67+
api.nvim_get_mode = function()
68+
return {
69+
mode = 'i'
70+
}
71+
end
72+
api.nvim_win_set_buf(win, buf)
73+
74+
before_each(function()
75+
capture = {}
76+
messages = {}
77+
end)
78+
79+
after_each(function()
80+
vim.lsp.stop_client(vim.lsp.get_active_clients())
81+
wait(function() return vim.tbl_count(vim.lsp.get_active_clients()) == 0 end, 'clients must stop')
82+
end)
83+
84+
it('fetches completions and shows them using complete on trigger_completion', function()
85+
local completion_result = {
86+
isIncomplete = false,
87+
items = {
88+
{
89+
label = 'hello',
90+
}
91+
}
92+
}
93+
local server = new_server(completion_result)
94+
vim.lsp.start({ name = 'fake-server', cmd = server, on_attach = compl.attach })
95+
api.nvim_buf_set_lines(buf, 0, -1, true, {'a'})
96+
api.nvim_win_set_cursor(win, { 1, 1 })
97+
compl.trigger_completion()
98+
wait(function() return capture.col ~= nil end)
99+
assert.are.same(2, #messages)
100+
local expected_matches = {
101+
{
102+
abbr = 'hello',
103+
dup = 1,
104+
empty = 1,
105+
equal = 0,
106+
icase = 1,
107+
info = '',
108+
kind = '',
109+
menu = '',
110+
user_data = {
111+
label = 'hello',
112+
},
113+
word = 'hello'
114+
}
115+
}
116+
assert.are.same(expected_matches, capture.matches)
117+
end)
118+
119+
it('merges results from multiple clients', function()
120+
local server1 = new_server({
121+
isIncomplete = false,
122+
items = {
123+
{
124+
label = 'hello',
125+
}
126+
}
127+
})
128+
vim.lsp.start({ name = 'server1', cmd = server1, on_attach = compl.attach })
129+
local server2 = new_server({
130+
isIncomplete = false,
131+
items = {
132+
{
133+
label = 'hallo',
134+
}
135+
}
136+
})
137+
vim.lsp.start({ name = 'server2', cmd = server2, on_attach = compl.attach })
138+
api.nvim_buf_set_lines(buf, 0, -1, true, {'a'})
139+
api.nvim_win_set_cursor(win, { 1, 1 })
140+
compl.trigger_completion()
141+
wait(function() return capture.col ~= nil end)
142+
assert.are.same(2, #capture.matches)
143+
assert.are.same('hello', capture.matches[1].word)
144+
assert.are.same('hallo', capture.matches[2].word)
145+
end)
146+
end)

‎tests/minimal.vim‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set rtp+=.
2+
set rtp+=../plenary.nvim
3+
runtime! plugin/plenary.vim

0 commit comments

Comments
 (0)