cmp.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. local M = {}
  2. local check_backspace = function()
  3. local col = vim.fn.col "." - 1
  4. return col == 0 or vim.fn.getline("."):sub(col, col):match "%s"
  5. end
  6. local function T(str)
  7. return vim.api.nvim_replace_termcodes(str, true, true, true)
  8. end
  9. local is_emmet_active = function()
  10. local clients = vim.lsp.buf_get_clients()
  11. for _, client in pairs(clients) do
  12. if client.name == "emmet_ls" then
  13. return true
  14. end
  15. end
  16. return false
  17. end
  18. M.config = function()
  19. local status_cmp_ok, cmp = pcall(require, "cmp")
  20. if not status_cmp_ok then
  21. return
  22. end
  23. local status_luasnip_ok, luasnip = pcall(require, "luasnip")
  24. if not status_luasnip_ok then
  25. return
  26. end
  27. local win_get_cursor = vim.api.nvim_win_get_cursor
  28. local get_current_buf = vim.api.nvim_get_current_buf
  29. local function inside_snippet()
  30. -- for outdated versions of luasnip
  31. if not luasnip.session.current_nodes then
  32. return false
  33. end
  34. local node = luasnip.session.current_nodes[get_current_buf()]
  35. if not node then
  36. return false
  37. end
  38. local snip_begin_pos, snip_end_pos = node.parent.snippet.mark:pos_begin_end()
  39. local pos = win_get_cursor(0)
  40. pos[1] = pos[1] - 1 -- LuaSnip is 0-based not 1-based like nvim for rows
  41. return pos[1] >= snip_begin_pos[1] and pos[1] <= snip_end_pos[1]
  42. end
  43. ---sets the current buffer's luasnip to the one nearest the cursor
  44. ---@return boolean true if a node is found, false otherwise
  45. local function seek_luasnip_cursor_node()
  46. -- for outdated versions of luasnip
  47. if not luasnip.session.current_nodes then
  48. return false
  49. end
  50. local pos = win_get_cursor(0)
  51. pos[1] = pos[1] - 1
  52. local node = luasnip.session.current_nodes[get_current_buf()]
  53. if not node then
  54. return false
  55. end
  56. local snippet = node.parent.snippet
  57. local exit_node = snippet.insert_nodes[0]
  58. -- exit early if we're past the exit node
  59. if exit_node then
  60. local exit_pos_end = exit_node.mark:pos_end()
  61. if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
  62. snippet:remove_from_jumplist()
  63. luasnip.session.current_nodes[get_current_buf()] = nil
  64. return false
  65. end
  66. end
  67. node = snippet.inner_first:jump_into(1, true)
  68. while node ~= nil and node.next ~= nil and node ~= snippet do
  69. local n_next = node.next
  70. local next_pos = n_next and n_next.mark:pos_begin()
  71. local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
  72. or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
  73. -- Past unmarked exit node, exit early
  74. if n_next == nil or n_next == snippet.next then
  75. snippet:remove_from_jumplist()
  76. luasnip.session.current_nodes[get_current_buf()] = nil
  77. return false
  78. end
  79. if candidate then
  80. luasnip.session.current_nodes[get_current_buf()] = node
  81. return true
  82. end
  83. local ok
  84. ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
  85. if not ok then
  86. snippet:remove_from_jumplist()
  87. luasnip.session.current_nodes[get_current_buf()] = nil
  88. return false
  89. end
  90. end
  91. -- No candidate, but have an exit node
  92. if exit_node then
  93. -- to jump to the exit node, seek to snippet
  94. luasnip.session.current_nodes[get_current_buf()] = snippet
  95. return true
  96. end
  97. -- No exit node, exit from snippet
  98. snippet:remove_from_jumplist()
  99. luasnip.session.current_nodes[get_current_buf()] = nil
  100. return false
  101. end
  102. lvim.builtin.cmp = {
  103. confirm_opts = {
  104. behavior = cmp.ConfirmBehavior.Replace,
  105. select = false,
  106. },
  107. experimental = {
  108. ghost_text = true,
  109. native_menu = false,
  110. },
  111. formatting = {
  112. kind_icons = {
  113. Class = " ",
  114. Color = " ",
  115. Constant = "ﲀ ",
  116. Constructor = " ",
  117. Enum = "練",
  118. EnumMember = " ",
  119. Event = " ",
  120. Field = " ",
  121. File = "",
  122. Folder = " ",
  123. Function = " ",
  124. Interface = "ﰮ ",
  125. Keyword = " ",
  126. Method = " ",
  127. Module = " ",
  128. Operator = "",
  129. Property = " ",
  130. Reference = " ",
  131. Snippet = " ",
  132. Struct = " ",
  133. Text = " ",
  134. TypeParameter = " ",
  135. Unit = "塞",
  136. Value = " ",
  137. Variable = " ",
  138. },
  139. source_names = {
  140. nvim_lsp = "(LSP)",
  141. emoji = "(Emoji)",
  142. path = "(Path)",
  143. calc = "(Calc)",
  144. cmp_tabnine = "(Tabnine)",
  145. vsnip = "(Snippet)",
  146. luasnip = "(Snippet)",
  147. buffer = "(Buffer)",
  148. },
  149. duplicates = {
  150. buffer = 1,
  151. path = 1,
  152. nvim_lsp = 0,
  153. luasnip = 1,
  154. },
  155. duplicates_default = 0,
  156. format = function(entry, vim_item)
  157. vim_item.kind = lvim.builtin.cmp.formatting.kind_icons[vim_item.kind]
  158. vim_item.menu = lvim.builtin.cmp.formatting.source_names[entry.source.name]
  159. vim_item.dup = lvim.builtin.cmp.formatting.duplicates[entry.source.name]
  160. or lvim.builtin.cmp.formatting.duplicates_default
  161. return vim_item
  162. end,
  163. },
  164. snippet = {
  165. expand = function(args)
  166. require("luasnip").lsp_expand(args.body)
  167. end,
  168. },
  169. documentation = {
  170. border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" },
  171. },
  172. sources = {
  173. { name = "nvim_lsp" },
  174. { name = "path" },
  175. { name = "luasnip" },
  176. { name = "cmp_tabnine" },
  177. { name = "nvim_lua" },
  178. { name = "buffer" },
  179. { name = "calc" },
  180. { name = "emoji" },
  181. { name = "treesitter" },
  182. { name = "crates" },
  183. },
  184. mapping = {
  185. ["<C-k>"] = cmp.mapping.select_prev_item(),
  186. ["<C-j>"] = cmp.mapping.select_next_item(),
  187. ["<C-d>"] = cmp.mapping.scroll_docs(-4),
  188. ["<C-f>"] = cmp.mapping.scroll_docs(4),
  189. -- TODO: potentially fix emmet nonsense
  190. ["<Tab>"] = cmp.mapping(function()
  191. if cmp.visible() then
  192. cmp.select_next_item()
  193. elseif luasnip.expandable() then
  194. luasnip.expand()
  195. elseif inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() then
  196. luasnip.jump(1)
  197. elseif check_backspace() then
  198. vim.fn.feedkeys(T "<Tab>", "n")
  199. elseif is_emmet_active() then
  200. return vim.fn["cmp#complete"]()
  201. else
  202. vim.fn.feedkeys(T "<Tab>", "n")
  203. end
  204. end, {
  205. "i",
  206. "s",
  207. }),
  208. ["<S-Tab>"] = cmp.mapping(function(fallback)
  209. if cmp.visible() then
  210. cmp.select_prev_item()
  211. elseif inside_snippet() and luasnip.jumpable(-1) then
  212. luasnip.jump(-1)
  213. else
  214. fallback()
  215. end
  216. end, {
  217. "i",
  218. "s",
  219. }),
  220. ["<C-Space>"] = cmp.mapping.complete(),
  221. ["<C-e>"] = cmp.mapping.close(),
  222. ["<CR>"] = cmp.mapping(function(fallback)
  223. if cmp.visible() and cmp.confirm(lvim.builtin.cmp.confirm_opts) then
  224. return
  225. end
  226. if inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() then
  227. if not luasnip.jump(1) then
  228. fallback()
  229. end
  230. else
  231. fallback()
  232. end
  233. end),
  234. },
  235. }
  236. end
  237. M.setup = function()
  238. require("luasnip/loaders/from_vscode").lazy_load()
  239. require("cmp").setup(lvim.builtin.cmp)
  240. end
  241. return M