diff options
Diffstat (limited to 'nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua')
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua | 73 |
1 files changed, 41 insertions, 32 deletions
diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua b/nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua index 418732b..5fd35d0 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/cmp.lua @@ -10,10 +10,6 @@ M.enabled = function() return enabled end -M.snippets = function(args) - require("luasnip").lsp_expand(args.body) -end - M.has_words_before = function() 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 @@ -30,39 +26,35 @@ M.complete = function(fallback) end end --- TODO: make this work more idiomatically with luasnip. Currently, a completion item must be accepted in order to --- expand/jump to next snippet item -M.next_item = function(fallback) - local cmp = require("cmp") +M.expand_snip = function(args) + require("luasnip").lsp_expand(args.body) +end + +M.next_snip = function(fallback) local luasnip = require("luasnip") - if cmp.visible() then - cmp.select_next_item() - elseif luasnip.expand_or_jumpable() then + if luasnip.expand_or_jumpable() then luasnip.expand_or_jump() - elseif M.has_words_before() then - cmp.complete() else fallback() end end -M.prev_item = function(fallback) - local cmp = require("cmp") +M.prev_snip = function(fallback) local luasnip = require("luasnip") - if cmp.visible() then - cmp.select_prev_item() - elseif luasnip.jumpable(-1) then + if luasnip.in_snippet() and luasnip.jumpable(-1) then luasnip.jump(-1) else fallback() end end --- TODO: wipe the luasnip expandable/jumpable list on close M.close = function(fallback) local cmp = require("cmp") - cmp.close() - fallback() + if cmp.visible() then + cmp.close() + else + fallback() + end end M.setup = function() @@ -75,17 +67,34 @@ M.setup = function() cmp.setup({ enabled = M.enabled, snippet = { - expand = M.snippets, - }, - mapping = { - ["<Tab>"] = cmp.mapping(M.next_item), - ["<S-Tab>"] = cmp.mapping(M.prev_item), - ["<C-d>"] = cmp.mapping.scroll_docs(-4), - ["<C-u>"] = cmp.mapping.scroll_docs(4), - ["<C-Space>"] = cmp.mapping(M.complete), - ["<CR>"] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.insert }), - ["<C-c>"] = cmp.mapping(M.close), + expand = M.expand_snip, }, + mapping = cmp.mapping.preset.cmdline({ + -- ["<Tab>"] = cmp.mapping(M.next_snip, { "i", "s" }), + -- ["<S-Tab>"] = cmp.mapping(M.prev_snip, { "i", "s" }), + -- ["<C-n>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "s" }), + -- ["<C-p>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "s" }), + -- ["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "s" }), + -- ["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "s" }), + -- ["<C-Space>"] = cmp.mapping(M.complete, { "i", "s" }), + -- ["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = false }), { "i", "s" }), + -- ["<C-e>"] = cmp.mapping(M.close), + ["<C-p>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "s" }), + ["<C-n>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "s" }), + ["<S-Tab>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "s" }), + ["<Tab>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "s" }), + ["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "s" }), + ["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "s" }), + ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "s" }), + ["<C-e>"] = cmp.mapping(cmp.mapping.close(), { "i", "s" }), + ["<CR>"] = cmp.mapping( + cmp.mapping.confirm({ + behavior = cmp.ConfirmBehavior.Insert, + select = true, + }), + { "i", "s" } + ), + }), ghost_text = true, sources = { { name = "nvim_lsp", group_index = 1 }, @@ -123,7 +132,7 @@ M.setup = function() }, }) - -- TODO: fix the default completion menu from showing on the cmdline + -- TODO: fix the default completion menu from showing on the cmdline cmp.setup.cmdline(":", { sources = { { name = "cmdline_history", max_item_count = 10 }, |