Update mapping configuration
This commit is contained in:
73
README.md
73
README.md
@@ -33,6 +33,20 @@ lua <<EOF
|
||||
vim.fn['vsnip#anonymous'](args.body)
|
||||
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.
|
||||
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:
|
||||
```lua
|
||||
require'cmp'.setup {
|
||||
...
|
||||
completion = {
|
||||
autocomplete = { .. },
|
||||
completeopt = 'menu,menuone,noselect',
|
||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
|
||||
keyword_length = 1,
|
||||
},
|
||||
sorting = {
|
||||
priority_weight = 2.,
|
||||
comparators = { ... },
|
||||
},
|
||||
sources = { ... },
|
||||
...
|
||||
local cmp = require'cmp'
|
||||
cmp.setup {
|
||||
...
|
||||
completion = {
|
||||
autocomplete = { .. },
|
||||
completeopt = 'menu,menuone,noselect',
|
||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)]],
|
||||
keyword_length = 1,
|
||||
},
|
||||
sorting = {
|
||||
priority_weight = 2.,
|
||||
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-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[])
|
||||
|
||||
Which events should trigger `autocompletion`.
|
||||
@@ -124,13 +155,13 @@ Each function must return `boolean|nil`.
|
||||
Default:
|
||||
```lua
|
||||
{
|
||||
compare.offset,
|
||||
compare.exact,
|
||||
compare.score,
|
||||
compare.kind,
|
||||
compare.sort_text,
|
||||
compare.length,
|
||||
compare.order,
|
||||
compare.offset,
|
||||
compare.exact,
|
||||
compare.score,
|
||||
compare.kind,
|
||||
compare.sort_text,
|
||||
compare.length,
|
||||
compare.order,
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -29,20 +29,10 @@ return function()
|
||||
winhighlight = 'NormalFloat:CmpDocumentation,FloatBorder:CmpDocumentationBorder',
|
||||
maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
|
||||
maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
|
||||
mapping = {
|
||||
['<C-d>'] = types.cmp.ScrollDirection.Up,
|
||||
['<C-f>'] = types.cmp.ScrollDirection.Down,
|
||||
}
|
||||
},
|
||||
|
||||
confirmation = {
|
||||
default_behavior = types.cmp.ConfirmBehavior.Replace,
|
||||
mapping = {
|
||||
['<CR>'] = {
|
||||
behavior = types.cmp.ConfirmBehavior.Replace,
|
||||
select = true,
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
sorting = {
|
||||
@@ -58,6 +48,8 @@ return function()
|
||||
}
|
||||
},
|
||||
|
||||
mapping = {},
|
||||
|
||||
formatting = {
|
||||
format = function(e, suggest_offset)
|
||||
local item = e:get_completion_item()
|
||||
|
||||
@@ -71,29 +71,49 @@ end
|
||||
|
||||
---Keypress handler
|
||||
core.on_keymap = function(keys, fallback)
|
||||
-- Documentation scroll
|
||||
if config.get().documentation.mapping[keys] then
|
||||
if config.get().documentation.mapping[keys] == types.cmp.ScrollDirection.Up then
|
||||
core.menu.float:scroll(-4)
|
||||
elseif config.get().documentation.mapping[keys] == types.cmp.ScrollDirection.Down then
|
||||
core.menu.float:scroll(4)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- Confirm character
|
||||
if config.get().confirmation.mapping[keys] then
|
||||
local c = config.get().confirmation.mapping[keys]
|
||||
local e = core.menu:get_selected_entry() or (c.select and core.menu:get_first_entry())
|
||||
if not e then
|
||||
for key, c 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()
|
||||
end
|
||||
core.confirm(e, {
|
||||
behavior = c.behavior,
|
||||
}, function()
|
||||
core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly }))
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
--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()
|
||||
if e and vim.tbl_contains(e:get_commit_characters(), chars) then
|
||||
local is_printable = char.is_printable(string.byte(chars, 1))
|
||||
return core.confirm(e, {
|
||||
core.confirm(e, {
|
||||
behavior = is_printable and 'insert' or 'replace',
|
||||
}, function()
|
||||
local ctx = core.get_context()
|
||||
@@ -112,6 +132,7 @@ core.on_keymap = function(keys, fallback)
|
||||
core.reset()
|
||||
end
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
fallback()
|
||||
@@ -119,10 +140,7 @@ end
|
||||
|
||||
---Prepare completion
|
||||
core.prepare = function()
|
||||
for keys in pairs(config.get().confirmation.mapping) do
|
||||
keymap.listen(keys, core.on_keymap)
|
||||
end
|
||||
for keys in pairs(config.get().documentation.mapping) do
|
||||
for keys in pairs(config.get().mapping) do
|
||||
keymap.listen(keys, core.on_keymap)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,7 +102,7 @@ end
|
||||
---Close floating window
|
||||
float.close = async.throttle(
|
||||
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)
|
||||
end
|
||||
self.entry = nil
|
||||
@@ -113,7 +113,7 @@ float.close = async.throttle(
|
||||
)
|
||||
|
||||
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 buf = vim.api.nvim_win_get_buf(self.win)
|
||||
local top = info.topline or 1
|
||||
@@ -129,4 +129,8 @@ float.scroll = function(self, delta)
|
||||
end
|
||||
end
|
||||
|
||||
float.is_visible = function(self)
|
||||
return self.win and vim.api.nvim_win_is_valid(self.win)
|
||||
end
|
||||
|
||||
return float
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
local core = require('cmp.core')
|
||||
local types = require('cmp.types')
|
||||
local source = require('cmp.source')
|
||||
local config = require('cmp.config')
|
||||
local autocmd = require('cmp.autocmd')
|
||||
@@ -13,6 +12,9 @@ end
|
||||
cmp.lsp = require('cmp.types.lsp')
|
||||
cmp.vim = require('cmp.types.vim')
|
||||
|
||||
---Export mapping
|
||||
cmp.mapping = require('cmp.mapping')
|
||||
|
||||
---Register completion sources
|
||||
---@param name string
|
||||
---@param s cmp.Source
|
||||
@@ -43,18 +45,6 @@ cmp.setup = setmetatable({
|
||||
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
|
||||
autocmd.subscribe('InsertEnter', function()
|
||||
core.prepare()
|
||||
|
||||
59
lua/cmp/mapping.lua
Normal file
59
lua/cmp/mapping.lua
Normal 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
|
||||
@@ -51,6 +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 sources cmp.SourceConfig[]
|
||||
|
||||
---@class cmp.CompletionConfig
|
||||
@@ -64,15 +65,9 @@ cmp.ScrollDirection.Down = 'down'
|
||||
---@field public winhighlight string
|
||||
---@field public maxwidth number|nil
|
||||
---@field public maxheight number|nil
|
||||
---@field public mapping table<string, cmp.ScrollDirection>
|
||||
|
||||
---@class cmp.ConfirmationConfig
|
||||
---@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
|
||||
---@field public priority_weight number
|
||||
@@ -88,5 +83,34 @@ 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
|
||||
|
||||
|
||||
@@ -26,13 +26,3 @@ if vim.fn.hlexists('CmpDocumentationBorder') == 0 then
|
||||
vim.cmd [[highlight link CmpDocumentationBorder NormalFloat]]
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user