cmp.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. local M = {}
  2. local check_backspace = function()
  3. local col = vim.fn.col "." - 1
  4. return col == 0 or vim.fn.getline("."):sub(col, col):match "%s"
  5. end
  6. local function T(str)
  7. return vim.api.nvim_replace_termcodes(str, true, true, true)
  8. end
  9. local is_emmet_active = function()
  10. local clients = vim.lsp.buf_get_clients()
  11. for _, client in pairs(clients) do
  12. if client.name == "emmet_ls" then
  13. return true
  14. end
  15. end
  16. return false
  17. end
  18. M.config = function()
  19. local status_cmp_ok, cmp = pcall(require, "cmp")
  20. if not status_cmp_ok then
  21. return
  22. end
  23. local status_luasnip_ok, luasnip = pcall(require, "luasnip")
  24. if not status_luasnip_ok then
  25. return
  26. end
  27. lvim.builtin.cmp = {
  28. confirm_opts = {
  29. behavior = cmp.ConfirmBehavior.Replace,
  30. select = true,
  31. },
  32. formatting = {
  33. format = function(entry, vim_item)
  34. local icons = require("lsp.kind").icons
  35. vim_item.kind = icons[vim_item.kind]
  36. vim_item.menu = ({
  37. nvim_lsp = "(LSP)",
  38. emoji = "(Emoji)",
  39. path = "(Path)",
  40. calc = "(Calc)",
  41. cmp_tabnine = "(Tabnine)",
  42. vsnip = "(Snippet)",
  43. luasnip = "(Snippet)",
  44. buffer = "(Buffer)",
  45. })[entry.source.name]
  46. vim_item.dup = ({
  47. buffer = 1,
  48. path = 1,
  49. nvim_lsp = 0,
  50. })[entry.source.name] or 0
  51. return vim_item
  52. end,
  53. },
  54. snippet = {
  55. expand = function(args)
  56. require("luasnip").lsp_expand(args.body)
  57. end,
  58. },
  59. documentation = {
  60. border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" },
  61. },
  62. sources = {
  63. { name = "nvim_lsp" },
  64. { name = "path" },
  65. { name = "luasnip" },
  66. { name = "cmp_tabnine" },
  67. { name = "nvim_lua" },
  68. { name = "buffer" },
  69. { name = "calc" },
  70. { name = "emoji" },
  71. { name = "treesitter" },
  72. { name = "crates" },
  73. },
  74. mapping = {
  75. ["<C-d>"] = cmp.mapping.scroll_docs(-4),
  76. ["<C-f>"] = cmp.mapping.scroll_docs(4),
  77. -- TODO: potentially fix emmet nonsense
  78. ["<Tab>"] = cmp.mapping(function()
  79. if vim.fn.pumvisible() == 1 then
  80. vim.fn.feedkeys(T "<down>", "n")
  81. elseif luasnip.expand_or_jumpable() then
  82. vim.fn.feedkeys(T "<Plug>luasnip-expand-or-jump", "")
  83. elseif check_backspace() then
  84. vim.fn.feedkeys(T "<Tab>", "n")
  85. elseif is_emmet_active() then
  86. return vim.fn["cmp#complete"]()
  87. else
  88. vim.fn.feedkeys(T "<Tab>", "n")
  89. end
  90. end, {
  91. "i",
  92. "s",
  93. }),
  94. ["<S-Tab>"] = cmp.mapping(function(fallback)
  95. if vim.fn.pumvisible() == 1 then
  96. vim.fn.feedkeys(T "<up>", "n")
  97. elseif luasnip.jumpable(-1) then
  98. vim.fn.feedkeys(T "<Plug>luasnip-jump-prev", "")
  99. else
  100. fallback()
  101. end
  102. end, {
  103. "i",
  104. "s",
  105. }),
  106. ["<C-Space>"] = cmp.mapping.complete(),
  107. ["<C-e>"] = cmp.mapping.close(),
  108. ["<CR>"] = cmp.mapping(function(fallback)
  109. if not require("cmp").confirm(lvim.builtin.cmp.confirm_opts) then
  110. if luasnip.jumpable() then
  111. vim.fn.feedkeys(T "<Plug>luasnip-jump-next", "")
  112. else
  113. fallback()
  114. end
  115. end
  116. end),
  117. },
  118. }
  119. end
  120. M.setup = function()
  121. require("luasnip/loaders/from_vscode").lazy_load()
  122. require("cmp").setup(lvim.builtin.cmp)
  123. end
  124. return M