summaryrefslogtreecommitdiff
path: root/lua/config/cmp.lua
diff options
context:
space:
mode:
authorYour Name <you@example.com>2024-07-03 17:03:56 +0100
committerYour Name <you@example.com>2024-07-03 17:03:56 +0100
commitc959b2112fb4c82b5bfd410df21706455225bd40 (patch)
tree6774868448d127c2f560827de8e5edbd868a2832 /lua/config/cmp.lua
minor additionsHEADmaster
Diffstat (limited to 'lua/config/cmp.lua')
-rw-r--r--lua/config/cmp.lua222
1 files changed, 222 insertions, 0 deletions
diff --git a/lua/config/cmp.lua b/lua/config/cmp.lua
new file mode 100644
index 0000000..68b7883
--- /dev/null
+++ b/lua/config/cmp.lua
@@ -0,0 +1,222 @@
+local has_words_before = function()
+ local line, col = unpack(vim.api.nvim_win_get_cursor(0))
+ return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
+end
+
+local function jumpable(dir)
+ local luasnip_ok, luasnip = pcall(require, "luasnip")
+ if not luasnip_ok then
+ return false
+ end
+
+ local win_get_cursor = vim.api.nvim_win_get_cursor
+ local get_current_buf = vim.api.nvim_get_current_buf
+
+ ---sets the current buffer's luasnip to the one nearest the cursor
+ ---@return boolean true if a node is found, false otherwise
+ local function seek_luasnip_cursor_node()
+ -- TODO(kylo252): upstream this
+ -- for outdated versions of luasnip
+ if not luasnip.session.current_nodes then
+ return false
+ end
+
+ local node = luasnip.session.current_nodes[get_current_buf()]
+ if not node then
+ return false
+ end
+
+ local snippet = node.parent.snippet
+ local exit_node = snippet.insert_nodes[0]
+
+ local pos = win_get_cursor(0)
+ pos[1] = pos[1] - 1
+
+ -- exit early if we're past the exit node
+ if exit_node then
+ local exit_pos_end = exit_node.mark:pos_end()
+ if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+ end
+
+ node = snippet.inner_first:jump_into(1, true)
+ while node ~= nil and node.next ~= nil and node ~= snippet do
+ local n_next = node.next
+ local next_pos = n_next and n_next.mark:pos_begin()
+ local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
+ or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
+
+ -- Past unmarked exit node, exit early
+ if n_next == nil or n_next == snippet.next then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+
+ if candidate then
+ luasnip.session.current_nodes[get_current_buf()] = node
+ return true
+ end
+
+ local ok
+ ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
+ if not ok then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+ end
+
+ -- No candidate, but have an exit node
+ if exit_node then
+ -- to jump to the exit node, seek to snippet
+ luasnip.session.current_nodes[get_current_buf()] = snippet
+ return true
+ end
+
+ -- No exit node, exit from snippet
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+ return false
+ end
+
+ if dir == -1 then
+ return luasnip.in_snippet() and luasnip.jumpable(-1)
+ else
+ return luasnip.in_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable(1)
+ end
+end
+
+local t = function(str)
+ return vim.api.nvim_replace_termcodes(str, true, true, true)
+end
+
+
+local status_cmp_ok, cmp = pcall(require, "cmp")
+if not status_cmp_ok then
+ return
+end
+local status_luasnip_ok, luasnip = pcall(require, "luasnip")
+if not status_luasnip_ok then
+ return
+end
+
+local setup = {
+ confirm_opts = lvim.builtin.cmp.confirm_opts,
+ completion = {
+ keyword_length = 1,
+ },
+ experimental = {
+ ghost_text = false,
+ native_menu = false,
+ },
+ formatting = {
+ fields = { "kind", "abbr", "menu" },
+ max_width = 0,
+ kind_icons = lvim.icons.kind,
+ source_names = {
+ nvim_lsp = "(LSP)",
+ luasnip = "(Snippet)",
+ latex_symbols = "(LaTeX)",
+ emoji = "(Emoji)",
+ path = "(Path)",
+ calc = "(Calc)",
+ cmp_tabnine = "(Tabnine)",
+ vsnip = "(Snippet)",
+ ultisnips = "(Snippet)",
+ buffer = "(Buffer)",
+ tmux = "(TMUX)",
+ },
+ duplicates = lvim.builtin.cmp.duplicates,
+ duplicates_default = 0,
+ format = lvim.builtin.cmp.format,
+ },
+ snippet = {
+ expand = function(args)
+ vim.fn["UltiSnips#Anon"](args.body)
+ end,
+ },
+ window = lvim.builtin.cmp.window,
+ sources = {
+ { name = "nvim_lsp" },
+ { name = "path" },
+ { name = "luasnip" },
+ { name = "cmp_tabnine" },
+ { name = "nvim_lua" },
+ { name = "buffer" },
+ { name = "calc" },
+ { name = "emoji" },
+ { name = "treesitter" },
+ { name = "ultisnips" },
+ { name = "latex_symbols" },
+ { name = "crates" },
+ { name = "tmux" },
+ },
+ mapping = cmp.mapping.preset.insert {
+ ["<C-k>"] = cmp.mapping.select_prev_item(),
+ ["<C-j>"] = cmp.mapping.select_next_item(),
+ ["<Down>"] = cmp.mapping(cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }, { "i" }),
+ ["<Up>"] = cmp.mapping(cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }, { "i" }),
+ ["<C-d>"] = cmp.mapping.scroll_docs(-4),
+ ["<C-f>"] = cmp.mapping.scroll_docs(4),
+ ["<C-y>"] = cmp.mapping {
+ i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
+ c = function(fallback)
+ if cmp.visible() then
+ cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
+ else
+ fallback()
+ end
+ end,
+ },
+ ["<Tab>"] = cmp.mapping(function(fallback)
+ if cmp.visible() then
+ cmp.select_next_item()
+ elseif vim.fn["UltiSnips#CanJumpForwards"]() == 1 then
+ vim.api.nvim_feedkeys(t("<Plug>(ultisnips_jump_forward)"), "m", true)
+ elseif has_words_before() then
+ fallback()
+ else
+ fallback()
+ end
+ end, { "i", "s" }),
+ ["<S-Tab>"] = cmp.mapping(function(fallback)
+ if cmp.visible() then
+ cmp.select_prev_item()
+ elseif vim.fn["UltiSnips#CanJumpBackwards"]() == 1 then
+ vim.api.nvim_feedkeys(t("<Plug>(ultisnips_jump_backward)"), "m", true)
+ else
+ fallback()
+ end
+ end, { "i", "s" }),
+ ["<C-Space>"] = cmp.mapping.complete(),
+ ["<C-e>"] = cmp.mapping.abort(),
+ ["<CR>"] = cmp.mapping(function(fallback)
+ if cmp.visible() then
+ local confirm_opts = vim.deepcopy(lvim.builtin.cmp.confirm_opts) -- avoid mutating the original opts below
+ local is_insert_mode = function()
+ return vim.api.nvim_get_mode().mode:sub(1, 1) == "i"
+ end
+ if is_insert_mode() then -- prevent overwriting brackets
+ confirm_opts.behavior = cmp.ConfirmBehavior.Insert
+ end
+ if cmp.confirm(confirm_opts) then
+ return -- success, exit early
+ end
+ end
+
+ if jumpable(1) and luasnip.jump(1) then
+ return -- success, exit early
+ end
+ fallback() -- if not exited early, always fallback
+ end),
+ },
+}
+
+require("cmp").setup(setup)