From 4f5fa9b471e5c150c6b02513f99615350a052967 Mon Sep 17 00:00:00 2001 From: Guanran Wang Date: Sat, 24 Feb 2024 01:24:08 +0800 Subject: [PATCH] nvim-cmp: cleanup, lazyload, add source for "/" "?" ":" --- lua/plugins/completion.lua | 45 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lua/plugins/completion.lua b/lua/plugins/completion.lua index 4796069..fb70388 100644 --- a/lua/plugins/completion.lua +++ b/lua/plugins/completion.lua @@ -13,7 +13,8 @@ return { "rafamadriz/friendly-snippets", -- pre-configured snippet texts "saadparwaiz1/cmp_luasnip", -- nvim-cmp source }, - init = function() + event = { "InsertEnter", "CmdlineEnter" }, + opts = function() local cmp = require("cmp") local luasnip = require("luasnip") local has_words_before = function() @@ -24,25 +25,14 @@ return { require("mason").setup() require("luasnip.loaders.from_vscode").lazy_load() - require("cmp").setup({ + + cmp.setup({ -- keybinds - mapping = cmp.mapping.preset.insert({ - -- unsure - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.complete(), - [""] = cmp.mapping.abort(), - - -- return to confirm - [""] = cmp.mapping.confirm({ select = false }), - - -- tab to select prev/next option - -- see: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip + -- see: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings + mapping = { [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() - -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() - -- that way you will only jump inside the snippet region elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() elseif has_words_before() then @@ -51,6 +41,7 @@ return { fallback() end end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() @@ -60,7 +51,7 @@ return { fallback() end end, { "i", "s" }), - }), + }, -- snippet engine snippet = { @@ -69,14 +60,30 @@ return { end, }, - -- snippet source + -- completion source sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "luasnip" }, - }, { { name = "buffer" }, { name = "path" }, }), }) + + -- Use buffer source for `/` and `?` + cmp.setup.cmdline({ "/", "?" }, { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = "buffer" }, + }, + }) + + -- Use cmdline & path source for ':' + cmp.setup.cmdline(":", { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = "path" }, + { name = "cmdline" }, + }), + }) end, }