autocomplete.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. -- [[ Configure nvim-cmp ]]
  2. -- See `:help cmp`
  3. local cmp = require 'cmp'
  4. local luasnip = require 'luasnip'
  5. require('luasnip.loaders.from_vscode').lazy_load()
  6. luasnip.config.setup {}
  7. cmp.setup {
  8. enabled = function()
  9. -- disable completion in comments
  10. local context = require 'cmp.config.context'
  11. -- keep command mode completion enabled when cursor is in a comment
  12. if vim.api.nvim_get_mode().mode == 'c' then
  13. return true
  14. else
  15. return not context.in_treesitter_capture 'comment' and not context.in_syntax_group 'Comment'
  16. end
  17. end,
  18. snippet = {
  19. expand = function(args)
  20. luasnip.lsp_expand(args.body)
  21. end,
  22. },
  23. completion = {
  24. completeopt = 'menu,menuone,noinsert',
  25. },
  26. mapping = cmp.mapping.preset.insert {
  27. ['<C-n>'] = cmp.mapping.select_next_item(),
  28. ['<C-p>'] = cmp.mapping.select_prev_item(),
  29. ['<C-b>'] = cmp.mapping.scroll_docs(-4),
  30. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  31. ['<C-Space>'] = cmp.mapping.complete {},
  32. ['<CR>'] = cmp.mapping.confirm {
  33. behavior = cmp.ConfirmBehavior.Replace,
  34. select = true,
  35. },
  36. ['<Tab>'] = cmp.mapping(function(fallback)
  37. if cmp.visible() then
  38. cmp.select_next_item()
  39. elseif luasnip.expand_or_locally_jumpable() then
  40. luasnip.expand_or_jump()
  41. else
  42. fallback()
  43. end
  44. end, { 'i', 's' }),
  45. ['<S-Tab>'] = cmp.mapping(function(fallback)
  46. if cmp.visible() then
  47. cmp.select_prev_item()
  48. elseif luasnip.locally_jumpable(-1) then
  49. luasnip.jump(-1)
  50. else
  51. fallback()
  52. end
  53. end, { 'i', 's' }),
  54. },
  55. sources = {
  56. { name = 'nvim_lsp' },
  57. { name = 'luasnip' },
  58. { name = 'path' },
  59. },
  60. }