Improve mappings
This commit is contained in:
29
README.md
29
README.md
@@ -36,10 +36,10 @@ lua <<EOF
|
||||
|
||||
-- You must set mapping.
|
||||
mapping = {
|
||||
['<C-p>'] = cmp.mapping.item.prev(),
|
||||
['<C-n>'] = cmp.mapping.item.next(),
|
||||
['<C-d>'] = cmp.mapping.scroll.up(),
|
||||
['<C-f>'] = cmp.mapping.scroll.down(),
|
||||
['<C-p>'] = cmp.mapping.prev_item(),
|
||||
['<C-n>'] = cmp.mapping.next_item(),
|
||||
['<C-d>'] = cmp.mapping.scroll(-4),
|
||||
['<C-f>'] = cmp.mapping.scroll(4),
|
||||
['<C-Space>'] = cmp.mapping.complete(),
|
||||
['<C-e>'] = cmp.mapping.close(),
|
||||
['<CR>'] = cmp.mapping.confirm({
|
||||
@@ -84,10 +84,10 @@ cmp.setup {
|
||||
comparators = { ... },
|
||||
},
|
||||
mapping = {
|
||||
['<C-p>'] = cmp.mapping.item.prev(),
|
||||
['<C-n>'] = cmp.mapping.item.next(),
|
||||
['<C-d>'] = cmp.mapping.scroll.up(),
|
||||
['<C-f>'] = cmp.mapping.scroll.down(),
|
||||
['<C-p>'] = cmp.mapping.prev_item(),
|
||||
['<C-n>'] = cmp.mapping.next_item(),
|
||||
['<C-d>'] = cmp.mapping.scroll(-4),
|
||||
['<C-f>'] = cmp.mapping.scroll(4),
|
||||
['<C-Space>'] = cmp.mapping.complete(),
|
||||
['<C-e>'] = cmp.mapping.close(),
|
||||
['<CR>'] = cmp.mapping.confirm({
|
||||
@@ -106,13 +106,12 @@ Define mappings with `cmp.mapping` helper.
|
||||
|
||||
The `cmp.mapping` helper has the below functions.
|
||||
|
||||
*cmp.mapping.confirm({ select = true or false, behavior = cmp.ConfirmBehavior.Insert or cmp.ConfirmBehavior.Replace })*
|
||||
*cmp.mapping.complete()*
|
||||
*cmp.mapping.close()*
|
||||
*cmp.mapping.item.next()*
|
||||
*cmp.mapping.item.prev()*
|
||||
*cmp.mapping.scroll.up()*
|
||||
*cmp.mapping.scroll.down()*
|
||||
- *cmp.mapping.confirm({ select = true or false, behavior = cmp.ConfirmBehavior.Insert or cmp.ConfirmBehavior.Replace })*
|
||||
- *cmp.mapping.complete()*
|
||||
- *cmp.mapping.close()*
|
||||
- *cmp.mapping.next_item()*
|
||||
- *cmp.mapping.prev_item()*
|
||||
- *cmp.mapping.scroll(delta = number)*
|
||||
|
||||
### completion.autocomplete (type: cmp.TriggerEvent[])
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ local misc = require('cmp.utils.misc')
|
||||
local config = require('cmp.config')
|
||||
local types = require('cmp.types')
|
||||
|
||||
---@class cmp.Core
|
||||
local core = {}
|
||||
|
||||
core.SOURCE_TIMEOUT = 500
|
||||
@@ -71,48 +72,9 @@ end
|
||||
|
||||
---Keypress handler
|
||||
core.on_keymap = function(keys, fallback)
|
||||
for key, c in pairs(config.get().mapping) do
|
||||
for key, action in pairs(config.get().mapping) do
|
||||
if key == keys then
|
||||
if c.type == 'confirm' then
|
||||
local e = core.menu:get_selected_entry() or (c.select and core.menu:get_first_entry())
|
||||
if e then
|
||||
core.confirm(e, {
|
||||
behavior = c.behavior,
|
||||
}, function()
|
||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
|
||||
end)
|
||||
return
|
||||
end
|
||||
elseif c.type == 'complete' then
|
||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
|
||||
return
|
||||
elseif c.type == 'close' then
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
core.reset()
|
||||
return
|
||||
end
|
||||
elseif c.type == 'scroll.up' then
|
||||
if core.menu.float:is_visible() then
|
||||
core.menu.float:scroll(-c.delta)
|
||||
return
|
||||
end
|
||||
elseif c.type == 'scroll.down' then
|
||||
if core.menu.float:is_visible() then
|
||||
core.menu.float:scroll(c.delta)
|
||||
return
|
||||
end
|
||||
elseif c.type == 'item.next' then
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
keymap.feedkeys('<C-n>', 'n')
|
||||
return
|
||||
end
|
||||
elseif c.type == 'item.prev' then
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
keymap.feedkeys('<C-p>', 'n')
|
||||
return
|
||||
end
|
||||
end
|
||||
return fallback()
|
||||
return action(core, fallback)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,58 +2,66 @@ local types = require('cmp.types')
|
||||
|
||||
local mapping = {}
|
||||
|
||||
---Create complete mapping
|
||||
mapping.complete = function()
|
||||
return {
|
||||
type = 'complete',
|
||||
}
|
||||
return function(core)
|
||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
|
||||
end
|
||||
end
|
||||
|
||||
---Create close mapping
|
||||
mapping.close = function()
|
||||
return {
|
||||
type = 'close',
|
||||
}
|
||||
return function(core, fallback)
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
core.close()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Create scroll mapping
|
||||
mapping.scroll = {
|
||||
up = function(delta)
|
||||
return {
|
||||
type = 'scroll.up',
|
||||
delta = delta or 4,
|
||||
}
|
||||
end,
|
||||
down = function(delta)
|
||||
return {
|
||||
type = 'scroll.down',
|
||||
delta = delta or 4,
|
||||
}
|
||||
end,
|
||||
}
|
||||
mapping.scroll = function(delta)
|
||||
return function(core, fallback)
|
||||
if core.menu.float:is_visible() then
|
||||
core.menu.float:scroll(delta)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Create item mapping
|
||||
mapping.item = {
|
||||
prev = function()
|
||||
return {
|
||||
type = 'item.prev',
|
||||
}
|
||||
end,
|
||||
next = function()
|
||||
return {
|
||||
type = 'item.next',
|
||||
}
|
||||
end,
|
||||
}
|
||||
mapping.next_item = function()
|
||||
return function(_, fallback)
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', 'n'))
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
mapping.prev_item = function()
|
||||
return function(_, fallback)
|
||||
if vim.fn.pumvisible() == 1 then
|
||||
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-p>', 'n'))
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Create confirm mapping
|
||||
mapping.confirm = function(option)
|
||||
option = option or {}
|
||||
return {
|
||||
type = 'confirm',
|
||||
select = option.select or false,
|
||||
behavior = option.behavior or types.cmp.ConfirmBehavior.Insert,
|
||||
}
|
||||
return function(core, fallback)
|
||||
local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
|
||||
if e then
|
||||
core.confirm(e, {
|
||||
behavior = option.behavior,
|
||||
}, function()
|
||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
|
||||
end)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return mapping
|
||||
|
||||
@@ -51,7 +51,7 @@ cmp.ScrollDirection.Down = 'down'
|
||||
---@field public sorting cmp.SortingConfig
|
||||
---@field public formatting cmp.FormattingConfig
|
||||
---@field public snippet cmp.SnippetConfig
|
||||
---@field public mapping table<string, cmp.MappingConfig>
|
||||
---@field public mapping table<string, fun(core: cmp.Core, fallback: function)>
|
||||
---@field public sources cmp.SourceConfig[]
|
||||
|
||||
---@class cmp.CompletionConfig
|
||||
@@ -83,34 +83,5 @@ cmp.ScrollDirection.Down = 'down'
|
||||
---@field public name string
|
||||
---@field public opts table
|
||||
|
||||
---@alias cmp.MappingConfig cmp.ConfirmMapping | cmp.CompleteMapping | cmp.CloseMapping | cmp.ItemNextMapping | cmp.ItemPrevMapping | cmp.ScrollUpMapping | cmp.ScrollDownMapping
|
||||
|
||||
---@class cmp.ConfirmMapping
|
||||
---@field public type '"confirm"'
|
||||
---@field public select boolean
|
||||
---@field public behavior cmp.ConfirmBehavior
|
||||
|
||||
---@class cmp.CompleteMapping
|
||||
---@field public type '"complete"'
|
||||
|
||||
---@class cmp.CloseMapping
|
||||
---@field public type '"close"'
|
||||
|
||||
---@class cmp.ItemNextMapping
|
||||
---@field public type '"item.next"'
|
||||
---@field public delta number
|
||||
|
||||
---@class cmp.ItemPrevMapping
|
||||
---@field public type '"item.prev"'
|
||||
---@field public delta number
|
||||
|
||||
---@class cmp.ScrollUpMapping
|
||||
---@field public type '"scroll.up"'
|
||||
---@field public delta number
|
||||
|
||||
---@class cmp.ScrollDownMapping
|
||||
---@field public type '"scroll.down"'
|
||||
---@field public delta number
|
||||
|
||||
return cmp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user