You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.4 KiB

  1. local M = {}
  2. function M.setup()
  3. local has_words_before = function()
  4. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  5. return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
  6. end
  7. local luasnip = require("luasnip")
  8. local cmp = require("cmp")
  9. cmp.setup {
  10. completion = { completeopt = "menu,menuone,noinsert", keyword_length = 1 },
  11. experimental = { native_menu = false, ghost_text = false },
  12. snippet = {
  13. expand = function(args)
  14. require("luasnip").lsp_expand(args.body)
  15. end,
  16. },
  17. formatting = {
  18. format = function(entry, vim_item)
  19. vim_item.menu = ({
  20. nvim_lsp = "[LSP]",
  21. buffer = "[Buffer]",
  22. luasnip = "[Snip]",
  23. nvim_lua = "[Lua]",
  24. treesitter = "[Treesitter]",
  25. path = "[Path]",
  26. })[entry.source.name]
  27. return vim_item
  28. end,
  29. },
  30. mapping = {
  31. ["<C-k>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
  32. ["<C-j>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
  33. ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
  34. ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
  35. ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
  36. ["<C-e>"] = cmp.mapping { i = cmp.mapping.close(), c = cmp.mapping.close() },
  37. ["<CR>"] = cmp.mapping {
  38. i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
  39. c = function(fallback)
  40. if cmp.visible() then
  41. cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
  42. else
  43. fallback()
  44. end
  45. end,
  46. },
  47. ["<Tab>"] = cmp.mapping(
  48. function(fallback)
  49. if cmp.visible() then
  50. cmp.select_next_item()
  51. elseif luasnip.expand_or_jumpable() then
  52. luasnip.expand_or_jump()
  53. elseif has_words_before() then
  54. cmp.complete()
  55. else
  56. fallback()
  57. end
  58. end, {
  59. "i",
  60. "s",
  61. "c",
  62. }),
  63. ["<S-Tab>"] = cmp.mapping(function(fallback)
  64. if cmp.visible() then
  65. cmp.select_prev_item()
  66. elseif luasnip.jumpable(-1) then
  67. luasnip.jump(-1)
  68. else
  69. fallback()
  70. end
  71. end, {
  72. "i",
  73. "s",
  74. "c",
  75. }),
  76. },
  77. sources = {
  78. { name = "nvim_lsp" },
  79. { name = "treesitter" },
  80. { name = "buffer" },
  81. { name = "luasnip" },
  82. { name = "nvim_lua" },
  83. { name = "path" },
  84. -- { name = "spell" },
  85. -- { name = "emoji" },
  86. -- { name = "calc" },
  87. },
  88. window = {
  89. documentation = {
  90. border = { "", "", "", "", "", "", "", "" },
  91. winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder",
  92. },
  93. },
  94. }
  95. -- Use buffer source for `/`
  96. cmp.setup.cmdline("/", {
  97. mapping = cmp.mapping.preset.cmdline(),
  98. sources = {
  99. { name = "buffer" },
  100. },
  101. })
  102. -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
  103. cmp.setup.cmdline(':', {
  104. mapping = cmp.mapping.preset.cmdline(),
  105. sources = cmp.config.sources({
  106. { name = 'path' }
  107. }, {
  108. { name = 'cmdline' }
  109. })
  110. })
  111. end
  112. return M