nvim-cmp.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. -- local has_words_before = function()
  2. -- unpack = unpack or table.unpack
  3. -- local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  4. -- return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
  5. -- end
  6. return {
  7. {
  8. "L3MON4D3/LuaSnip",
  9. keys = function()
  10. return {}
  11. end,
  12. },
  13. -- then: setup supertab in cmp
  14. {
  15. "hrsh7th/nvim-cmp",
  16. dependencies = {
  17. "hrsh7th/cmp-emoji",
  18. },
  19. ---@param opts cmp.ConfigSchema
  20. opts = function(_, opts)
  21. local has_words_before = function()
  22. unpack = unpack or table.unpack
  23. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  24. return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
  25. end
  26. local luasnip = require("luasnip")
  27. local cmp = require("cmp")
  28. opts.mapping = vim.tbl_extend("force", opts.mapping, {
  29. -- Define the down arrow to select the next item in the completion menu
  30. ["<Down>"] = cmp.mapping(function(fallback)
  31. if cmp.visible() then
  32. cmp.select_next_item()
  33. elseif luasnip.expand_or_jumpable() then
  34. luasnip.expand_or_jump()
  35. else
  36. fallback()
  37. end
  38. end, { "i", "s" }),
  39. -- Define the up arrow to select the previous item in the completion menu
  40. ["<Up>"] = cmp.mapping(function(fallback)
  41. if cmp.visible() then
  42. cmp.select_prev_item()
  43. elseif luasnip.jumpable(-1) then
  44. luasnip.jump(-1)
  45. else
  46. fallback()
  47. end
  48. end, { "i", "s" }),
  49. -- ["<Tab>"] = cmp.mapping(function(fallback)
  50. -- if cmp.visible() then
  51. -- cmp.select_next_item()
  52. -- -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
  53. -- -- this way you will only jump inside the snippet region
  54. -- elseif luasnip.expand_or_jumpable() then
  55. -- luasnip.expand_or_jump()
  56. -- elseif has_words_before() then
  57. -- cmp.complete()
  58. -- else
  59. -- fallback()
  60. -- end
  61. -- end, { "i", "s" }),
  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, { "i", "s" }),
  71. ["<CR>"] = cmp.mapping.confirm({
  72. behavior = cmp.ConfirmBehavior.Replace,
  73. select = true,
  74. }),
  75. ["<C-Space>"] = cmp.mapping.complete(),
  76. ["<Esc>"] = cmp.mapping({
  77. i = cmp.mapping.abort(),
  78. c = function()
  79. if cmp.visible() then
  80. cmp.close()
  81. else
  82. vim.api.nvim_feedkays(vim.api.nvim_replace_termcodes("<C-c>", true, true, true), "n", true)
  83. end
  84. end,
  85. }),
  86. --["<C-t>"] = {
  87. -- "copilot#Accept('\\<CR>')",
  88. -- desc = "copilot expand",
  89. -- silent = true,
  90. -- expr = true,
  91. -- script = true
  92. --},
  93. })
  94. end,
  95. },
  96. }
  97. -- vim.opt.completeopt = "menu,menuone,noselect"
  98. --[[
  99. return {
  100. "nvim-cmp",
  101. dependencies = {
  102. "hrsh7th/cmp-emoji",
  103. {
  104. "zbirenbaum/copilot-cmp",
  105. opts = {}
  106. },
  107. "L3MON4D3/LuaSnip",
  108. },
  109. config = function(_, opts)
  110. -- TODO: This is should be the cmp configuration with its custom bindings?
  111. local cmp = require("cmp")
  112. local luasnip = require("luasnip")
  113. opts.mapping = {
  114. ["<Tab>"] = cmp.mapping(
  115. function(fallback)
  116. if cmp.visible() then
  117. cmp.select_next_item()
  118. elseif luasnip.expand_or_jumpable() then
  119. luasnip.expand_or_jump()
  120. elseif has_words_before() then
  121. cmp.complete()
  122. else
  123. fallback()
  124. end
  125. end, { "i", "s" }),
  126. ["<S-Tab>"] = cmp.mapping(
  127. function(fallback)
  128. if cmp.visible() then
  129. cmp.select_prev_item()
  130. elseif luasnip.jumpable(-1) then
  131. luasnip.jump(-1)
  132. else
  133. fallback()
  134. end
  135. end,
  136. { "i", "s" }),
  137. ["<CR>"] = cmp.mapping()
  138. }
  139. cmp.setup(opts)
  140. end,
  141. }
  142. ]]
  143. -- M.opts = function(_, opts)
  144. --
  145. -- local cmp = require("cmp")
  146. -- opts.sources = cmp.config.sources(vim.list_extend(
  147. -- opts.sources,
  148. -- {
  149. -- { name = "copilot" },
  150. -- { name = "emoji" }
  151. -- }
  152. -- ))
  153. -- opts.mapping = cmp.config.mapping(vim.list_extend(
  154. -- opts.mapping,
  155. -- {
  156. -- ["<C-Space>"] = cmp.mapping.complete(),
  157. -- ["<Esc>"] = cmp.mapping.close(),
  158. -- ["<CR>"] = cmp.mapping.confirm({
  159. -- behavior = cmp.ConfirmBehavior.Replace,
  160. -- select = true,
  161. -- }),
  162. -- }
  163. -- ))
  164. --
  165. -- opts.mapping["<Tab>"] = cmp.mapping.select_next_item()
  166. -- opts.mapping["<S-Tab>"] = cmp.mapping.select_prev_item()
  167. -- end
  168. --return M