cmp.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. -- import nvim-cmp plugin safely
  2. local cmp_status, cmp = pcall(require, "cmp")
  3. if not cmp_status then
  4. return
  5. end
  6. -- import luasnip plugin safely
  7. local luasnip_status, luasnip = pcall(require, "luasnip")
  8. if not luasnip_status then
  9. vim.notify("没有找到luasnip")
  10. return
  11. end
  12. -- import lspkind plugin safely
  13. local lspkind_status, lspkind = pcall(require, "lspkind")
  14. if not lspkind_status then
  15. vim.notify("没有找到lspkind")
  16. return
  17. end
  18. -- load vs-code like snippets from plugins (e.g. friendly-snippets)
  19. -- require("luasnip/loaders/from_vscode").lazy_load()
  20. vim.opt.completeopt = "menu,menuone,noselect"
  21. cmp.setup({
  22. snippet = {
  23. expand = function(args)
  24. luasnip.lsp_expand(args.body)
  25. end,
  26. },
  27. mapping = cmp.mapping.preset.insert({
  28. ["<S-Tab>"] = cmp.mapping.select_prev_item(), -- previous suggestion
  29. ["<Tab>"] = cmp.mapping.select_next_item(), -- next suggestion
  30. ["<C-b>"] = cmp.mapping.scroll_docs(-4),
  31. ["<C-f>"] = cmp.mapping.scroll_docs(4),
  32. ["<C-Space>"] = cmp.mapping.complete(), -- show completion suggestions
  33. ["<C-e>"] = cmp.mapping.abort(), -- close completion window
  34. ["<CR>"] = cmp.mapping.confirm({ select = false }),
  35. }),
  36. -- sources for autocompletion
  37. sources = cmp.config.sources({
  38. { name = "nvim_lsp" }, -- lsp
  39. { name = "luasnip" }, -- snippets
  40. { name = "buffer" }, -- text within current buffer
  41. { name = "path" }, -- file system paths
  42. }),
  43. -- configure lspkind for vs-code like icons
  44. formatting = {
  45. format = lspkind.cmp_format({
  46. maxwidth = 50,
  47. ellipsis_char = "...",
  48. }),
  49. },
  50. })
  51. -- / 查找模式使用 buffer 源
  52. cmp.setup.cmdline("/", {
  53. mapping = cmp.mapping.preset.cmdline(),
  54. sources = {
  55. { name = "buffer" },
  56. },
  57. })
  58. -- : 命令行模式中使用 path 和 cmdline 源.
  59. cmp.setup.cmdline(":", {
  60. mapping = cmp.mapping.preset.cmdline(),
  61. sources = cmp.config.sources({
  62. { name = "path" },
  63. }, {
  64. { name = "cmdline" },
  65. }),
  66. })