@@ -16,7 +16,17 @@ if vim.fn.has('nvim-0.7') ~= 1 then
1616 return
1717end
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>
2030local clients = {}
2131local completion_ctx
2232completion_ctx = {
@@ -236,42 +246,94 @@ end
236246local 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+
239284function 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 )
277339end
0 commit comments