Explorar el Código

feat: compe -> cmp (#1496)

Christian Chiarulli hace 3 años
padre
commit
9ece2e5369
Se han modificado 9 ficheros con 215 adiciones y 199 borrados
  1. 0 7
      init.lua
  2. 8 8
      lua/config/defaults.lua
  3. 30 22
      lua/core/autopairs.lua
  4. 1 1
      lua/core/builtins/init.lua
  5. 123 0
      lua/core/cmp.lua
  6. 0 133
      lua/core/compe.lua
  7. 6 0
      lua/lsp/init.lua
  8. 31 0
      lua/lsp/kind.lua
  9. 16 28
      lua/plugins.lua

+ 0 - 7
init.lua

@@ -52,10 +52,3 @@ if lsp_settings_status_ok then
 end
 
 require("keymappings").setup()
-
--- TODO: these guys need to be in language files
--- if lvim.lang.emmet.active then
---   require "lsp.emmet-ls"
--- end
--- if lvim.lang.tailwindcss.active then
---   require "lsp.tailwind

+ 8 - 8
lua/config/defaults.lua

@@ -37,30 +37,30 @@ lvim = {
   lsp = {
     completion = {
       item_kind = {
-        "   (Text) ",
+        "   (Text) ",
         "   (Method)",
         "   (Function)",
         "   (Constructor)",
-        "   (Field)",
-        "[] (Variable)",
+        "   (Field)",
+        " (Variable)",
         "   (Class)",
         " ﰮ  (Interface)",
         "   (Module)",
-        "  (Property)",
-        "  (Unit)",
+        "  (Property)",
+        "  (Unit)",
         "   (Value)",
         " 練 (Enum)",
         "   (Keyword)",
         "   (Snippet)",
         "   (Color)",
-        "   (File)",
+        "   (File)",
         "   (Reference)",
         "   (Folder)",
         "   (EnumMember)",
         " ﲀ  (Constant)",
-        "   (Struct)",
+        "   (Struct)",
         "   (Event)",
-        "   (Operator)",
+        "   (Operator)",
         "   (TypeParameter)",
       },
     },

+ 30 - 22
lua/core/autopairs.lua

@@ -20,36 +20,44 @@ function M.config()
 end
 
 M.setup = function()
-  -- skip it, if you use another global object
-  _G.MUtils = {}
   local autopairs = require "nvim-autopairs"
   local Rule = require "nvim-autopairs.rule"
-
-  vim.g.completion_confirm_key = ""
-  MUtils.completion_confirm = function()
-    if vim.fn.pumvisible() ~= 0 then
-      if vim.fn.complete_info()["selected"] ~= -1 then
-        return vim.fn["compe#confirm"](autopairs.esc "<cr>")
-      else
-        return autopairs.esc "<cr>"
-      end
-    else
-      return autopairs.autopairs_cr()
-    end
-  end
-
-  if package.loaded["compe"] then
-    require("nvim-autopairs.completion.compe").setup {
-      map_cr = lvim.builtin.autopairs.map_cr,
-      map_complete = lvim.builtin.autopairs.map_complete,
-    }
-  end
+  local cond = require "nvim-autopairs.conds"
 
   autopairs.setup {
     check_ts = lvim.builtin.autopairs.check_ts,
     ts_config = lvim.builtin.autopairs.ts_config,
   }
 
+  -- vim.g.completion_confirm_key = ""
+
+  autopairs.add_rule(Rule("$$", "$$", "tex"))
+  autopairs.add_rules {
+    Rule("$", "$", { "tex", "latex" }) -- don't add a pair if the next character is %
+      :with_pair(cond.not_after_regex_check "%%") -- don't add a pair if  the previous character is xxx
+      :with_pair(cond.not_before_regex_check("xxx", 3)) -- don't move right when repeat character
+      :with_move(cond.none()) -- don't delete if the next character is xx
+      :with_del(cond.not_after_regex_check "xx") -- disable  add newline when press <cr>
+      :with_cr(cond.none()),
+  }
+  autopairs.add_rules {
+    Rule("$$", "$$", "tex"):with_pair(function(opts)
+      print(vim.inspect(opts))
+      if opts.line == "aa $$" then
+        -- don't add pair on that line
+        return false
+      end
+    end),
+  }
+
+  if package.loaded["cmp"] then
+    require("nvim-autopairs.completion.cmp").setup {
+      map_cr = true, --  map <CR> on insert mode
+      map_complete = true, -- it will auto insert `(` after select function or method item
+      auto_select = true, -- automatically select the first item
+    }
+  end
+
   require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
 
   local ts_conds = require "nvim-autopairs.ts-conds"

+ 1 - 1
lua/core/builtins/init.lua

@@ -4,7 +4,7 @@ local builtins = {
   "keymappings",
   "core.which-key",
   "core.gitsigns",
-  "core.compe",
+  "core.cmp",
   "core.dashboard",
   "core.dap",
   "core.terminal",

+ 123 - 0
lua/core/cmp.lua

@@ -0,0 +1,123 @@
+local M = {}
+
+local check_backspace = function()
+  local col = vim.fn.col "." - 1
+  return col == 0 or vim.fn.getline("."):sub(col, col):match "%s"
+end
+
+local function T(str)
+  return vim.api.nvim_replace_termcodes(str, true, true, true)
+end
+
+local is_emmet_active = function()
+  local clients = vim.lsp.buf_get_clients()
+
+  for _, client in pairs(clients) do
+    if client.name == "emmet_ls" then
+      return true
+    end
+  end
+  return false
+end
+
+M.config = function()
+  local status_cmp_ok, cmp = pcall(require, "cmp")
+  if not status_cmp_ok then
+    return
+  end
+  local status_luasnip_ok, luasnip = pcall(require, "luasnip")
+  if not status_luasnip_ok then
+    return
+  end
+  lvim.builtin.cmp = {
+    formatting = {
+      format = function(entry, vim_item)
+        local icons = require("lsp.kind").icons
+        vim_item.kind = icons[vim_item.kind]
+        vim_item.menu = ({
+          nvim_lsp = "(LSP)",
+          emoji = "(Emoji)",
+          path = "(Path)",
+          calc = "(Calc)",
+          cmp_tabnine = "(Tabnine)",
+          vsnip = "(Snippet)",
+          luasnip = "(Snippet)",
+          buffer = "(Buffer)",
+        })[entry.source.name]
+        vim_item.dup = ({
+          buffer = 1,
+          path = 1,
+          nvim_lsp = 0,
+        })[entry.source.name] or 0
+        return vim_item
+      end,
+    },
+    snippet = {
+      expand = function(args)
+        require("luasnip").lsp_expand(args.body)
+      end,
+    },
+    documentation = {
+      border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" },
+    },
+    sources = {
+      { name = "nvim_lsp" },
+      { name = "path" },
+      { name = "luasnip" },
+      { name = "cmp_tabnine" },
+      { name = "nvim_lua" },
+      { name = "buffer" },
+      { name = "calc" },
+      { name = "emoji" },
+      { name = "treesitter" },
+      { name = "crates" },
+    },
+    mapping = {
+      ["<C-d>"] = cmp.mapping.scroll_docs(-4),
+      ["<C-f>"] = cmp.mapping.scroll_docs(4),
+      -- TODO: potentially fix emmet nonsense
+      ["<Tab>"] = cmp.mapping(function()
+        if vim.fn.pumvisible() == 1 then
+          vim.fn.feedkeys(T "<C-n>", "n")
+        elseif luasnip.expand_or_jumpable() then
+          vim.fn.feedkeys(T "<Plug>luasnip-expand-or-jump", "")
+        elseif check_backspace() then
+          vim.fn.feedkeys(T "<Tab>", "n")
+        elseif is_emmet_active() then
+          return vim.fn["cmp#complete"]()
+        else
+          vim.fn.feedkeys(T "<Tab>", "n")
+        end
+      end, {
+        "i",
+        "s",
+      }),
+      ["<S-Tab>"] = cmp.mapping(function(fallback)
+        if vim.fn.pumvisible() == 1 then
+          vim.fn.feedkeys(T "<C-p>", "n")
+        elseif luasnip.jumpable(-1) then
+          vim.fn.feedkeys(T "<Plug>luasnip-jump-prev", "")
+        else
+          fallback()
+        end
+      end, {
+        "i",
+        "s",
+      }),
+
+      ["<C-Space>"] = cmp.mapping.complete(),
+      ["<C-e>"] = cmp.mapping.close(),
+      ["<CR>"] = cmp.mapping.confirm {
+        behavior = cmp.ConfirmBehavior.Replace,
+        select = true,
+      },
+    },
+  }
+
+  M.setup = function()
+    require("luasnip/loaders/from_vscode").lazy_load()
+    require("cmp").setup(lvim.builtin.cmp)
+  end
+end
+
+return M

+ 0 - 133
lua/core/compe.lua

@@ -1,133 +0,0 @@
-local M = {}
-
-M.config = function()
-  lvim.builtin.compe = {
-    active = true,
-    on_config_done = nil,
-    autocomplete = true,
-    debug = false,
-    min_length = 1,
-    preselect = "enable",
-    throttle_time = 80,
-    source_timeout = 200,
-    incomplete_delay = 400,
-    max_abbr_width = 100,
-    max_kind_width = 100,
-    max_menu_width = 100,
-    documentation = {
-      border = "single",
-      winhighlight = "NormalFloat:CompeDocumentation,FloatBorder:CompeDocumentationBorder",
-      max_width = 120,
-      min_width = 60,
-      max_height = math.floor(vim.o.lines * 0.3),
-      min_height = 1,
-    },
-    -- documentation = true,
-
-    source = {
-      path = { kind = "   (Path)" },
-      buffer = { kind = "   (Buffer)" },
-      calc = { kind = "   (Calc)" },
-      vsnip = { kind = "   (Snippet)" },
-      nvim_lsp = { kind = "   (LSP)" },
-      nvim_lua = false,
-      spell = { kind = "   (Spell)" },
-      tags = false,
-      vim_dadbod_completion = false,
-      snippets_nvim = false,
-      ultisnips = false,
-      treesitter = false,
-      emoji = { kind = " ﲃ  (Emoji)", filetypes = { "markdown", "text" } },
-      -- for emoji press : (idk if that in compe tho)
-    },
-
-    keymap = {
-      values = {
-        insert_mode = {
-          -- ["<Tab>"] = { 'pumvisible() ? "<C-n>" : "<Tab>"', { silent = true, noremap = true, expr = true } },
-          -- ["<S-Tab>"] = { 'pumvisible() ? "<C-p>" : "<S-Tab>"', { silent = true, noremap = true, expr = true } },
-          ["<C-Space>"] = { "compe#complete()", { silent = true, noremap = true, expr = true } },
-          ["<C-e>"] = { "compe#close('<C-e>')", { silent = true, noremap = true, expr = true } },
-          ["<C-f>"] = { "compe#scroll({ 'delta': +4 })", { silent = true, noremap = true, expr = true } },
-          ["<C-d>"] = { "compe#scroll({ 'delta': -4 })", { silent = true, noremap = true, expr = true } },
-        },
-      },
-      opts = {
-        insert_mode = { noremap = true, silent = true, expr = true },
-      },
-    },
-  }
-end
-
-M.setup = function()
-  vim.g.vsnip_snippet_dir = lvim.vsnip_dir
-
-  local compe = require "compe"
-
-  compe.setup(lvim.builtin.compe)
-
-  local t = function(str)
-    return vim.api.nvim_replace_termcodes(str, true, true, true)
-  end
-
-  local check_back_space = function()
-    local col = vim.fn.col "." - 1
-    if col == 0 or vim.fn.getline("."):sub(col, col):match "%s" then
-      return true
-    else
-      return false
-    end
-  end
-
-  local is_emmet_active = function()
-    local clients = vim.lsp.buf_get_clients()
-
-    for _, client in pairs(clients) do
-      if client.name == "emmet_ls" then
-        return true
-      end
-    end
-    return false
-  end
-
-  -- Use (s-)tab to:
-  --- move to prev/next item in completion menuone
-  --- jump to prev/next snippet's placeholder
-  _G.tab_complete = function()
-    if vim.fn.pumvisible() == 1 then
-      return t "<C-n>"
-    elseif vim.fn.call("vsnip#jumpable", { 1 }) == 1 then
-      return t "<Plug>(vsnip-jump-next)"
-    elseif check_back_space() then
-      return t "<Tab>"
-    elseif is_emmet_active() then
-      return vim.fn["compe#complete"]()
-    else
-      return t "<Tab>"
-    end
-  end
-
-  _G.s_tab_complete = function()
-    if vim.fn.pumvisible() == 1 then
-      return t "<C-p>"
-    elseif vim.fn.call("vsnip#jumpable", { -1 }) == 1 then
-      return t "<Plug>(vsnip-jump-prev)"
-    else
-      return t "<S-Tab>"
-    end
-  end
-
-  local keymap = require "keymappings"
-  keymap.load(lvim.builtin.compe.keymap.values, lvim.builtin.compe.keymap.opts)
-
-  vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", { expr = true })
-  vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", { expr = true })
-  vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
-  vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
-
-  if lvim.builtin.compe.on_config_done then
-    lvim.builtin.compe.on_config_done(compe)
-  end
-end
-
-return M

+ 6 - 0
lua/lsp/init.lua

@@ -65,6 +65,12 @@ function M.common_capabilities()
       "additionalTextEdits",
     },
   }
+
+  local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
+  if not status_ok then
+    return
+  end
+  capabilities = cmp_nvim_lsp.update_capabilities(capabilities)
   return capabilities
 end
 

+ 31 - 0
lua/lsp/kind.lua

@@ -0,0 +1,31 @@
+local M = {}
+
+M.icons = {
+  Class = " ",
+  Color = " ",
+  Constant = "ﲀ ",
+  Constructor = " ",
+  Enum = "練",
+  EnumMember = " ",
+  Event = " ",
+  Field = " ",
+  File = "",
+  Folder = " ",
+  Function = " ",
+  Interface = "ﰮ ",
+  Keyword = " ",
+  Method = " ",
+  Module = " ",
+  Operator = "",
+  Property = " ",
+  Reference = " ",
+  Snippet = " ",
+  Struct = " ",
+  Text = " ",
+  TypeParameter = " ",
+  Unit = "塞",
+  Value = " ",
+  Variable = " ",
+}
+
+return M

+ 16 - 28
lua/plugins.lua

@@ -24,49 +24,36 @@ return {
     end,
     disable = not lvim.builtin.telescope.active,
   },
-
-  -- Completion & Snippets
+  -- Install nvim-cmp, and buffer source as a dependency
   {
-    "hrsh7th/nvim-compe",
-    event = "InsertEnter",
+    "hrsh7th/nvim-cmp",
     config = function()
-      require("core.compe").setup()
+      require("core.cmp").setup()
     end,
-    disable = not lvim.builtin.compe.active,
-    -- wants = "vim-vsnip",
-    -- requires = {
-    -- {
-    --   "hrsh7th/vim-vsnip",
-    --   wants = "friendly-snippets",
-    --   event = "InsertCharPre",
-    -- },
-    -- {
-    --   "rafamadriz/friendly-snippets",
-    --   event = "InsertCharPre",
-    -- },
-    -- },
-  },
-  {
-    "hrsh7th/vim-vsnip",
-    -- wants = "friendly-snippets",
-    event = "InsertEnter",
-    disable = not lvim.builtin.compe.active,
+    requires = {
+      "L3MON4D3/LuaSnip",
+      "saadparwaiz1/cmp_luasnip",
+      "hrsh7th/cmp-buffer",
+      "hrsh7th/cmp-nvim-lsp",
+      "hrsh7th/cmp-path",
+      "hrsh7th/cmp-nvim-lua",
+    },
   },
   {
     "rafamadriz/friendly-snippets",
-    event = "InsertCharPre",
-    disable = not lvim.builtin.compe.active,
+    -- event = "InsertCharPre",
+    -- disable = not lvim.builtin.compe.active,
   },
 
   -- Autopairs
   {
     "windwp/nvim-autopairs",
     -- event = "InsertEnter",
-    after = "nvim-compe",
+    after = "nvim-cmp",
     config = function()
       require("core.autopairs").setup()
     end,
-    disable = not lvim.builtin.autopairs.active or not lvim.builtin.compe.active,
+    disable = not lvim.builtin.autopairs.active,
   },
 
   -- Treesitter
@@ -98,6 +85,7 @@ return {
       require("core.gitsigns").setup()
     end,
     event = "BufRead",
+    commit = "25d4b182ece5b03cd4b2d8c196f3c38e0df58801",
     disable = not lvim.builtin.gitsigns.active,
   },