-- Completions with nvim-cmp and friends -- https://github.com/hrsh7th/nvim-cmp local function configure_cmp() local cmp = require("cmp") local cmp_types = require("cmp.types") local source_mapping = { nvim_lsp = "[LSP]", luasnip = "[LuaSnip]", path = "[Path]", buffer = "[Buffer]", } cmp.setup({ -- Expand snippets with cmp_luasnip snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, -- Keybindings mapping = cmp.mapping.preset.insert({ -- [""] = cmp.mapping(cmp.mapping.select_next_item({ behavior = cmp_types.cmp.SelectBehavior.Insert })), -- This is to make the hotkey for both initiating completion and select next item [""] = function(fallback) if cmp.visible() then cmp.select_next_item({ behavior = cmp_types.cmp.SelectBehavior.Insert }) elseif (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 end)() then cmp.complete() else fallback() end end, [""] = cmp.mapping.select_prev_item({ behavior = cmp_types.cmp.SelectBehavior.Insert }), [""] = cmp.mapping.scroll_docs( 4), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.complete(), -- same as above [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = false }), }), -- TODO: Why are these separate? Priority? sources = cmp.config.sources( { { name = "nvim_lsp" }, { name = "luasnip" }, { name = "path" }, }, { { name = "buffer" }, } ), completion = { keyword_length = 4, completeopt = "menu,menuone,noselect", }, formatting = { format = function(entry, vim_item) vim_item.menu = source_mapping[entry.source.name] return vim_item end, }, }) end return { "hrsh7th/nvim-cmp", dependencies = { {"hrsh7th/cmp-nvim-lsp"}, {"hrsh7th/cmp-buffer"}, {"hrsh7th/cmp-path"}, -- Snippet engine(s) required for nvim-cmp (expands things from LS) {"L3MON4D3/LuaSnip"}, {"saadparwaiz1/cmp_luasnip"}, }, config = configure_cmp, }