Skip to content

Commit

Permalink
Update mapping configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Aug 11, 2021
1 parent 4c9462c commit c2f8729
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 88 deletions.
73 changes: 52 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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`.
Expand Down Expand Up @@ -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,
}
```

Expand Down
12 changes: 2 additions & 10 deletions lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -58,6 +48,8 @@ return function()
}
},

mapping = {},

formatting = {
format = function(e, suggest_offset)
local item = e:get_completion_item()
Expand Down
70 changes: 44 additions & 26 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,57 @@ 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.
local chars = keymap.t(keys)
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()
Expand All @@ -112,17 +132,15 @@ core.on_keymap = function(keys, fallback)
core.reset()
end
end)
return
end

fallback()
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
Expand Down
8 changes: 6 additions & 2 deletions lua/cmp/float.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
16 changes: 3 additions & 13 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
59 changes: 59 additions & 0 deletions lua/cmp/mapping.lua
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit c2f8729

Please sign in to comment.