|
| 1 | +return { |
| 2 | + { |
| 3 | + "hrsh7th/nvim-cmp", |
| 4 | + -- event = "InsertEnter", -- lazy-load this if want a faster startup time |
| 5 | + dependencies = { |
| 6 | + "L3MON4D3/LuaSnip", -- Snippet engine |
| 7 | + "saadparwaiz1/cmp_luasnip", -- Snippet completion |
| 8 | + "hrsh7th/cmp-buffer", -- Buffer completion |
| 9 | + "hrsh7th/cmp-path", -- Path completion |
| 10 | + "hrsh7th/cmp-nvim-lsp", -- LSP completion |
| 11 | + "onsails/lspkind.nvim", -- LSP completion formatter |
| 12 | + "windwp/nvim-autopairs", -- Autopair |
| 13 | + }, |
| 14 | + config = function() |
| 15 | + local cmp = require("cmp") |
| 16 | + local luasnip = require("luasnip") |
| 17 | + |
| 18 | + -- Utility function to check backspace in "magic tab snippet" |
| 19 | + local check_backspace = function() |
| 20 | + local col = vim.fn.col "." - 1 |
| 21 | + return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" |
| 22 | + end |
| 23 | + |
| 24 | + cmp.setup { |
| 25 | + snippet = { |
| 26 | + expand = function(args) |
| 27 | + luasnip.lsp_expand(args.body) |
| 28 | + end, |
| 29 | + }, |
| 30 | + mapping = { |
| 31 | + ["<C-k>"] = cmp.mapping.select_prev_item(), |
| 32 | + ["<C-j>"] = cmp.mapping.select_next_item(), |
| 33 | + ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }), |
| 34 | + ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }), |
| 35 | + ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), |
| 36 | + ["<C-y>"] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping. |
| 37 | + ["<C-e>"] = cmp.mapping { |
| 38 | + i = cmp.mapping.abort(), |
| 39 | + c = cmp.mapping.close(), |
| 40 | + }, |
| 41 | + -- Accept currently selected item. If none selected, `select` first item. |
| 42 | + -- Set `select` to `false` to only confirm explicitly selected items. |
| 43 | + ["<CR>"] = cmp.mapping.confirm { select = true }, |
| 44 | + ["<Tab>"] = cmp.mapping(function(fallback) |
| 45 | + if cmp.visible() then |
| 46 | + cmp.select_next_item() |
| 47 | + elseif luasnip.expandable() then |
| 48 | + luasnip.expand() |
| 49 | + elseif luasnip.expand_or_jumpable() then |
| 50 | + luasnip.expand_or_jump() |
| 51 | + elseif check_backspace() then |
| 52 | + fallback() |
| 53 | + else |
| 54 | + fallback() |
| 55 | + end |
| 56 | + end, { |
| 57 | + "i", |
| 58 | + "s", |
| 59 | + }), |
| 60 | + ["<S-Tab>"] = cmp.mapping(function(fallback) |
| 61 | + if cmp.visible() then |
| 62 | + cmp.select_prev_item() |
| 63 | + elseif luasnip.jumpable(-1) then |
| 64 | + luasnip.jump(-1) |
| 65 | + else |
| 66 | + fallback() |
| 67 | + end |
| 68 | + end, { |
| 69 | + "i", |
| 70 | + "s", |
| 71 | + }), |
| 72 | + }, |
| 73 | + formatting = { |
| 74 | + fields = { "kind", "abbr", "menu" }, |
| 75 | + format = function(entry, vim_item) |
| 76 | + local kind = require("lspkind").cmp_format({ mode = "symbol_text", maxwidth = 50 })(entry, vim_item) |
| 77 | + local strings = vim.split(kind.kind, "%s", { trimempty = true }) |
| 78 | + kind.kind = " " .. (strings[1] or "") .. " " |
| 79 | + kind.menu = " (" .. (strings[2] or "") .. ")" |
| 80 | + |
| 81 | + return kind |
| 82 | + end, |
| 83 | + }, |
| 84 | + sources = { |
| 85 | + { name = "nvim_lsp" }, |
| 86 | + { name = "luasnip" }, |
| 87 | + { name = "buffer" }, |
| 88 | + { name = "path" }, |
| 89 | + }, |
| 90 | + confirm_opts = { |
| 91 | + behavior = cmp.ConfirmBehavior.Replace, |
| 92 | + select = false, |
| 93 | + }, |
| 94 | + window = { |
| 95 | + completion = cmp.config.window.bordered({ |
| 96 | + border = "single", |
| 97 | + }), |
| 98 | + documentation = cmp.config.window.bordered({ |
| 99 | + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, |
| 100 | + }), |
| 101 | + }, |
| 102 | + experimental = { |
| 103 | + ghost_text = false, |
| 104 | + native_menu = false, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + -- Link autocompt with cmp |
| 109 | + local cmp_autopairs = require('nvim-autopairs.completion.cmp') |
| 110 | + cmp.event:on( |
| 111 | + 'confirm_done', |
| 112 | + cmp_autopairs.on_confirm_done() |
| 113 | + ) |
| 114 | + |
| 115 | + -- -- gray |
| 116 | + -- vim.api.nvim_set_hl(0, 'CmpItemAbbrDeprecated', { bg = 'NONE', strikethrough = true, fg = '#808080' }) |
| 117 | + -- -- blue |
| 118 | + -- vim.api.nvim_set_hl(0, 'CmpItemAbbrMatch', { bg = 'NONE', fg = '#569CD6' }) |
| 119 | + -- vim.api.nvim_set_hl(0, 'CmpItemAbbrMatchFuzzy', { link = 'CmpIntemAbbrMatch' }) |
| 120 | + -- -- light blue |
| 121 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindVariable', { bg = 'NONE', fg = '#9CDCFE' }) |
| 122 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindVariable', { bg = 'NONE', fg = '#FF00FF' }) |
| 123 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindInterface', { link = 'CmpItemKindVariable' }) |
| 124 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindText', { link = 'CmpItemKindVariable' }) |
| 125 | + -- -- pink |
| 126 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindFunction', { bg = 'NONE', fg = '#C586C0' }) |
| 127 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindMethod', { link = 'CmpItemKindFunction' }) |
| 128 | + -- -- front |
| 129 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindKeyword', { bg = 'NONE', fg = '#D4D4D4' }) |
| 130 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindProperty', { link = 'CmpItemKindKeyword' }) |
| 131 | + -- vim.api.nvim_set_hl(0, 'CmpItemKindUnit', { link = 'CmpItemKindKeyword' }) |
| 132 | + end, |
| 133 | + }, |
| 134 | + |
| 135 | +} |
0 commit comments