@@ -24,6 +24,7 @@ end
|
|||||||
---Emit autocmd
|
---Emit autocmd
|
||||||
---@param event string
|
---@param event string
|
||||||
autocmd.emit = function(event)
|
autocmd.emit = function(event)
|
||||||
|
debug.log(' ')
|
||||||
debug.log(string.format('>>> %s', event))
|
debug.log(string.format('>>> %s', event))
|
||||||
autocmd.events[event] = autocmd.events[event] or {}
|
autocmd.events[event] = autocmd.events[event] or {}
|
||||||
for _, callback in ipairs(autocmd.events[event]) do
|
for _, callback in ipairs(autocmd.events[event]) do
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ core.SOURCE_TIMEOUT = 500
|
|||||||
core.menu = menu.new({
|
core.menu = menu.new({
|
||||||
on_select = function(e)
|
on_select = function(e)
|
||||||
for _, c in ipairs(e:get_commit_characters()) do
|
for _, c in ipairs(e:get_commit_characters()) do
|
||||||
keymap.listen(c, core.on_keymap)
|
keymap.listen('i', c, core.on_keymap)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@@ -102,8 +102,16 @@ end
|
|||||||
|
|
||||||
---Prepare completion
|
---Prepare completion
|
||||||
core.prepare = function()
|
core.prepare = function()
|
||||||
for keys in pairs(config.get().mapping) do
|
for keys, action in pairs(config.get().mapping) do
|
||||||
keymap.listen(keys, core.on_keymap)
|
if type(action) == 'function' then
|
||||||
|
action = {
|
||||||
|
modes = { 'i' },
|
||||||
|
action = action,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
for _, mode in ipairs(action.modes) do
|
||||||
|
keymap.listen(mode, keys, core.on_keymap)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,55 +2,66 @@ local types = require('cmp.types')
|
|||||||
|
|
||||||
local mapping = {}
|
local mapping = {}
|
||||||
|
|
||||||
mapping.complete = function()
|
mapping.mode = function(modes, action)
|
||||||
return function(core)
|
return setmetatable({
|
||||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
|
modes = modes or { 'i' },
|
||||||
|
action = action,
|
||||||
|
}, {
|
||||||
|
__call = function(_, ...)
|
||||||
|
action(...)
|
||||||
|
end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
mapping.complete = function()
|
||||||
|
return mapping.mode({ 'i' }, function(core)
|
||||||
|
core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
mapping.close = function()
|
mapping.close = function()
|
||||||
return function(core, fallback)
|
return mapping.mode({ 'i' }, function(core, fallback)
|
||||||
if vim.fn.pumvisible() == 1 then
|
if vim.fn.pumvisible() == 1 then
|
||||||
core.reset()
|
core.reset()
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
mapping.scroll = function(delta)
|
mapping.scroll = function(delta)
|
||||||
return function(core, fallback)
|
return mapping.mode({ 'i' }, function(core, fallback)
|
||||||
if core.menu.float:is_visible() then
|
if core.menu.float:is_visible() then
|
||||||
core.menu.float:scroll(delta)
|
core.menu.float:scroll(delta)
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
mapping.next_item = function()
|
mapping.next_item = function()
|
||||||
return function(_, fallback)
|
return mapping.mode({ 'i' }, function(_, fallback)
|
||||||
if vim.fn.pumvisible() == 1 then
|
if vim.fn.pumvisible() == 1 then
|
||||||
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n')
|
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n')
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
mapping.prev_item = function()
|
mapping.prev_item = function()
|
||||||
return function(_, fallback)
|
return mapping.mode({ 'i' }, function(_, fallback)
|
||||||
if vim.fn.pumvisible() == 1 then
|
if vim.fn.pumvisible() == 1 then
|
||||||
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-p>', true, true, true), 'n')
|
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-p>', true, true, true), 'n')
|
||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
mapping.confirm = function(option)
|
mapping.confirm = function(option)
|
||||||
option = option or {}
|
option = option or {}
|
||||||
return function(core, fallback)
|
return mapping.mode({ 'i' }, function(core, fallback)
|
||||||
local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
|
local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
|
||||||
if e then
|
if e then
|
||||||
core.confirm(e, {
|
core.confirm(e, {
|
||||||
@@ -61,7 +72,7 @@ mapping.confirm = function(option)
|
|||||||
else
|
else
|
||||||
fallback()
|
fallback()
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return mapping
|
return mapping
|
||||||
|
|||||||
@@ -74,8 +74,8 @@ menu.update = check.wrap(function(self, ctx, sources)
|
|||||||
-- check the source triggered by character
|
-- check the source triggered by character
|
||||||
local has_triggered_by_symbol_source = false
|
local has_triggered_by_symbol_source = false
|
||||||
for _, s in ipairs(sources) do
|
for _, s in ipairs(sources) do
|
||||||
if s:has_items() then
|
if #s:get_entries(ctx) > 0 then
|
||||||
if #s:get_entries(ctx) > 0 and s.is_triggered_by_symbol then
|
if s.is_triggered_by_symbol then
|
||||||
has_triggered_by_symbol_source = true
|
has_triggered_by_symbol_source = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -85,7 +85,7 @@ menu.update = check.wrap(function(self, ctx, sources)
|
|||||||
-- create filtered entries.
|
-- create filtered entries.
|
||||||
local offset = ctx.cursor.col
|
local offset = ctx.cursor.col
|
||||||
for i, s in ipairs(sources) do
|
for i, s in ipairs(sources) do
|
||||||
if s:has_items() and s.offset <= offset then
|
if s.offset <= offset then
|
||||||
if not has_triggered_by_symbol_source or s.is_triggered_by_symbol then
|
if not has_triggered_by_symbol_source or s.is_triggered_by_symbol then
|
||||||
-- source order priority bonus.
|
-- source order priority bonus.
|
||||||
local priority = (#sources - (i - 1)) * config.get().sorting.priority_weight
|
local priority = (#sources - (i - 1)) * config.get().sorting.priority_weight
|
||||||
|
|||||||
@@ -91,16 +91,16 @@ end)
|
|||||||
keymap.listen = setmetatable({
|
keymap.listen = setmetatable({
|
||||||
cache = cache.new(),
|
cache = cache.new(),
|
||||||
}, {
|
}, {
|
||||||
__call = function(_, keys, callback)
|
__call = function(_, mode, keys, callback)
|
||||||
keys = keymap.to_keymap(keys)
|
keys = keymap.to_keymap(keys)
|
||||||
|
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
if keymap.listen.cache:get({ bufnr, keys }) then
|
if keymap.listen.cache:get({ mode, bufnr, keys }) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local existing = nil
|
local existing = nil
|
||||||
for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, 'i')) do
|
for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do
|
||||||
if existing then
|
if existing then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -108,7 +108,7 @@ keymap.listen = setmetatable({
|
|||||||
existing = map
|
existing = map
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for _, map in ipairs(vim.api.nvim_get_keymap('i')) do
|
for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do
|
||||||
if existing then
|
if existing then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -125,11 +125,12 @@ keymap.listen = setmetatable({
|
|||||||
noremap = 1,
|
noremap = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
keymap.listen.cache:set({ bufnr, keys }, {
|
keymap.listen.cache:set({ mode, bufnr, keys }, {
|
||||||
|
mode = mode,
|
||||||
existing = existing,
|
existing = existing,
|
||||||
callback = callback,
|
callback = callback,
|
||||||
})
|
})
|
||||||
vim.api.nvim_buf_set_keymap(0, 'i', keys, ('v:lua.cmp.utils.keymap.expr("%s")'):format(keymap.escape(keys)), {
|
vim.api.nvim_buf_set_keymap(0, mode, keys, ('v:lua.cmp.utils.keymap.expr("%s", "%s")'):format(mode, keymap.escape(keys)), {
|
||||||
expr = true,
|
expr = true,
|
||||||
nowait = true,
|
nowait = true,
|
||||||
noremap = true,
|
noremap = true,
|
||||||
@@ -137,13 +138,13 @@ keymap.listen = setmetatable({
|
|||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
misc.set(_G, { 'cmp', 'utils', 'keymap', 'expr' }, function(keys)
|
misc.set(_G, { 'cmp', 'utils', 'keymap', 'expr' }, function(mode, keys)
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local existing = keymap.listen.cache:get({ bufnr, keys }).existing
|
local existing = keymap.listen.cache:get({ mode, bufnr, keys }).existing
|
||||||
local callback = keymap.listen.cache:get({ bufnr, keys }).callback
|
local callback = keymap.listen.cache:get({ mode, bufnr, keys }).callback
|
||||||
callback(keys, function()
|
callback(keys, function()
|
||||||
vim.api.nvim_buf_set_keymap(0, 'i', '<Plug>(cmp-utils-keymap:_)', existing.rhs, {
|
vim.api.nvim_buf_set_keymap(0, mode, '<Plug>(cmp-utils-keymap:_)', existing.rhs, {
|
||||||
expr = existing.expr ~= 0,
|
expr = existing.expr ~= 0,
|
||||||
noremap = existing.noremap ~= 0,
|
noremap = existing.noremap ~= 0,
|
||||||
script = existing.script ~= 0,
|
script = existing.script ~= 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user