Skip to content

Commit

Permalink
feat: cache resolve tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 28, 2024
1 parent bd90e00 commit 83a8303
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lua/blink/cmp/sources/lib/provider/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--- @field config blink.cmp.SourceProviderConfigWrapper
--- @field module blink.cmp.Source
--- @field last_response blink.cmp.CompletionResponse | nil
--- @field resolve_tasks table<blink.cmp.CompletionItem, blink.cmp.Task>
---
--- @field new fun(id: string, config: blink.cmp.SourceProviderConfig): blink.cmp.SourceProvider
--- @field get_trigger_characters fun(self: blink.cmp.SourceProvider): string[]
Expand Down Expand Up @@ -35,6 +36,7 @@ function source.new(id, config)
)
self.config = require('blink.cmp.sources.lib.provider.config').new(config)
self.last_response = nil
self.resolve_tasks = {}

return self
end
Expand Down Expand Up @@ -100,12 +102,23 @@ end
--- @param item blink.cmp.CompletionItem
--- @return blink.cmp.Task
function source:resolve(item)
return async.task.new(function(resolve)
if self.module.resolve == nil then return resolve(nil) end
return self.module:resolve(item, function(resolved_item)
vim.schedule(function() resolve(resolved_item) end)
local tasks = self.resolve_tasks
if tasks[item] == nil or tasks[item].status == async.STATUS.CANCELLED then
tasks[item] = async.task.new(function(resolve)
if self.module.resolve == nil then return resolve(nil) end
return self.module:resolve(item, function(resolved_item)
-- use the item's existing documentation and detail if the LSP didn't return it
-- TODO: do we need this? this would be for java but never checked if it's needed
if resolved_item ~= nil and resolved_item.documentation == nil then
resolved_item.documentation = item.documentation
end
if resolved_item ~= nil and resolved_item.detail == nil then resolved_item.detail = item.detail end

vim.schedule(function() resolve(resolved_item or item) end)
end)
end)
end)
end
return tasks[item]
end

--- Signature help ---
Expand Down

0 comments on commit 83a8303

Please sign in to comment.