Update mapping configuration

This commit is contained in:
hrsh7th
2021-08-11 23:24:47 +09:00
parent 4c9462c28c
commit c2f872920e
8 changed files with 196 additions and 88 deletions

View File

@@ -33,6 +33,20 @@ lua <<EOF
vim.fn['vsnip#anonymous'](args.body) vim.fn['vsnip#anonymous'](args.body)
end end
}, },
-- 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-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
})
},
-- You should specify your *installed* sources. -- You should specify your *installed* sources.
sources = { sources = {
@@ -56,23 +70,40 @@ The default configuration can be found in [here](./lua/cmp/config/default.lua)
You can use your own configuration like this: You can use your own configuration like this:
```lua ```lua
require'cmp'.setup { local cmp = require'cmp'
... cmp.setup {
completion = { ...
autocomplete = { .. }, completion = {
completeopt = 'menu,menuone,noselect', autocomplete = { .. },
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]], completeopt = 'menu,menuone,noselect',
keyword_length = 1, keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
}, keyword_length = 1,
sorting = { },
priority_weight = 2., sorting = {
comparators = { ... }, priority_weight = 2.,
}, comparators = { ... },
sources = { ... }, },
... 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-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
})
},
sources = { ... },
...
} }
``` ```
### mapping (type: table<string, cmp.MappingConfig>)
Define mappings with `cmp.mapping` helper.
### completion.autocomplete (type: cmp.TriggerEvent[]) ### completion.autocomplete (type: cmp.TriggerEvent[])
Which events should trigger `autocompletion`. Which events should trigger `autocompletion`.
@@ -124,13 +155,13 @@ Each function must return `boolean|nil`.
Default: Default:
```lua ```lua
{ {
compare.offset, compare.offset,
compare.exact, compare.exact,
compare.score, compare.score,
compare.kind, compare.kind,
compare.sort_text, compare.sort_text,
compare.length, compare.length,
compare.order, compare.order,
} }
``` ```

View File

