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.

116 lines
3.0 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(function(fallback)
  48. if cmp.visible() then
  49. cmp.select_next_item()
  50. elseif luasnip.expand_or_jumpable() then
  51. luasnip.expand_or_jump()
  52. elseif has_words_before() then
  53. cmp.complete()
  54. else
  55. fallback()
  56. end
  57. end, {
  58. "i",
  59. "s",
  60. "c",
  61. }),
  62. ["<S-Tab>"] = cmp.mapping(function(fallback)
  63. if cmp.visible() then
  64. cmp.select_prev_item()
  65. elseif luasnip.jumpable(-1) then
  66. luasnip.jump(-1)
  67. else
  68. fallback()
  69. end
  70. end, {
  71. "i",
  72. "s",
  73. "c",
  74. }),
  75. },
  76. sources = {
  77. { name = "nvim_lsp" },
  78. { name = "treesitter" },
  79. { name = "buffer" },
  80. { name = "luasnip" },
  81. { name = "nvim_lua" },
  82. { name = "path" },
  83. -- { name = "spell" },
  84. -- { name = "emoji" },
  85. -- { name = "calc" },
  86. },
  87. window = {
  88. documentation = {
  89. border = { "", "", "", "", "", "", "", "" },
  90. winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder",
  91. },
  92. },
  93. }
  94. -- Use buffer source for `/`
  95. cmp.setup.cmdline("/", {
  96. sources = {
  97. { name = "buffer" },
  98. },
  99. })
  100. -- Use cmdline & path source for ':'
  101. cmp.setup.cmdline(":", {
  102. sources = cmp.config.sources({
  103. { name = "path" },
  104. }, {
  105. { name = "cmdline" },
  106. }),
  107. })
  108. end
  109. return M