Improve mappings

This commit is contained in:
hrsh7th
2021-08-12 16:14:23 +09:00
parent 04af63f742
commit 22f8ce1bd5
4 changed files with 68 additions and 128 deletions

View File

@@ -36,10 +36,10 @@ lua <<EOF
-- You must set mapping. -- You must set mapping.
mapping = { mapping = {
['<C-p>'] = cmp.mapping.item.prev(), ['<C-p>'] = cmp.mapping.prev_item(),
['<C-n>'] = cmp.mapping.item.next(), ['<C-n>'] = cmp.mapping.next_item(),
['<C-d>'] = cmp.mapping.scroll.up(), ['<C-d>'] = cmp.mapping.scroll(-4),
['<C-f>'] = cmp.mapping.scroll.down(), ['<C-f>'] = cmp.mapping.scroll(4),
['<C-Space>'] = cmp.mapping.complete(), ['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(), ['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({ ['<CR>'] = cmp.mapping.confirm({
@@ -84,10 +84,10 @@ cmp.setup {
comparators = { ... }, comparators = { ... },
}, },
mapping = { mapping = {
['<C-p>'] = cmp.mapping.item.prev(), ['<C-p>'] = cmp.mapping.prev_item(),
['<C-n>'] = cmp.mapping.item.next(), ['<C-n>'] = cmp.mapping.next_item(),
['<C-d>'] = cmp.mapping.scroll.up(), ['<C-d>'] = cmp.mapping.scroll(-4),
['<C-f>'] = cmp.mapping.scroll.down(), ['<C-f>'] = cmp.mapping.scroll(4),
['<C-Space>'] = cmp.mapping.complete(), ['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(), ['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({ ['<CR>'] = cmp.mapping.confirm({
@@ -106,13 +106,12 @@ Define mappings with `cmp.mapping` helper.
The `cmp.mapping` helper has the below functions. 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.confirm({ select = true or false, behavior = cmp.ConfirmBehavior.Insert or cmp.ConfirmBehavior.Replace })*
*cmp.mapping.complete()* - *cmp.mapping.complete()*
*cmp.mapping.close()* - *cmp.mapping.close()*
*cmp.mapping.item.next()* - *cmp.mapping.next_item()*
*cmp.mapping.item.prev()* - *cmp.mapping.prev_item()*
*cmp.mapping.scroll.up()* - *cmp.mapping.scroll(delta = number)*
*cmp.mapping.scroll.down()*
### completion.autocomplete (type: cmp.TriggerEvent[]) ### completion.autocomplete (type: cmp.TriggerEvent[])

View File

@@ -9,6 +9,7 @@ local misc = require('cmp.utils.misc')
local config = require('cmp.config') local config = require('cmp.config')
local types = require('cmp.types') local types = require('cmp.types')
---@class cmp.Core
local core = {} local core = {}
core.SOURCE_TIMEOUT = 500 core.SOURCE_TIMEOUT = 500
@@ -71,48 +72,9 @@ end
---Keypress handler ---Keypress handler
core.on_keymap = function(keys, fallback) 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 key == keys then
if c.type == 'confirm' then return action(core, fallback)
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()
end end
end end

View File

@@ -2,58 +2,66 @@ local types = require('cmp.types')
local mapping = {} local mapping = {}
---Create complete mapping
mapping.complete = function() mapping.complete = function()
return { return function(core)
type = 'complete', core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
} end
end end
---Create close mapping
mapping.close = function() mapping.close = function()
return { return function(core, fallback)
type = 'close', if vim.fn.pumvisible() == 1 then
} core.close()
else
fallback()
end
end
end end
---Create scroll mapping mapping.scroll = function(delta)
mapping.scroll = { return function(core, fallback)
up = function(delta) if core.menu.float:is_visible() then
return { core.menu.float:scroll(delta)
type = 'scroll.up', else
delta = delta or 4, fallback()
} end
end, end
down = function(delta) end
return {
type = 'scroll.down',
delta = delta or 4,
}
end,
}
---Create item mapping mapping.next_item = function()
mapping.item = { return function(_, fallback)
prev = function() if vim.fn.pumvisible() == 1 then
return { vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', 'n'))
type = 'item.prev', else
} fallback()
end, end
next = function() end
return { end
type = 'item.next',
} mapping.prev_item = function()
end, 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) mapping.confirm = function(option)
option = option or {} option = option or {}
return { return function(core, fallback)
type = 'confirm', local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
select = option.select or false, if e then
behavior = option.behavior or types.cmp.ConfirmBehavior.Insert, core.confirm(e, {
} behavior = option.behavior,
}, function()
core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
end)
else
fallback()
end
end
end end
return mapping return mapping

View File

@@ -51,7 +51,7 @@ cmp.ScrollDirection.Down = 'down'
---@field public sorting cmp.SortingConfig ---@field public sorting cmp.SortingConfig
---@field public formatting cmp.FormattingConfig ---@field public formatting cmp.FormattingConfig
---@field public snippet cmp.SnippetConfig ---@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[] ---@field public sources cmp.SourceConfig[]
---@class cmp.CompletionConfig ---@class cmp.CompletionConfig
@@ -83,34 +83,5 @@ cmp.ScrollDirection.Down = 'down'
---@field public name string ---@field public name string
---@field public opts table ---@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 return cmp