cmp.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. local M = {}
  2. M.methods = {}
  3. local has_words_before = function()
  4. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  5. return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
  6. end
  7. M.methods.has_words_before = has_words_before
  8. ---@deprecated use M.methods.has_words_before instead
  9. M.methods.check_backspace = function()
  10. return not has_words_before()
  11. end
  12. local T = function(str)
  13. return vim.api.nvim_replace_termcodes(str, true, true, true)
  14. end
  15. local function feedkeys(key, mode)
  16. vim.api.nvim_feedkeys(T(key), mode, true)
  17. end
  18. M.methods.feedkeys = feedkeys
  19. ---when inside a snippet, seeks to the nearest luasnip field if possible, and checks if it is jumpable
  20. ---@param dir number 1 for forward, -1 for backward; defaults to 1
  21. ---@return boolean true if a jumpable luasnip field is found while inside a snippet
  22. local function jumpable(dir)
  23. local luasnip_ok, luasnip = pcall(require, "luasnip")
  24. if not luasnip_ok then
  25. return false
  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. ---sets the current buffer's luasnip to the one nearest the cursor
  30. ---@return boolean true if a node is found, false otherwise
  31. local function seek_luasnip_cursor_node()
  32. -- TODO(kylo252): upstream this
  33. -- for outdated versions of luasnip
  34. if not luasnip.session.current_nodes then
  35. return false
  36. end
  37. local node = luasnip.session.current_nodes[get_current_buf()]
  38. if not node then
  39. return false
  40. end
  41. local snippet = node.parent.snippet
  42. local exit_node = snippet.insert_nodes[0]
  43. local pos = win_get_cursor(0)
  44. pos[1] = pos[1] - 1
  45. -- exit early if we're past the exit node
  46. if exit_node then
  47. local exit_pos_end = exit_node.mark:pos_end()
  48. if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
  49. snippet:remove_from_jumplist()
  50. luasnip.session.current_nodes[get_current_buf()] = nil
  51. return false
  52. end
  53. end
  54. node = snippet.inner_first:jump_into(1, true)
  55. while node ~= nil and node.next ~= nil and node ~= snippet do
  56. local n_next = node.next
  57. local next_pos = n_next and n_next.mark:pos_begin()
  58. local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
  59. or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
  60. -- Past unmarked exit node, exit early
  61. if n_next == nil or n_next == snippet.next then
  62. snippet:remove_from_jumplist()
  63. luasnip.session.current_nodes[get_current_buf()] = nil
  64. return false
  65. end
  66. if candidate then
  67. luasnip.session.current_nodes[get_current_buf()] = node
  68. return true
  69. end
  70. local ok
  71. ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
  72. if not ok then
  73. snippet:remove_from_jumplist()
  74. luasnip.session.current_nodes[get_current_buf()] = nil
  75. return false
  76. end
  77. end
  78. -- No candidate, but have an exit node
  79. if exit_node then
  80. -- to jump to the exit node, seek to snippet
  81. luasnip.session.current_nodes[get_current_buf()] = snippet
  82. return true
  83. end
  84. -- No exit node, exit from snippet
  85. snippet:remove_from_jumplist()
  86. luasnip.session.current_nodes[get_current_buf()] = nil
  87. return false
  88. end
  89. if dir == -1 then
  90. return luasnip.in_snippet() and luasnip.jumpable(-1)
  91. else
  92. return luasnip.in_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable(1)
  93. end
  94. end
  95. M.methods.jumpable = jumpable
  96. M.config = function()
  97. local status_cmp_ok, cmp_types = pcall(require, "cmp.types.cmp")
  98. if not status_cmp_ok then
  99. return
  100. end
  101. local ConfirmBehavior = cmp_types.ConfirmBehavior
  102. local SelectBehavior = cmp_types.SelectBehavior
  103. local cmp = require("lvim.utils.modules").require_on_index "cmp"
  104. local luasnip = require("lvim.utils.modules").require_on_index "luasnip"
  105. local cmp_window = require "cmp.config.window"
  106. local cmp_mapping = require "cmp.config.mapping"
  107. lvim.builtin.cmp = {
  108. active = true,
  109. on_config_done = nil,
  110. enabled = function()
  111. local buftype = vim.api.nvim_get_option_value("buftype", { buf = 0 })
  112. if buftype == "prompt" then
  113. return false
  114. end
  115. return lvim.builtin.cmp.active
  116. end,
  117. confirm_opts = {
  118. behavior = ConfirmBehavior.Replace,
  119. select = false,
  120. },
  121. completion = {
  122. ---@usage The minimum length of a word to complete on.
  123. keyword_length = 1,
  124. },
  125. experimental = {
  126. ghost_text = false,
  127. native_menu = false,
  128. },
  129. formatting = {
  130. fields = { "kind", "abbr", "menu" },
  131. max_width = 0,
  132. kind_icons = lvim.icons.kind,
  133. source_names = {
  134. nvim_lsp = "(LSP)",
  135. emoji = "(Emoji)",
  136. path = "(Path)",
  137. calc = "(Calc)",
  138. cmp_tabnine = "(Tabnine)",
  139. vsnip = "(Snippet)",
  140. luasnip = "(Snippet)",
  141. buffer = "(Buffer)",
  142. tmux = "(TMUX)",
  143. copilot = "(Copilot)",
  144. codeium = "(Codeium)",
  145. treesitter = "(TreeSitter)",
  146. },
  147. duplicates = {
  148. buffer = 1,
  149. path = 1,
  150. nvim_lsp = 0,
  151. luasnip = 1,
  152. },
  153. duplicates_default = 0,
  154. format = function(entry, vim_item)
  155. local max_width = lvim.builtin.cmp.formatting.max_width
  156. if max_width ~= 0 and #vim_item.abbr > max_width then
  157. vim_item.abbr = string.sub(vim_item.abbr, 1, max_width - 1) .. lvim.icons.ui.Ellipsis
  158. end
  159. if lvim.use_icons then
  160. vim_item.kind = lvim.builtin.cmp.formatting.kind_icons[vim_item.kind]
  161. if entry.source.name == "copilot" then
  162. vim_item.kind = lvim.icons.git.Octoface
  163. vim_item.kind_hl_group = "CmpItemKindCopilot"
  164. end
  165. if entry.source.name == "codeium" then
  166. vim_item.kind = lvim.icons.misc.Watch
  167. vim_item.kind_hl_group = "CmpItemKindCopilot"
  168. end
  169. if entry.source.name == "cmp_tabnine" then
  170. vim_item.kind = lvim.icons.misc.Robot
  171. vim_item.kind_hl_group = "CmpItemKindTabnine"
  172. end
  173. if entry.source.name == "crates" then
  174. vim_item.kind = lvim.icons.misc.Package
  175. vim_item.kind_hl_group = "CmpItemKindCrate"
  176. end
  177. if entry.source.name == "lab.quick_data" then
  178. vim_item.kind = lvim.icons.misc.CircuitBoard
  179. vim_item.kind_hl_group = "CmpItemKindConstant"
  180. end
  181. if entry.source.name == "emoji" then
  182. vim_item.kind = lvim.icons.misc.Smiley
  183. vim_item.kind_hl_group = "CmpItemKindEmoji"
  184. end
  185. end
  186. vim_item.menu = lvim.builtin.cmp.formatting.source_names[entry.source.name]
  187. vim_item.dup = lvim.builtin.cmp.formatting.duplicates[entry.source.name]
  188. or lvim.builtin.cmp.formatting.duplicates_default
  189. return vim_item
  190. end,
  191. },
  192. snippet = {
  193. expand = function(args)
  194. luasnip.lsp_expand(args.body)
  195. end,
  196. },
  197. window = {
  198. completion = cmp_window.bordered(),
  199. documentation = cmp_window.bordered(),
  200. },
  201. sources = {
  202. {
  203. name = "copilot",
  204. -- keyword_length = 0,
  205. max_item_count = 3,
  206. trigger_characters = {
  207. {
  208. ".",
  209. ":",
  210. "(",
  211. "'",
  212. '"',
  213. "[",
  214. ",",
  215. "#",
  216. "*",
  217. "@",
  218. "|",
  219. "=",
  220. "-",
  221. "{",
  222. "/",
  223. "\\",
  224. "+",
  225. "?",
  226. " ",
  227. -- "\t",
  228. -- "\n",
  229. },
  230. },
  231. },
  232. {
  233. name = "nvim_lsp",
  234. entry_filter = function(entry, ctx)
  235. local kind = require("cmp.types.lsp").CompletionItemKind[entry:get_kind()]
  236. if kind == "Snippet" and ctx.prev_context.filetype == "java" then
  237. return false
  238. end
  239. return true
  240. end,
  241. },
  242. { name = "codeium" }, -- for codeium completion
  243. { name = "path" },
  244. { name = "luasnip" },
  245. { name = "cmp_tabnine" },
  246. { name = "nvim_lua" },
  247. { name = "buffer" },
  248. { name = "calc" },
  249. { name = "emoji" },
  250. { name = "treesitter" },
  251. { name = "crates" },
  252. { name = "tmux" },
  253. },
  254. mapping = cmp_mapping.preset.insert {
  255. ["<C-k>"] = cmp_mapping(cmp_mapping.select_prev_item(), { "i", "c" }),
  256. ["<C-j>"] = cmp_mapping(cmp_mapping.select_next_item(), { "i", "c" }),
  257. ["<Down>"] = cmp_mapping(cmp_mapping.select_next_item { behavior = SelectBehavior.Select }, { "i" }),
  258. ["<Up>"] = cmp_mapping(cmp_mapping.select_prev_item { behavior = SelectBehavior.Select }, { "i" }),
  259. ["<C-d>"] = cmp_mapping.scroll_docs(-4),
  260. ["<C-f>"] = cmp_mapping.scroll_docs(4),
  261. ["<C-y>"] = cmp_mapping {
  262. i = cmp_mapping.confirm { behavior = ConfirmBehavior.Replace, select = false },
  263. c = function(fallback)
  264. if cmp.visible() then
  265. cmp.confirm { behavior = ConfirmBehavior.Replace, select = false }
  266. else
  267. fallback()
  268. end
  269. end,
  270. },
  271. ["<Tab>"] = cmp_mapping(function(fallback)
  272. if cmp.visible() then
  273. cmp.select_next_item()
  274. elseif luasnip.expand_or_locally_jumpable() then
  275. luasnip.expand_or_jump()
  276. elseif jumpable(1) then
  277. luasnip.jump(1)
  278. elseif has_words_before() then
  279. -- cmp.complete()
  280. fallback()
  281. else
  282. fallback()
  283. end
  284. end, { "i", "s" }),
  285. ["<S-Tab>"] = cmp_mapping(function(fallback)
  286. if cmp.visible() then
  287. cmp.select_prev_item()
  288. elseif luasnip.jumpable(-1) then
  289. luasnip.jump(-1)
  290. else
  291. fallback()
  292. end
  293. end, { "i", "s" }),
  294. ["<C-Space>"] = cmp_mapping.complete(),
  295. ["<C-e>"] = cmp_mapping.abort(),
  296. ["<CR>"] = cmp_mapping(function(fallback)
  297. if cmp.visible() then
  298. local confirm_opts = vim.deepcopy(lvim.builtin.cmp.confirm_opts) -- avoid mutating the original opts below
  299. local is_insert_mode = function()
  300. return vim.api.nvim_get_mode().mode:sub(1, 1) == "i"
  301. end
  302. if is_insert_mode() then -- prevent overwriting brackets
  303. confirm_opts.behavior = ConfirmBehavior.Insert
  304. end
  305. local entry = cmp.get_selected_entry()
  306. local is_copilot = entry and entry.source.name == "copilot"
  307. if is_copilot then
  308. confirm_opts.behavior = ConfirmBehavior.Replace
  309. confirm_opts.select = true
  310. end
  311. if cmp.confirm(confirm_opts) then
  312. return -- success, exit early
  313. end
  314. end
  315. fallback() -- if not exited early, always fallback
  316. end),
  317. },
  318. cmdline = {
  319. enable = false,
  320. options = {
  321. {
  322. type = ":",
  323. sources = {
  324. { name = "path" },
  325. { name = "cmdline" },
  326. },
  327. },
  328. {
  329. type = { "/", "?" },
  330. sources = {
  331. { name = "buffer" },
  332. },
  333. },
  334. },
  335. },
  336. }
  337. end
  338. function M.setup()
  339. local cmp = require "cmp"
  340. cmp.setup(lvim.builtin.cmp)
  341. if lvim.builtin.cmp.cmdline.enable then
  342. for _, option in ipairs(lvim.builtin.cmp.cmdline.options) do
  343. cmp.setup.cmdline(option.type, {
  344. mapping = cmp.mapping.preset.cmdline(),
  345. sources = option.sources,
  346. })
  347. end
  348. end
  349. if lvim.builtin.cmp.on_config_done then
  350. lvim.builtin.cmp.on_config_done(cmp)
  351. end
  352. end
  353. return M