diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua index 23f0632..abaf30c 100644 --- a/lua/plugins/cmp.lua +++ b/lua/plugins/cmp.lua @@ -1,5 +1,6 @@ return { 'hrsh7th/nvim-cmp', + branch = 'main', lazy = false, dependencies = { 'neovim/nvim-lspconfig', @@ -11,10 +12,18 @@ return { 'saadparwaiz1/cmp_luasnip', 'onsails/lspkind.nvim', 'zbirenbaum/copilot-cmp', + 'hrsh7th/cmp-path', + 'dmitmel/cmp-cmdline-history', + 'hrsh7th/cmp-nvim-lsp-signature-help', }, config = function() + local t = function(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) + end + local cmp = require('cmp') local lspkind = require('lspkind') + local luasnip = require('luasnip') lspkind.init({ symbol_map = { Copilot = '', @@ -23,29 +32,75 @@ return { require('copilot_cmp').setup() cmp.setup({ + preselect = cmp.PreselectMode.None, snippet = { - -- REQUIRED - you must specify a snippet engine expand = function(args) require('luasnip').lsp_expand(args.body) -- For `luasnip` users. end, }, + mapping = { + [''] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 'c', 's' }), + [''] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 'c', 's' }), + [''] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 'c', 's' }), + [''] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 'c', 's' }), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c', 's' }), + [''] = cmp.mapping(cmp.mapping.close(), { 'i', 'c', 's' }), + [''] = cmp.mapping({ + i = function(fallback) + if cmp.visible() and cmp.get_active_entry() then + cmp.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = false }) + else + fallback() + end + end, + c = cmp.mapping.confirm({ select = false }), + s = cmp.mapping.confirm({ select = false }), + }), + [''] = cmp.mapping(function(fallback) + if luasnip.expand_or_jumpable() then + vim.api.nvim_feedkeys(t('luasnip-expand-or-jump'), '', true) + else + fallback() + end + end, { + 'i', + 's', + }), + [''] = cmp.mapping(function(fallback) + if luasnip.jumpable(-1) then + vim.api.nvim_feedkeys(t('luasnip-jump-prev'), '', true) + else + fallback() + end + end, { 'i', 's' }), + [''] = cmp.mapping( + cmp.mapping.complete({ + config = { + sources = { + { + name = 'cmdline_history', + }, + }, + }, + }), + { 'c' } + ), + }, + sources = cmp.config.sources({ + { name = 'copilot' }, + { name = 'nvim_lsp' }, + { name = 'nvim_lsp_signature_help' }, + { name = 'luasnip' }, + { name = 'buffer' }, + { name = 'path' }, + }), + window = { completion = cmp.config.window.bordered(), documentation = cmp.config.window.bordered(), }, - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. - }), - sources = cmp.config.sources({ - { name = 'copilot' }, - { name = 'nvim_lsp' }, - { name = 'luasnip' }, -- For luasnip users. - { name = 'buffer' }, - }), formatting = { format = lspkind.cmp_format({ mode = 'symbol', @@ -60,33 +115,26 @@ return { }, }) - -- To use git you need to install the plugin petertriho/cmp-git and uncomment lines below - -- Set configuration for specific filetype. - --[[ cmp.setup.filetype('gitcommit', { - sources = cmp.config.sources({ - { name = 'git' }, - }, { - { name = 'buffer' }, - }) - }) - require("cmp_git").setup() ]] -- - -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore). cmp.setup.cmdline({ '/', '?' }, { mapping = cmp.mapping.preset.cmdline(), sources = { - { name = 'buffer' } + { name = 'buffer' }, + { name = 'cmdline_history' }, + { name = 'path' }, + { name = 'buffer' }, } }) -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). cmp.setup.cmdline(':', { mapping = cmp.mapping.preset.cmdline(), - sources = cmp.config.sources({ - { name = 'path' } - }, { - { name = 'cmdline' } - }), + sources = { + { name = 'cmdline', priority = 200 }, + { name = 'path', priority = 3 }, + { name = 'cmdline_history', priority = 3 }, + { name = 'buffer', priority = 3 } + }, matching = { disallow_symbol_nonprefix_matching = false } }) end