From 2a4892b7966cf54c80a74d050cf741287dc069fb Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Fri, 29 Mar 2024 11:29:36 -0400 Subject: [PATCH] chore(nvim): Move cmp keymaps into cmp configuration --- nvim/lua/my/configure/completion.lua | 77 +++++++++++++++++++++++++- nvim/lua/my/legendary/keymap.lua | 80 ---------------------------- 2 files changed, 76 insertions(+), 81 deletions(-) diff --git a/nvim/lua/my/configure/completion.lua b/nvim/lua/my/configure/completion.lua index 3443e9e8..e149d27c 100644 --- a/nvim/lua/my/configure/completion.lua +++ b/nvim/lua/my/configure/completion.lua @@ -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', @@ -100,7 +105,77 @@ return { completion = { completeopt = 'menu,menuone,noinsert', }, - mapping = require('my.legendary.keymap').cmp_mappings(), + mapping = { + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, { + 'i', + 's', + 'c', + }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() + end + end, { + 'i', + 's', + 'c', + }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, { + 'i', + 's', + 'c', + }), + [''] = 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', + }), + [''] = cmp.mapping({ i = cmp.mapping.scroll_docs(-4) }), + [''] = cmp.mapping({ i = cmp.mapping.scroll_docs(4) }), + [''] = cmp.mapping({ c = cmp.mapping.confirm({ select = true }) }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.mapping.confirm({ select = true })() + else + fallback() + end + end, { + 'c', + }), + [''] = cmp.mapping.confirm({ select = true }), + [''] = 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 })(...) diff --git a/nvim/lua/my/legendary/keymap.lua b/nvim/lua/my/legendary/keymap.lua index fa10b1ff..fcc39a49 100644 --- a/nvim/lua/my/legendary/keymap.lua +++ b/nvim/lua/my/legendary/keymap.lua @@ -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 { @@ -199,79 +194,4 @@ function M.lsp_keymaps(bufnr) } end -function M.cmp_mappings() - local cmp = require('cmp') - return { - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_prev_item() - else - fallback() - end - end, { - 'i', - 's', - 'c', - }), - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - else - fallback() - end - end, { - 'i', - 's', - 'c', - }), - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_prev_item() - else - fallback() - end - end, { - 'i', - 's', - 'c', - }), - [''] = 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', - }), - [''] = cmp.mapping({ i = cmp.mapping.scroll_docs(-4) }), - [''] = cmp.mapping({ i = cmp.mapping.scroll_docs(4) }), - [''] = cmp.mapping({ c = cmp.mapping.confirm({ select = true }) }), - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.mapping.confirm({ select = true })() - else - fallback() - end - end, { - 'c', - }), - [''] = cmp.mapping.confirm({ select = true }), - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.mapping.close()() - else - fallback() - end - end, { - 'i', - 'c', - }), - } -end - return M