This commit is contained in:
hrsh7th
2022-01-31 14:27:33 +09:00
parent f4bb6ffdb1
commit 0d23640fb6
2 changed files with 39 additions and 28 deletions

View File

@@ -29,6 +29,19 @@ cmp.event = cmp.core.event
---Export mapping ---Export mapping
cmp.mapping = require('cmp.config.mapping') cmp.mapping = require('cmp.config.mapping')
---Sync asynchronous process.
cmp.sync = function(callback)
return function(...)
cmp.core.filter:sync(1000)
return callback(...)
end
end
---Suspend completion.
cmp.suspend = function()
return cmp.core:suspend()
end
---Register completion sources ---Register completion sources
---@param name string ---@param name string
---@param s cmp.Source ---@param s cmp.Source
@@ -53,30 +66,30 @@ end
---Invoke completion manually ---Invoke completion manually
---@param option cmp.CompleteParams ---@param option cmp.CompleteParams
cmp.complete = function(option) cmp.complete = cmp.sync(function(option)
option = option or {} option = option or {}
config.set_onetime(option.config) config.set_onetime(option.config)
cmp.core:complete(cmp.core:get_context({ reason = option.reason or cmp.ContextReason.Manual })) cmp.core:complete(cmp.core:get_context({ reason = option.reason or cmp.ContextReason.Manual }))
return true return true
end end)
---Return view is visible or not. ---Return view is visible or not.
cmp.visible = function() cmp.visible = cmp.sync(function()
return cmp.core.view:visible() or vim.fn.pumvisible() == 1 return cmp.core.view:visible() or vim.fn.pumvisible() == 1
end end)
---Get current selected entry or nil ---Get current selected entry or nil
cmp.get_selected_entry = function() cmp.get_selected_entry = cmp.sync(function()
return cmp.core.view:get_selected_entry() return cmp.core.view:get_selected_entry()
end end)
---Get current active entry or nil ---Get current active entry or nil
cmp.get_active_entry = function() cmp.get_active_entry = cmp.sync(function()
return cmp.core.view:get_active_entry() return cmp.core.view:get_active_entry()
end end)
---Close current completion ---Close current completion
cmp.close = function() cmp.close = cmp.sync(function()
if cmp.core.view:visible() then if cmp.core.view:visible() then
local release = cmp.core:suspend() local release = cmp.core:suspend()
cmp.core.view:close() cmp.core.view:close()
@@ -86,10 +99,10 @@ cmp.close = function()
else else
return false return false
end end
end end)
---Abort current completion ---Abort current completion
cmp.abort = function() cmp.abort = cmp.sync(function()
if cmp.core.view:visible() then if cmp.core.view:visible() then
local release = cmp.core:suspend() local release = cmp.core:suspend()
cmp.core.view:abort() cmp.core.view:abort()
@@ -98,15 +111,10 @@ cmp.abort = function()
else else
return false return false
end end
end end)
---Suspend completion.
cmp.suspend = function()
return cmp.core:suspend()
end
---Select next item if possible ---Select next item if possible
cmp.select_next_item = function(option) cmp.select_next_item = cmp.sync(function(option)
option = option or {} option = option or {}
-- Hack: Ignore when executing macro. -- Hack: Ignore when executing macro.
@@ -128,10 +136,10 @@ cmp.select_next_item = function(option)
return true return true
end end
return false return false
end end)
---Select prev item if possible ---Select prev item if possible
cmp.select_prev_item = function(option) cmp.select_prev_item = cmp.sync(function(option)
option = option or {} option = option or {}
-- Hack: Ignore when executing macro. -- Hack: Ignore when executing macro.
@@ -153,20 +161,20 @@ cmp.select_prev_item = function(option)
return true return true
end end
return false return false
end end)
---Scrolling documentation window if possible ---Scrolling documentation window if possible
cmp.scroll_docs = function(delta) cmp.scroll_docs = cmp.sync(function(delta)
if cmp.core.view:visible() then if cmp.core.view:visible() then
cmp.core.view:scroll_docs(delta) cmp.core.view:scroll_docs(delta)
return true return true
else else
return false return false
end end
end end)
---Confirm completion ---Confirm completion
cmp.confirm = function(option, callback) cmp.confirm = cmp.sync(function(option, callback)
option = option or {} option = option or {}
callback = callback or function() end callback = callback or function() end
@@ -175,9 +183,6 @@ cmp.confirm = function(option, callback)
return true return true
end end
vim.wait(1000, function()
return not cmp.core.filter.running
end)
local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil) local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil)
if e then if e then
cmp.core:confirm(e, { cmp.core:confirm(e, {
@@ -194,7 +199,7 @@ cmp.confirm = function(option, callback)
end end
return false return false
end end
end end)
---Show status ---Show status
cmp.status = function() cmp.status = function()

View File

@@ -3,6 +3,7 @@ local async = {}
---@class cmp.AsyncThrottle ---@class cmp.AsyncThrottle
---@field public running boolean ---@field public running boolean
---@field public timeout number ---@field public timeout number
---@field public sync function(self: cmp.AsyncThrottle, timeout: number|nil)
---@field public stop function ---@field public stop function
---@field public __call function ---@field public __call function
@@ -15,6 +16,11 @@ async.throttle = function(fn, timeout)
return setmetatable({ return setmetatable({
running = false, running = false,
timeout = timeout, timeout = timeout,
sync = function(self, timeout_)
vim.wait(timeout_ or 1000, function()
return not self.running
end)
end,
stop = function() stop = function()
time = nil time = nil
timer:stop() timer:stop()