summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/completion/prefetch.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/blink/cmp/completion/prefetch.lua')
-rw-r--r--lua/blink/cmp/completion/prefetch.lua29
1 files changed, 29 insertions, 0 deletions
diff --git a/lua/blink/cmp/completion/prefetch.lua b/lua/blink/cmp/completion/prefetch.lua
new file mode 100644
index 0000000..c722a30
--- /dev/null
+++ b/lua/blink/cmp/completion/prefetch.lua
@@ -0,0 +1,29 @@
+-- Run `resolve` on the item ahead of time to avoid delays
+-- when accepting the item or showing documentation
+
+local last_context_id = nil
+local last_request = nil
+local timer = vim.uv.new_timer()
+
+--- @param context blink.cmp.Context
+--- @param item blink.cmp.CompletionItem
+local function prefetch_resolve(context, item)
+ if not item then return end
+
+ local resolve = vim.schedule_wrap(function()
+ if last_request ~= nil then last_request:cancel() end
+ last_request = require('blink.cmp.sources.lib').resolve(context, item)
+ end)
+
+ -- immediately resolve if the context has changed
+ if last_context_id ~= context.id then
+ last_context_id = context.id
+ resolve()
+ end
+
+ -- otherwise, wait for the debounce period
+ timer:stop()
+ timer:start(50, 0, resolve)
+end
+
+return prefetch_resolve