Skip to content

Commit

Permalink
fix: documentation delays
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 4, 2024
1 parent 3927e23 commit 01d5fd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ end
---------- Visibility ----------

function autocomplete.open_with_items(context, items)
autocomplete.context = context
autocomplete.items = items
autocomplete.draw()

Expand All @@ -77,7 +78,7 @@ function autocomplete.open_with_items(context, items)

-- todo: some logic to maintain the selection if the user moved the cursor?
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { 1, 0 })
autocomplete.event_targets.on_select(autocomplete.get_selected_item())
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), context)
end

function autocomplete.open()
Expand Down Expand Up @@ -144,7 +145,7 @@ function autocomplete.select_next()
if current_line == line_count then return end

vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { current_line + 1, 0 })
autocomplete.event_targets.on_select(autocomplete.get_selected_item())
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
end

function autocomplete.select_prev()
Expand All @@ -154,7 +155,7 @@ function autocomplete.select_prev()
if current_line == 1 then return end

vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { math.max(current_line - 1, 1), 0 })
autocomplete.event_targets.on_select(autocomplete.get_selected_item())
autocomplete.event_targets.on_select(autocomplete.get_selected_item(), autocomplete.context)
end

function autocomplete.listen_on_select(callback) autocomplete.event_targets.on_select = callback end
Expand Down
18 changes: 12 additions & 6 deletions lua/blink/cmp/windows/documentation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local config = require('blink.cmp.config').windows.documentation
local sources = require('blink.cmp.sources.lib')
local autocomplete = require('blink.cmp.windows.autocomplete')
local utils = require('blink.cmp.utils')
local docs = {}

function docs.setup()
Expand All @@ -18,12 +17,19 @@ function docs.setup()
if autocomplete.win:is_open() then docs.update_position() end
end)
if config.auto_show then
autocomplete.listen_on_select(function(item)
local delay = autocomplete.win:is_open() and config.update_delay_ms or config.auto_show_delay_ms
if delay == 0 then
return docs.show_item(item)
local timer = vim.uv.new_timer()
local last_context_id = nil
autocomplete.listen_on_select(function(item, context)
timer:stop()
if docs.win:is_open() or context.id == last_context_id then
timer:start(config.update_delay_ms, 0, function()
vim.schedule(function() docs.show_item(item) end)
end)
else
utils.debounce(docs.show_item, delay)(item)
timer:start(config.auto_show_delay_ms, 0, function()
last_context_id = context.id
vim.schedule(function() docs.show_item(item) end)
end)
end
end)
end
Expand Down

0 comments on commit 01d5fd0

Please sign in to comment.