Improve source reset handling

This commit is contained in:
hrsh7th
2021-08-07 00:43:43 +09:00
parent 7e097da01f
commit 39c8abaeb3
4 changed files with 9 additions and 20 deletions

View File

@@ -76,16 +76,10 @@ core.on_keymap = function(keys, fallback)
if not e then if not e then
return fallback() return fallback()
end end
local pre = core.get_context()
core.confirm(e, { core.confirm(e, {
behavior = c.behavior, behavior = c.behavior,
}, function() }, function()
local new = core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }) core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
if new:changed(pre) then
core.complete(new)
else
core.reset()
end
end) end)
return return
end end
@@ -146,7 +140,7 @@ end
---Invoke completion ---Invoke completion
---@param ctx cmp.Context ---@param ctx cmp.Context
core.complete = function(ctx) core.complete = function(ctx)
for _, s in ipairs(core.get_sources({ source.SourceStatus.WAITING, source.SourceStatus.COMPLETED })) do for _, s in ipairs(core.get_sources()) do
s:complete(ctx, function() s:complete(ctx, function()
local new = context.new(ctx) local new = context.new(ctx)
if new:changed(new.prev_context) then if new:changed(new.prev_context) then

View File

@@ -106,7 +106,7 @@ float.close = async.throttle(
self.buf = nil self.buf = nil
self.win = nil self.win = nil
end), end),
0 20
) )
return float return float

View File

@@ -138,10 +138,6 @@ menu.update = function(self, ctx, sources)
self.preselect = preselect self.preselect = preselect
self.context = ctx self.context = ctx
self:show() self:show()
if #self.entries == 0 then
self:unselect()
end
end end
---Restore previous menu ---Restore previous menu
@@ -164,6 +160,7 @@ end
---Show completion item ---Show completion item
menu.show = function(self) menu.show = function(self)
if vim.fn.pumvisible() == 0 and #self.entries == 0 then if vim.fn.pumvisible() == 0 and #self.entries == 0 then
self:close()
return return
end end
@@ -174,11 +171,10 @@ menu.show = function(self)
vim.cmd('set completeopt=' .. config.get().completion.completeopt) vim.cmd('set completeopt=' .. config.get().completion.completeopt)
end end
vim.fn.complete(self.offset, self.items) vim.fn.complete(self.offset, self.items)
vim.cmd('set completeopt=' .. completeopt)
if self.preselect > 0 then if self.preselect > 0 then
vim.api.nvim_select_popupmenu_item(self.preselect - 1, false, false, {}) vim.api.nvim_select_popupmenu_item(self.preselect - 1, false, false, {})
end end
vim.cmd('set completeopt=' .. completeopt)
end end
---Select current item ---Select current item

View File

@@ -30,11 +30,6 @@ source.SourceStatus.WAITING = 1
source.SourceStatus.FETCHING = 2 source.SourceStatus.FETCHING = 2
source.SourceStatus.COMPLETED = 3 source.SourceStatus.COMPLETED = 3
---@alias cmp.SourceChangeKind "1" | "2" | "3"
source.SourceChangeKind = {}
source.SourceChangeKind.RETRIEVE = 1
source.SourceChangeKind.CONTINUE = 2
---@return cmp.Source ---@return cmp.Source
source.new = function(name, s) source.new = function(name, s)
local self = setmetatable({}, { __index = source }) local self = setmetatable({}, { __index = source })
@@ -51,6 +46,7 @@ end
---Reset current completion state ---Reset current completion state
---@return boolean ---@return boolean
source.reset = function(self) source.reset = function(self)
debug.log(self.id, self.name, 'source.reset')
self.cache:clear() self.cache:clear()
self.revision = self.revision + 1 self.revision = self.revision + 1
self.context = context.empty() self.context = context.empty()
@@ -242,6 +238,9 @@ source.complete = function(self, ctx, callback)
end end
if not completion_context then if not completion_context then
debug.log('skip empty context', self.name, self.id) debug.log('skip empty context', self.name, self.id)
if ctx:get_reason() == types.cmp.ContextReason.TriggerOnly then
self:reset()
end
return return
end end