# HG changeset patch # User Meredith Howard # Date 1680675964 18000 # Node ID 7df98d33285d9e56d3dfaaebfb52da6a619c400e # Parent 8bb51fcd6f2aa37188c8736c0da777dafdc9a3c4 actually set up lsp snippets diff --git a/.config/nvim/lua/plugins/completion.lua b/.config/nvim/lua/plugins/completion.lua --- a/.config/nvim/lua/plugins/completion.lua +++ b/.config/nvim/lua/plugins/completion.lua @@ -10,15 +10,28 @@ return { "hrsh7th/cmp-nvim-lua", "hrsh7th/cmp-omni", "hrsh7th/cmp-path", + { "L3MON4D3/LuaSnip", opts = { history = true, delete_check_events = "TextChanged" } }, "quangnguyen30192/cmp-nvim-tags", }, config = function() local cmp = require("cmp") + local luasnip = require("luasnip") + + 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 + cmp.setup({ preselect = cmp.PreselectMode.None, completion = { keyword_length = 3, }, + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, formatting = { format = function(entry, vim_item) if entry.source.name == "nvim_lsp_signature_help" then @@ -32,15 +45,36 @@ return { mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.select_next_item(), - [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_locally_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), [""] = cmp.mapping.complete(), [""] = function(fallback) - if cmp.visible() then + local e = cmp.get_active_entry() + if cmp.visible() and e then cmp.confirm({ select = false }, function() - vim.api.nvim_feedkeys(" ", "n", false) + if e:get_kind() ~= cmp.lsp.CompletionItemKind.Snippet then + vim.api.nvim_feedkeys(" ", "n", false) + end end) end fallback()