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.

118 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. }),
  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. }),
  74. },
  75. sources = {
  76. { name = "nvim_lsp" },
  77. { name = "treesitter" },
  78. { name = "buffer" },
  79. { name = "luasnip" },
  80. { name = "nvim_lua" },
  81. { name = "path" },
  82. -- { name = "spell" },
  83. -- { name = "emoji" },
  84. -- { name = "calc" },
  85. },
  86. window = {
  87. documentation = {
  88. border = { "", "", "", "", "", "", "", "" },
  89. winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder",
  90. },
  91. },
  92. }
  93. -- Use buffer source for `/`
  94. cmp.setup.cmdline("/", {
  95. enabled = false
  96. -- mapping = cmp.mapping.preset.cmdline(),
  97. -- sources = {
  98. -- { name = "buffer" },
  99. -- },
  100. })
  101. -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
  102. cmp.setup.cmdline(':', {
  103. sources = cmp.config.sources({
  104. { name = 'path' }
  105. }, {
  106. { name = 'cmdline' }
  107. })
  108. })
  109. end
  110. return M