Skip to content

Commit 93cf84f

Browse files
author
hrsh7th
committed
Remove all default mappings
1 parent 4f1358e commit 93cf84f

File tree

4 files changed

+73
-85
lines changed

4 files changed

+73
-85
lines changed

README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,13 @@ lua <<EOF
7979
-- completion = cmp.config.window.bordered(),
8080
-- documentation = cmp.config.window.bordered(),
8181
},
82-
mapping = {
83-
['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
84-
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
85-
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
86-
['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
87-
['<C-e>'] = cmp.mapping({
88-
i = cmp.mapping.abort(),
89-
c = cmp.mapping.close(),
90-
}),
82+
mapping = cmp.mapping.preset.insert({
83+
['<C-b>'] = cmp.mapping.scroll_docs(-4),
84+
['<C-f>'] = cmp.mapping.scroll_docs(4),
85+
['<C-Space>'] = cmp.mapping.complete(),
86+
['<C-e>'] = cmp.mapping.abort(),
9187
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
92-
},
88+
}),
9389
sources = cmp.config.sources({
9490
{ name = 'nvim_lsp' },
9591
{ name = 'vsnip' }, -- For vsnip users.
@@ -112,13 +108,15 @@ lua <<EOF
112108

113109
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
114110
cmp.setup.cmdline('/', {
111+
mapping = cmp.mapping.preset.cmdline(),
115112
sources = {
116113
{ name = 'buffer' }
117114
}
118115
})
119116

120117
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
121118
cmp.setup.cmdline(':', {
119+
mapping = cmp.mapping.preset.cmdline(),
122120
sources = cmp.config.sources({
123121
{ name = 'path' }
124122
}, {

doc/cmp.txt

+7-11
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,12 @@ NOTE:
9191
-- completion = cmp.config.window.bordered(),
9292
-- documentation = cmp.config.window.bordered(),
9393
},
94-
mapping = {
95-
['<C-d>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
96-
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
97-
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
98-
['<C-e>'] = cmp.mapping({
99-
i = cmp.mapping.abort(),
100-
c = cmp.mapping.close(),
101-
}),
102-
-- Accept currently selected item. If none selected, `select` first item.
103-
-- Set `select` to `false` to only confirm explicitly selected items.
94+
mapping = cmp.mapping.preset.insert({
95+
['<C-d>'] = cmp.mapping.scroll_docs(-4),
96+
['<C-f>'] = cmp.mapping.scroll_docs(4),
97+
['<C-Space>'] = cmp.mapping.complete(),
10498
['<CR>'] = cmp.mapping.confirm({ select = true }),
105-
},
99+
}),
106100
sources = cmp.config.sources({
107101
{ name = 'nvim_lsp' },
108102
{ name = 'vsnip' }, -- For vsnip users.
@@ -116,13 +110,15 @@ NOTE:
116110
117111
-- `/` cmdline setup.
118112
cmp.setup.cmdline('/', {
113+
mapping = cmp.mapping.preset.cmdline(),
119114
sources = {
120115
{ name = 'buffer' }
121116
}
122117
})
123118
124119
-- `:` cmdline setup.
125120
cmp.setup.cmdline(':', {
121+
mapping = cmp.mapping.preset.cmdline(),
126122
sources = cmp.config.sources({
127123
{ name = 'path' }
128124
}, {

lua/cmp/config/default.lua

+1-62
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
local compare = require('cmp.config.compare')
2-
local mapping = require('cmp.config.mapping')
3-
local keymap = require('cmp.utils.keymap')
42
local types = require('cmp.types')
53

64
local WIDE_HEIGHT = 40
@@ -18,66 +16,7 @@ return function()
1816

1917
preselect = types.cmp.PreselectMode.Item,
2018

21-
mapping = {
22-
['<Down>'] = mapping({
23-
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
24-
c = function(fallback)
25-
local cmp = require('cmp')
26-
cmp.close()
27-
vim.schedule(cmp.suspend())
28-
fallback()
29-
end,
30-
}),
31-
['<Up>'] = mapping({
32-
i = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }),
33-
c = function(fallback)
34-
local cmp = require('cmp')
35-
cmp.close()
36-
vim.schedule(cmp.suspend())
37-
fallback()
38-
end,
39-
}),
40-
['<Tab>'] = mapping({
41-
c = function()
42-
local cmp = require('cmp')
43-
if #cmp.core:get_sources() > 0 and not require('cmp.config').is_native_menu() then
44-
if cmp.visible() then
45-
cmp.select_next_item()
46-
else
47-
cmp.complete()
48-
end
49-
else
50-
if vim.fn.pumvisible() == 0 then
51-
vim.api.nvim_feedkeys(keymap.t('<C-z>'), 'in', true)
52-
else
53-
vim.api.nvim_feedkeys(keymap.t('<C-n>'), 'in', true)
54-
end
55-
end
56-
end,
57-
}),
58-
['<S-Tab>'] = mapping({
59-
c = function()
60-
local cmp = require('cmp')
61-
if #cmp.core:get_sources() > 0 and not require('cmp.config').is_native_menu() then
62-
if cmp.visible() then
63-
cmp.select_prev_item()
64-
else
65-
cmp.complete()
66-
end
67-
else
68-
if vim.fn.pumvisible() == 0 then
69-
vim.api.nvim_feedkeys(keymap.t('<C-z><C-p><C-p>'), 'in', true)
70-
else
71-
vim.api.nvim_feedkeys(keymap.t('<C-p>'), 'in', true)
72-
end
73-
end
74-
end,
75-
}),
76-
['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
77-
['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
78-
['<C-y>'] = mapping.confirm({ select = false }),
79-
['<C-e>'] = mapping.abort(),
80-
},
19+
mapping = {},
8120

8221
snippet = {
8322
expand = function()

lua/cmp/config/mapping.lua

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
local mapping
2-
mapping = setmetatable({}, {
1+
local types = require('cmp.types')
2+
local misc = require('cmp.utils.misc')
3+
4+
local mapping = setmetatable({}, {
35
__call = function(_, invoke, modes)
46
if type(invoke) == 'function' then
57
local map = {}
@@ -12,6 +14,59 @@ mapping = setmetatable({}, {
1214
end,
1315
})
1416

17+
---Mapping preset configuration.
18+
mapping.preset = {}
19+
20+
---Mapping preset insert-mode configuration.
21+
mapping.preset.insert = function(override)
22+
return misc.merge(override or {}, {
23+
['<Down>'] = {
24+
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
25+
},
26+
['<Up>'] = {
27+
i = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }),
28+
},
29+
['<C-n>'] = {
30+
i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }),
31+
},
32+
['<C-p>'] = {
33+
i = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }),
34+
},
35+
['<C-y>'] = {
36+
i = mapping.confirm({ select = false }),
37+
},
38+
['<C-e>'] = {
39+
i = mapping.abort(),
40+
}
41+
})
42+
end
43+
44+
---Mapping preset cmdline-mode configuration.
45+
mapping.preset.cmdline = function(override)
46+
return misc.merge(override or {}, {
47+
['<Tab>'] = {
48+
c = function()
49+
local cmp = require('cmp')
50+
if cmp.visible() then
51+
cmp.select_next_item()
52+
else
53+
cmp.complete()
54+
end
55+
end
56+
},
57+
['<S-Tab>'] = {
58+
c = function()
59+
local cmp = require('cmp')
60+
if cmp.visible() then
61+
cmp.select_prev_item()
62+
else
63+
cmp.complete()
64+
end
65+
end
66+
}
67+
})
68+
end
69+
1570
---Invoke completion
1671
---@param option cmp.CompleteParams
1772
mapping.complete = function(option)

0 commit comments

Comments
 (0)