summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/completion/prefetch.lua
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
committerMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
commitb77413ff8f59f380612074f0c9bd49093d8db695 (patch)
tree32c39a811ba96ed4ab0a1c81cce9f8d518ed7e31 /lua/blink/cmp/completion/prefetch.lua
Squashed 'mut/neovim/pack/plugins/start/blink.cmp/' content from commit 1cc3b1a
git-subtree-dir: mut/neovim/pack/plugins/start/blink.cmp git-subtree-split: 1cc3b1a908fbcfd15451c4772759549724f38524
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