@@ -29,20 +29,10 @@ return function()
winhighlight = 'NormalFloat:CmpDocumentation,FloatBorder:CmpDocumentationBorder', winhighlight = 'NormalFloat:CmpDocumentation,FloatBorder:CmpDocumentationBorder',
maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))), maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)), maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
mapping = {
['<C-d>'] = types.cmp.ScrollDirection.Up,
['<C-f>'] = types.cmp.ScrollDirection.Down,
}
}, },
confirmation = { confirmation = {
default_behavior = types.cmp.ConfirmBehavior.Replace, default_behavior = types.cmp.ConfirmBehavior.Replace,
mapping = {
['<CR>'] = {
behavior = types.cmp.ConfirmBehavior.Replace,
select = true,
},
}
}, },
sorting = { sorting = {
@@ -58,6 +48,8 @@ return function()
} }
}, },
mapping = {},
formatting = { formatting = {
format = function(e, suggest_offset) format = function(e, suggest_offset)
local item = e:get_completion_item() local item = e:get_completion_item()

View File

@@ -71,29 +71,49 @@ end
---Keypress handler ---Keypress handler
core.on_keymap = function(keys, fallback) core.on_keymap = function(keys, fallback)
-- Documentation scroll for key, c in pairs(config.get().mapping) do
if config.get().documentation.mapping[keys] then if key == keys then
if config.get().documentation.mapping[keys] == types.cmp.ScrollDirection.Up then if c.type == 'confirm' then
core.menu.float:scroll(-4) local e = core.menu:get_selected_entry() or (c.select and core.menu:get_first_entry())
elseif config.get().documentation.mapping[keys] == types.cmp.ScrollDirection.Down then if e then
core.menu.float:scroll(4) core.confirm(e, {
end behavior = c.behavior,
return }, function()
end core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
end)
-- Confirm character return
if config.get().confirmation.mapping[keys] then end
local c = config.get().confirmation.mapping[keys] elseif c.type == 'complete' then
local e = core.menu:get_selected_entry() or (c.select and core.menu:get_first_entry()) core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
if not e then 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 fallback()
end end
core.confirm(e, {
behavior = c.behavior,
}, function()
core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
end)
return
end end
--Commit character. NOTE: This has a lot of cmp specific implementation to make more user-friendly. --Commit character. NOTE: This has a lot of cmp specific implementation to make more user-friendly.
@@ -101,7 +121,7 @@ core.on_keymap = function(keys, fallback)
local e = core.menu:get_selected_entry() local e = core.menu:get_selected_entry()
if e and vim.tbl_contains(e:get_commit_characters(), chars) then if e and vim.tbl_contains(e:get_commit_characters(), chars) then
local is_printable = char.is_printable(string.byte(chars, 1)) local is_printable = char.is_printable(string.byte(chars, 1))
return core.confirm(e, { core.confirm(e, {
behavior = is_printable and 'insert' or 'replace', behavior = is_printable and 'insert' or 'replace',
}, function() }, function()
local ctx = core.get_context() local ctx = core.get_context()
@@ -112,6 +132,7 @@ core.on_keymap = function(keys, fallback)
core.reset() core.reset()
end end
end) end)
return
end end
fallback() fallback()
@@ -119,10 +140,7 @@ end
---Prepare completion ---Prepare completion
core.prepare = function() core.prepare = function()
for keys in pairs(config.get().confirmation.mapping) do for keys in pairs(config.get().mapping) do
keymap.listen(keys, core.on_keymap)
end
for keys in pairs(config.get().documentation.mapping) do
keymap.listen(keys, core.on_keymap) keymap.listen(keys, core.on_keymap)
end end
end end

View File

@@ -102,7 +102,7 @@ end
---Close floating window ---Close floating window
float.close = async.throttle( float.close = async.throttle(
vim.schedule_wrap(function(self) vim.schedule_wrap(function(self)
if self.win and vim.api.nvim_win_is_valid(self.win) then if self:is_visible() then
vim.api.nvim_win_close(self.win, true) vim.api.nvim_win_close(self.win, true)
end end
self.entry = nil self.entry = nil
@@ -113,7 +113,7 @@ float.close = async.throttle(
) )
float.scroll = function(self, delta) float.scroll = function(self, delta)
if self.win and vim.api.nvim_win_is_valid(self.win) then if self:is_visible() then
local info = vim.fn.getwininfo(self.win)[1] or {} local info = vim.fn.getwininfo(self.win)[1] or {}
local buf = vim.api.nvim_win_get_buf(self.win) local buf = vim.api.nvim_win_get_buf(self.win)
local top = info.topline or 1 local top = info.topline or 1
@@ -129,4 +129,8 @@ float.scroll = function(self, delta)
end end
end end
float.is_visible = function(self)
return self.win and vim.api.nvim_win_is_valid(self.win)
end
return float return float

View File

@@ -1,5 +1,4 @@
local core = require('cmp.core') local core = require('cmp.core')
local types = require('cmp.types')
local source = require('cmp.source') local source = require('cmp.source')
local config = require('cmp.config') local config = require('cmp.config')
local autocmd = require('cmp.autocmd') local autocmd = require('cmp.autocmd')
@@ -13,6 +12,9 @@ end
cmp.lsp = require('cmp.types.lsp') cmp.lsp = require('cmp.types.lsp')
cmp.vim = require('cmp.types.vim') cmp.vim = require('cmp.types.vim')
---Export mapping
cmp.mapping = require('cmp.mapping')
---Register completion sources ---Register completion sources
---@param name string ---@param name string
---@param s cmp.Source ---@param s cmp.Source
@@ -43,18 +45,6 @@ cmp.setup = setmetatable({
end, end,
}) })
---Invoke completion manually
cmp.complete = function()
core.complete(core.get_context({
reason = types.cmp.ContextReason.Manual,
}))
end
---Close completion
cmp.close = function()
core.reset()
end
---Handle events ---Handle events
autocmd.subscribe('InsertEnter', function() autocmd.subscribe('InsertEnter', function()
core.prepare() core.prepare()

59
lua/cmp/mapping.lua Normal file
View File

@@ -0,0 +1,59 @@
local types = require('cmp.types')
local mapping = {}
---Create complete mapping
mapping.complete = function()
return {
type = 'complete',
}
end
---Create close mapping
mapping.close = function()
return {
type = 'close',
}
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,
}
---Create item mapping
mapping.item = {
prev = function()
return {
type = 'item.prev',
}
end,
next = function()
return {
type = 'item.next',
}
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,
}
end
return mapping

View File

@@ -51,6 +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 sources cmp.SourceConfig[] ---@field public sources cmp.SourceConfig[]
---@class cmp.CompletionConfig ---@class cmp.CompletionConfig
@@ -64,15 +65,9 @@ cmp.ScrollDirection.Down = 'down'
---@field public winhighlight string ---@field public winhighlight string
---@field public maxwidth number|nil ---@field public maxwidth number|nil
---@field public maxheight number|nil ---@field public maxheight number|nil
---@field public mapping table<string, cmp.ScrollDirection>
---@class cmp.ConfirmationConfig ---@class cmp.ConfirmationConfig
---@field public default_behavior cmp.ConfirmBehavior ---@field public default_behavior cmp.ConfirmBehavior
---@field public mapping table<string, cmp.ConfirmMappingConfig>
---@class cmp.ConfirmMappingConfig
---@field behavior cmp.ConfirmBehavior
---@field select boolean
---@class cmp.SortingConfig ---@class cmp.SortingConfig
---@field public priority_weight number ---@field public priority_weight number
@@ -88,5 +83,34 @@ 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

View File

@@ -26,13 +26,3 @@ if vim.fn.hlexists('CmpDocumentationBorder') == 0 then
vim.cmd [[highlight link CmpDocumentationBorder NormalFloat]] vim.cmd [[highlight link CmpDocumentationBorder NormalFloat]]
end end
misc.set(_G, { 'cmp', 'complete' }, function()
cmp.complete()
return vim.api.nvim_replace_termcodes('<Ignore>', true, true, true)
end)
misc.set(_G, { 'cmp', 'close' }, function()
cmp.close()
return vim.api.nvim_replace_termcodes('<Ignore>', true, true, true)
end)