--[ -- Completion with snippets and LSP and all... -- -- Very good. --] return { "hrsh7th/nvim-cmp", enabled = true, event = "InsertEnter", dependencies = { "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer", "hrsh7th/cmp-path", "hrsh7th/cmp-cmdline", "saadparwaiz1/cmp_luasnip", "hrsh7th/cmp-nvim-lua", "hrsh7th/cmp-vsnip", "hrsh7th/vim-vsnip", "refamadriz/friendly-snippets", "onsails/lspkind.nvim", { -- TODO: Put that one in its own config file ! "L3MON4D3/LuaSnip", build = "make install_jsregexp", version = "v2.*", dependencies = { "rafamadriz/friendly-snippets", }, opts = { history = true, updateevents = "TextChanged,TextChangedI" }, config = function() local ls = require("luasnip") require("luasnip.loaders.from_vscode").lazy_load() require("luasnip.loaders.from_snipmate").lazy_load() require("luasnip.loaders.from_lua").lazy_load() -- print(vim.g.snipmate_snippets_path or "Hello") -- print(vim.g.vscode_snippets_path or "Hello2") vim.api.nvim_create_autocmd("InsertLeave", { callback = function() if require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()] and not require("luasnip").session.jump_active then require("luasnip").unlink_current() end end, }) -- Set keymaps vim.keymap.set({ "i" }, "", function() ls.expand() end, { silent = true }) vim.keymap.set({ "i", "s" }, "", function() ls.jump(1) end, { silent = true }) vim.keymap.set({ "i", "s" }, "", function() ls.jump(-1) end, { silent = true }) vim.keymap.set({ "i", "s" }, "", function() if ls.choice_active() then ls.change_choice(1) end end, { silent = true }) end, }, }, config = function() local cmp = require("cmp") local luasnip = require("luasnip") local lspkind = require("lspkind") --NOTE: -- Ctrl + Space (insert mode)=> to trigger completion. cmp.setup({ completion = { -- DOC: This is just help. -- - menuone: popup even when there's only one match -- - noinsert: Do not insert text until a selection is made -- - noselect: Do not select, force to select one from the menu -- - shortness: avoid showing extra messages when using completion -- - updatetime: set updatetime for CursorHold -- Check :help completeopt completeopt = "menu,menuone,preview,noselect", }, -- Select a snippet engine. snippet = { expand = function(args) luasnip.lsp_expand(args.body) end, }, mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping.select_prev_item(), [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), }), -- Order is important ! sources = { { name = "nvim_lsp_signature_help", keyword_length = 2 }, { name = "nvim_lsp", keyword_length = 2 }, -- lsp completion { name = "nvim-lua", keyword_length = 2 }, { name = "luasnip", keyword_length = 3 }, -- snippets { name = "buffer", keyword_length = 3 }, -- text within current buffer { name = "path" }, -- system path { name = "calc" }, -- source for math calculation ! }, window = { completion = cmp.config.window.bordered(), documentation = cmp.config.window.bordered(), }, formatting = { expandable_indicator = true, fields = { cmp.ItemField.Kind, cmp.ItemField.Abbr, cmp.ItemField.Menu }, --[[ -- TODO: Use that icons ! 🥳 format = function(entry, item) local menu_icon = { nvim_lsp = "λ", vsnip = "⋗", buffer = "Ω", path = "🖫", } item.menu = menu_icon[entry.source.name] return item end, --]] format = lspkind.cmp_format({ maxwidth = 50, ellipsis_char = "...", }), }, }) -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). cmp.setup.cmdline(":", { -- mapping = cmp.mapping.preset.cmdline(), mapping = cmp.mapping.preset.cmdline({ [""] = { c = function(default) if cmp.visible() then return cmp.confirm({ select = true }) end default() end, }, }), sources = cmp.config.sources({ { name = "path" }, }, { { name = "cmdline" }, }), }) end, }