nopasteonlybin

cmp-nvim.lua

Apr 13th, 2023
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.42 KB | None | 0 0
  1. return {
  2.         'hrsh7th/nvim-cmp',
  3.         dependencies = { 'hrsh7th/cmp-nvim-lsp',
  4.                 'L3MON4D3/LuaSnip',
  5.                 'saadparwaiz1/cmp_luasnip',
  6.                 "hrsh7th/cmp-path",
  7.                 "hrsh7th/cmp-cmdline",
  8.                 "rafamadriz/friendly-snippets"
  9.         },
  10.         config = function()
  11.                 local status_cmp_ok, cmp = pcall(require, "cmp")
  12.                 local status_luasnip_ok, luasnip = pcall(require, "luasnip")
  13.                 -- local cmp_autopairs = require('nvim-autopairs.completion.cmp')
  14.                 -- local status_autopairs_ok, autopairs = pcall(require, " nvim-autopairs.completion.cmp")
  15.                 if not (status_cmp_ok or status_luasnip_ok) then
  16.                         print("CMP not loaded")
  17.                         vim.notify("CMP not working", "warn")
  18.                         return
  19.                 end
  20.  
  21.                 luasnip.config.setup {}
  22.                 require('luasnip.loaders.from_vscode').lazy_load()
  23.                 local check_backspace = function()
  24.                         local col = vim.fn.col(".") - 1
  25.                         return col == 0 or vim.fn.getline("."):sub(col, col):match("%s")
  26.                 end
  27.  
  28.                 local kind_icons = {
  29.                         Text = "",
  30.                         Method = "",
  31.                         Function = "",
  32.                         Constructor = "",
  33.                         Field = "",
  34.                         Variable = "",
  35.                         Class = "",
  36.                         Interface = "",
  37.                         Module = "",
  38.                         Property = "",
  39.                         Unit = "",
  40.                         Value = "",
  41.                         Enum = "",
  42.                         Keyword = "",
  43.                         Snippet = "",
  44.                         Color = "",
  45.                         File = "",
  46.                         Reference = "",
  47.                         Folder = "",
  48.                         EnumMember = "",
  49.                         Constant = "",
  50.                         Struct = "",
  51.                         Event = "",
  52.                         Operator = "",
  53.                         TypeParameter = "",
  54.                 }
  55.  
  56.                 cmp.setup({
  57.                         snippet = {
  58.                                 expand = function(args)
  59.                                         luasnip.lsp_expand(args.body) -- For `luasnip` users.
  60.                                 end,
  61.                         },
  62.                         mapping = cmp.mapping.preset.insert({
  63.                                 -- ['<C-d>'] = cmp.mapping.scroll_docs( -4),
  64.                                 -- ['<C-f>'] = cmp.mapping.scroll_docs(4),
  65.                                 -- ['<CR>'] = cmp.mapping.confirm {
  66.                                 --   behavior = cmp.ConfirmBehavior.Replace,
  67.                                 --   select = true,
  68.                                 -- },
  69.                                     ["<C-k>"] = cmp.mapping.select_prev_item(),
  70.                                     ["<C-j>"] = cmp.mapping.select_next_item(),
  71.                                     ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
  72.                                     ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
  73.                                     ["<C-Space>"] = cmp.mapping(cmp.mapping.complete({}), { "i", "c" }),
  74.                                     ["<C-e>"] = cmp.mapping({
  75.                                         i = cmp.mapping.abort(),
  76.                                         c = cmp.mapping.close(),
  77.                                 }),
  78.                                 -- Accept currently selected item. If none selected, `select` first item.
  79.                                 -- Set `select` to `false` to only confirm explicitly selected items.
  80.                                     ["<CR>"] = cmp.mapping.confirm({ select = true }),
  81.                                     ["<Tab>"] = cmp.mapping(function(fallback)
  82.                                         if cmp.visible() then
  83.                                                 cmp.select_next_item()
  84.                                         elseif luasnip.expandable() then
  85.                                                 luasnip.expand()
  86.                                         elseif luasnip.expand_or_jumpable() then
  87.                                                 luasnip.expand_or_jump()
  88.                                         elseif check_backspace() then
  89.                                                 fallback()
  90.                                         else
  91.                                                 fallback()
  92.                                         end
  93.                                 end, {
  94.                                         "i",
  95.                                         "s",
  96.                                 }),
  97.                                     ["<S-Tab>"] = cmp.mapping(function(fallback)
  98.                                         if cmp.visible() then
  99.                                                 cmp.select_prev_item()
  100.                                         elseif luasnip.jumpable(-1) then
  101.                                                 luasnip.jump(-1)
  102.                                         else
  103.                                                 fallback()
  104.                                         end
  105.                                 end, {
  106.                                         "i",
  107.                                         "s",
  108.                                 }),
  109.                         }),
  110.                         formatting = {
  111.                                 fields = { "kind", "abbr", "menu" },
  112.                                 format = function(entry, vim_item)
  113.                                         vim_item.kind = kind_icons[vim_item.kind]
  114.                                         vim_item.menu = ({
  115.                                                     nvim_lsp = "LSP",
  116.                                                     nvim_lua = "LUA",
  117.                                                     luasnip = "SNIP",
  118.                                                     buffer = "BUFF",
  119.                                                     path = "DIR",
  120.                                                     emoji = "EMOJI",
  121.                                             })[entry.source.name]
  122.                                         return vim_item
  123.                                 end,
  124.                         },
  125.                         sources = {
  126.                                 { name = "nvim_lsp" },
  127.                                 { name = "nvim_lua" },
  128.                                 { name = "luasnip" },
  129.                                 { name = "buffer" },
  130.                                 { name = "path" },
  131.                         },
  132.                         confirm_opts = {
  133.                                 behavior = cmp.ConfirmBehavior.Replace,
  134.                                 select = false,
  135.                         },
  136.                         window = {
  137.                                 completion = cmp.config.window.bordered(),
  138.                                 documentation = cmp.config.window.bordered(),
  139.                         },
  140.                         experimental = {
  141.                                 ghost_text = true,
  142.                         },
  143.                 })
  144.         end
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment