setup.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. -- https://github.com/neovim/nvim-lspconfig/wiki/Autocompletion
  2. -- https://github.com/hrsh7th/nvim-cmp
  3. -- https://github.com/onsails/lspkind-nvim
  4. local status, cmp = pcall(require, "cmp")
  5. if not status then
  6. return
  7. end
  8. local status, luasnip = pcall(require, "luasnip")
  9. if not status then
  10. vim.notify("not fount luasnip")
  11. return
  12. end
  13. local status, config = pcall(require, "uConfig")
  14. if not status then
  15. return
  16. end
  17. local has_words_before = function()
  18. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  19. return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
  20. end
  21. local mapping = {
  22. -- 出现补全
  23. [config.keys.cmp_complete] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
  24. -- 取消
  25. [config.keys.cmp_abort] = cmp.mapping({
  26. i = cmp.mapping.abort(),
  27. c = cmp.mapping.close(),
  28. }),
  29. -- 确认
  30. -- Accept currently selected item. If none selected, `select` first item.
  31. -- Set `select` to `false` to only confirm explicitly selected items.
  32. [config.keys.cmp_confirm] = cmp.mapping.confirm({
  33. select = true,
  34. behavior = cmp.ConfirmBehavior.Replace,
  35. }),
  36. -- 如果窗口内容太多,可以滚动
  37. [config.keys.cmp_scroll_doc_up] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
  38. [config.keys.cmp_scroll_doc_down] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
  39. -- 上一个
  40. [config.keys.cmp_select_prev_item] = cmp.mapping.select_prev_item(),
  41. -- 下一个
  42. [config.keys.cmp_select_next_item] = cmp.mapping.select_next_item(),
  43. }
  44. cmp.setup({
  45. -- 指定 snippet 引擎 luasnip
  46. snippet = {
  47. expand = function(args)
  48. luasnip.lsp_expand(args.body)
  49. end,
  50. },
  51. window = {
  52. completion = cmp.config.window.bordered(),
  53. -- documentation = cmp.config.window.bordered(),
  54. },
  55. -- 快捷键
  56. -- mapping = mapping,
  57. mapping = mapping,
  58. -- 来源
  59. sources = cmp.config.sources({
  60. {
  61. name = "luasnip",
  62. group_index = 1,
  63. },
  64. {
  65. name = "nvim_lsp",
  66. group_index = 1,
  67. },
  68. {
  69. name = "nvim_lsp_signature_help",
  70. group_index = 1,
  71. },
  72. {
  73. name = "buffer",
  74. group_index = 2,
  75. },
  76. {
  77. name = "path",
  78. group_index = 2,
  79. },
  80. }),
  81. -- 使用lspkind-nvim显示类型图标
  82. formatting = require("cmp.lspkind").formatting,
  83. })
  84. -- Use buffer source for `/`.
  85. cmp.setup.cmdline("/", {
  86. mapping = cmp.mapping.preset.cmdline(),
  87. sources = { {
  88. name = "buffer",
  89. } },
  90. })
  91. -- Use cmdline & path source for ':'.
  92. cmp.setup.cmdline(":", {
  93. mapping = cmp.mapping.preset.cmdline(),
  94. sources = cmp.config.sources({ {
  95. name = "path",
  96. } }, { {
  97. name = "cmdline",
  98. } }),
  99. })
  100. cmp.setup.filetype({ "markdown", "help" }, {
  101. sources = { {
  102. name = "luasnip",
  103. }, {
  104. name = "buffer",
  105. }, {
  106. name = "path",
  107. } },
  108. })
  109. require("cmp.luasnip")