Skip to content

Commit

Permalink
chore(nvim): Move cmp keymaps into cmp configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Mar 29, 2024
1 parent f0a1543 commit 2a4892b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 81 deletions.
77 changes: 76 additions & 1 deletion nvim/lua/my/configure/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ return {
},
event = { 'InsertEnter', 'CmdlineEnter' },
config = function()
local function has_words_before()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil
end

local cmp = require('cmp')
local window_config = cmp.config.window.bordered({
border = 'none',
Expand All @@ -100,7 +105,77 @@ return {
completion = {
completeopt = 'menu,menuone,noinsert',
},
mapping = require('my.legendary.keymap').cmp_mappings(),
mapping = {
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-n>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-p>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-d>'] = cmp.mapping({ i = cmp.mapping.scroll_docs(-4) }),
['<C-f>'] = cmp.mapping({ i = cmp.mapping.scroll_docs(4) }),
['<C-Space>'] = cmp.mapping({ c = cmp.mapping.confirm({ select = true }) }),
['<Right>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.mapping.confirm({ select = true })()
else
fallback()
end
end, {
'c',
}),
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-e>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.mapping.close()()
else
fallback()
end
end, {
'i',
'c',
}),
},
formatting = {
format = function(...)
return require('lspkind').cmp_format({ with_text = true })(...)
Expand Down
80 changes: 0 additions & 80 deletions nvim/lua/my/legendary/keymap.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
local M = {}

local function has_words_before()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil
end

function M.default_keymaps()
local h = require('legendary.toolbox')
return {
Expand Down Expand Up @@ -199,79 +194,4 @@ function M.lsp_keymaps(bufnr)
}
end

function M.cmp_mappings()
local cmp = require('cmp')
return {
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-n>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-p>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, {
'i',
's',
'c',
}),
['<C-d>'] = cmp.mapping({ i = cmp.mapping.scroll_docs(-4) }),
['<C-f>'] = cmp.mapping({ i = cmp.mapping.scroll_docs(4) }),
['<C-Space>'] = cmp.mapping({ c = cmp.mapping.confirm({ select = true }) }),
['<Right>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.mapping.confirm({ select = true })()
else
fallback()
end
end, {
'c',
}),
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-e>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.mapping.close()()
else
fallback()
end
end, {
'i',
'c',
}),
}
end

return M

0 comments on commit 2a4892b

Please sign in to comment.