Skip to content

Commit

Permalink
fix: wait for all LSPs to respond before showing
Browse files Browse the repository at this point in the history
Closes #691
  • Loading branch information
Saghen committed Dec 30, 2024
1 parent 0350fee commit 86a13ae
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 216 deletions.
2 changes: 2 additions & 0 deletions lua/blink/cmp/sources/lib/provider/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function list.new(provider, context, on_items, opts)
end

function list:append(response)
if self.has_completed and #response.items == 0 then return end

if not self.has_completed then
self.has_completed = true
self.is_incomplete_backward = response.is_incomplete_backward
Expand Down
216 changes: 0 additions & 216 deletions lua/blink/cmp/sources/lsp.lua

This file was deleted.

71 changes: 71 additions & 0 deletions lua/blink/cmp/sources/lsp/completion.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local async = require('blink.cmp.lib.async')
local known_defaults = {
'commitCharacters',
'insertTextFormat',
'insertTextMode',
'data',
}
local CompletionTriggerKind = vim.lsp.protocol.CompletionTriggerKind

local completion = {}

function completion.get_completion_for_client(client, context)
return async.task.new(function(resolve)
local params = vim.lsp.util.make_position_params(0, client.offset_encoding)
params.context = {
triggerKind = context.trigger.kind == 'trigger_character' and CompletionTriggerKind.TriggerCharacter
or CompletionTriggerKind.Invoked,
}
if context.trigger.kind == 'trigger_character' then params.context.triggerCharacter = context.trigger.character end

local _, request_id = client.request('textDocument/completion', params, function(err, result)
if err or result == nil then
resolve({ is_incomplete_forward = false, is_incomplete_backward = false, items = {} })
return
end

local items = result.items or result
local default_edit_range = result.itemDefaults and result.itemDefaults.editRange
for _, item in ipairs(items) do
item.client_id = client.id

-- score offset for deprecated items
-- todo: make configurable
if item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)) then item.score_offset = -2 end

-- set defaults
for key, value in pairs(result.itemDefaults or {}) do
if vim.tbl_contains(known_defaults, key) then item[key] = item[key] or value end
end
if default_edit_range and item.textEdit == nil then
local new_text = item.textEditText or item.insertText or item.label
if default_edit_range.replace ~= nil then
item.textEdit = {
replace = default_edit_range.replace,
insert = default_edit_range.insert,
newText = new_text,
}
else
item.textEdit = {
range = result.itemDefaults.editRange,
newText = new_text,
}
end
end
end

resolve({
is_incomplete_forward = result.isIncomplete or false,
is_incomplete_backward = true,
items = items,
})
end)

-- cancellation function
return function()
if request_id ~= nil then client.cancel_request(request_id) end
end
end)
end

return completion
Loading

0 comments on commit 86a13ae

Please sign in to comment.