diff --git a/lua/core/plugin_config/completions.lua b/lua/core/plugin_config/completions.lua index f257a56..610130d 100644 --- a/lua/core/plugin_config/completions.lua +++ b/lua/core/plugin_config/completions.lua @@ -1,4 +1,11 @@ +local has_words_before = function() + unpack = unpack or table.unpack + 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 cmp = require("cmp") +local luasnip = require("luasnip") cmp.setup({ mapping = cmp.mapping.preset.insert({ @@ -7,11 +14,38 @@ cmp.setup({ [''] = cmp.mapping.complete(), [''] = cmp.mapping.abort(), [''] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- they way you will only jump inside the snippet region + elseif luasnip.expand_or_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" }), }), snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, }, sources = cmp.config.sources({ { name = 'nvim_lsp' }, + { name = 'luasnip' }, }, { { name = 'buffer' }, }), diff --git a/lua/core/plugins.lua b/lua/core/plugins.lua index cf18132..bf6400b 100644 --- a/lua/core/plugins.lua +++ b/lua/core/plugins.lua @@ -37,6 +37,12 @@ local plugins = { -- completion 'hrsh7th/nvim-cmp', 'hrsh7th/cmp-nvim-lsp', + { + "L3MON4D3/LuaSnip", + version = "2.*", -- Replace by the latest released major + -- install jsregexp (optional!). + build = "make install_jsregexp" + } } local opts = {}