Add basic recently_used comparator
This commit is contained in:
@@ -360,6 +360,7 @@ Default:
|
|||||||
cmp.config.compare.offset,
|
cmp.config.compare.offset,
|
||||||
cmp.config.compare.exact,
|
cmp.config.compare.exact,
|
||||||
cmp.config.compare.score,
|
cmp.config.compare.score,
|
||||||
|
cmp.config.compare.recently_used,
|
||||||
cmp.config.compare.kind,
|
cmp.config.compare.kind,
|
||||||
cmp.config.compare.sort_text,
|
cmp.config.compare.sort_text,
|
||||||
cmp.config.compare.length,
|
cmp.config.compare.length,
|
||||||
@@ -478,6 +479,10 @@ Subscribe the following events.
|
|||||||
|
|
||||||
- `confirm_done`
|
- `confirm_done`
|
||||||
|
|
||||||
|
#### `cmp.get_config()`
|
||||||
|
|
||||||
|
Return the current configuration.
|
||||||
|
|
||||||
#### `cmp.visible()`
|
#### `cmp.visible()`
|
||||||
|
|
||||||
Return the completion menu is visible or not.
|
Return the completion menu is visible or not.
|
||||||
|
|||||||
@@ -30,6 +30,22 @@ compare.score = function(entry1, entry2)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- recently_used
|
||||||
|
compare.recently_used = setmetatable({
|
||||||
|
records = {},
|
||||||
|
add_entry = function(self, e)
|
||||||
|
self.records[e.completion_item.label] = vim.loop.now()
|
||||||
|
end
|
||||||
|
}, {
|
||||||
|
__call = function(self, entry1, entry2)
|
||||||
|
local t1 = self.records[entry1.completion_item.label] or -1
|
||||||
|
local t2 = self.records[entry2.completion_item.label] or -1
|
||||||
|
if t1 ~= t2 then
|
||||||
|
return t1 > t2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
-- kind
|
-- kind
|
||||||
compare.kind = function(entry1, entry2)
|
compare.kind = function(entry1, entry2)
|
||||||
local kind1 = entry1:get_kind()
|
local kind1 = entry1:get_kind()
|
||||||
|
|||||||
@@ -47,34 +47,14 @@ return function()
|
|||||||
sorting = {
|
sorting = {
|
||||||
priority_weight = 2,
|
priority_weight = 2,
|
||||||
comparators = {
|
comparators = {
|
||||||
function(e1, e2)
|
compare.offset,
|
||||||
local diff
|
compare.exact,
|
||||||
diff = compare.offset(e1, e2)
|
compare.score,
|
||||||
if diff ~= nil then
|
compare.recently_used,
|
||||||
return diff
|
compare.kind,
|
||||||
end
|
compare.sort_text,
|
||||||
diff = compare.exact(e1, e2)
|
compare.length,
|
||||||
if diff ~= nil then
|
compare.order,
|
||||||
return diff
|
|
||||||
end
|
|
||||||
diff = compare.score(e1, e2)
|
|
||||||
if diff ~= nil then
|
|
||||||
return diff
|
|
||||||
end
|
|
||||||
diff = compare.kind(e1, e2)
|
|
||||||
if diff ~= nil then
|
|
||||||
return diff
|
|
||||||
end
|
|
||||||
diff = compare.sort_text(e1, e2)
|
|
||||||
if diff ~= nil then
|
|
||||||
return diff
|
|
||||||
end
|
|
||||||
diff = compare.length(e1, e2)
|
|
||||||
if diff ~= nil then
|
|
||||||
return diff
|
|
||||||
end
|
|
||||||
return compare.order(e1, e2)
|
|
||||||
end,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -102,7 +82,7 @@ return function()
|
|||||||
['<Tab>'] = mapping({
|
['<Tab>'] = mapping({
|
||||||
c = function(fallback)
|
c = function(fallback)
|
||||||
local cmp = require('cmp')
|
local cmp = require('cmp')
|
||||||
if #cmp.core:get_sources() > 0 and not require('cmp.config').get().experimental.native_menu then
|
if #cmp.core:get_sources() > 0 and not cmp.get_config().experimental.native_menu then
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_next_item()
|
cmp.select_next_item()
|
||||||
else
|
else
|
||||||
@@ -116,7 +96,7 @@ return function()
|
|||||||
['<S-Tab>'] = mapping({
|
['<S-Tab>'] = mapping({
|
||||||
c = function(fallback)
|
c = function(fallback)
|
||||||
local cmp = require('cmp')
|
local cmp = require('cmp')
|
||||||
if #cmp.core:get_sources() > 0 and not require('cmp.config').get().experimental.native_menu then
|
if #cmp.core:get_sources() > 0 and not cmp.get_config().experimental.native_menu then
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_prev_item()
|
cmp.select_prev_item()
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -44,6 +44,12 @@ cmp.unregister_source = function(id)
|
|||||||
cmp.core:unregister_source(id)
|
cmp.core:unregister_source(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Get current configuration.
|
||||||
|
---@return cmp.ConfigSchema
|
||||||
|
cmp.get_config = function()
|
||||||
|
return require('cmp.config').get()
|
||||||
|
end
|
||||||
|
|
||||||
---Invoke completion manually
|
---Invoke completion manually
|
||||||
cmp.complete = function()
|
cmp.complete = function()
|
||||||
cmp.core:complete(cmp.core:get_context({ reason = cmp.ContextReason.Manual }))
|
cmp.core:complete(cmp.core:get_context({ reason = cmp.ContextReason.Manual }))
|
||||||
@@ -262,7 +268,6 @@ cmp.setup = setmetatable({
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
---Handle events
|
|
||||||
autocmd.subscribe('InsertEnter', function()
|
autocmd.subscribe('InsertEnter', function()
|
||||||
-- Avoid unexpected mode detection (mode() function will returns `normal mode` on the InsertEnter event.)
|
-- Avoid unexpected mode detection (mode() function will returns `normal mode` on the InsertEnter event.)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
@@ -290,4 +295,8 @@ autocmd.subscribe('InsertLeave', function()
|
|||||||
cmp.core.view:close()
|
cmp.core.view:close()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
cmp.event:on('confirm_done', function(e)
|
||||||
|
cmp.config.compare.recently_used:add_entry(e)
|
||||||
|
end)
|
||||||
|
|
||||||
return cmp
|
return cmp
|
||||||
|
|||||||
Reference in New Issue
Block a user