local ls = require('luasnip') local types = require('luasnip.util.types') vim.api.nvim_set_hl(0, 'LuasnipIndicator', { fg = vim.g.terminal_color_15, bg = vim.g.terminal_color_1, italic = true, nocombine = true, }) -- autowrap: wraps the function in top and bot if there is any content local autowrap = function(top, bot, inner) local autoinsert = function(args, _, _, wrap_with) local nodes = {} if vim.trim(table.concat(args[1] or {})) ~= "" then table.insert(nodes, wrap_with) end return ls.sn(nil, nodes) end local argnodes = {} local maxpos = -1 for _, e in ipairs(inner) do if e.pos ~= nil then table.insert(argnodes, e.pos) if e.pos > maxpos then maxpos = e.pos end end end local nodes = {} table.insert(nodes, ls.d(maxpos+1, autoinsert, argnodes, {user_args = {top}})) for _, e in ipairs(inner) do table.insert(nodes, e) end table.insert(nodes, ls.d(maxpos+2, autoinsert, argnodes, {user_args = {bot}})) return nodes end -- comment: wraps the content in &commentstring (or a blockcomment) local comment = function(wrapped, blockcomment) -- commentstring helper local get_cstring = function() local cs = require('Comment.ft').calculate { ctype = blockcomment and 2 or 1, range = require('Comment.utils').get_region() } local tbl = vim.split(cs or '', '%s', {plain = true, trimempty = true}) return (#tbl == 0) and {'', ''} or ((#tbl == 1) and {tbl[1] .. ' ', ''} or {tbl[1], tbl[2]}) end if type(wrapped) ~= "table" then wrapped = {wrapped} end local nodes = {} table.insert(nodes, ls.f(function() return get_cstring()[1] end)) for _, n in ipairs(wrapped) do table.insert(nodes, n) end table.insert(nodes, ls.f(function() return get_cstring()[2] end)) return nodes end -- exec: inserts the output of command local exec = function(command) return ls.f(function(_, _, ...) local results, code = require('plenary.job'):new({ command = vim.env.SHELL, args = {'-c', ...}, enable_recording = true, }):sync() if not code == 0 or #results == 0 then error(string.format('exec "%s" failed', ...)) end return results end, {}, {user_args = {command}}) end ls.setup { ext_opts = { [types.snippet] = { active = { virt_text = {{ '<-- luasnip', 'LuasnipIndicator' }}, virt_text_pos = 'right_align' } }, [types.insertNode] = { unvisited = { hl_group = 'LuasnipIndicator' } }, [types.choiceNode] = { active = { virt_text = {{'<-- choice node', 'LuasnipIndicator'}}, } } }, snip_env = vim.tbl_extend('keep', ls.session.config.snip_env, { autowrap = autowrap, exec = exec, comment = comment, user_email = { exec([[git config --get user.name]]), ls.t(' <'), ls.c(1, { exec([[git config --get user.email]]), ls.t([[robert.gunzler@postman.com]]), ls.t([[robert@gnzler.io]]), }), ls.t('>'), }, }) } -- snipmate snippets require('luasnip.loaders.from_snipmate').lazy_load {paths = './after/snippets'} -- lua snippets require("luasnip.loaders.from_lua").lazy_load {paths = './snippets'} vim.api.nvim_create_user_command('LuaSnipUnlinkAll', function() while #package.loaded.luasnip.session.current_nodes > 0 do package.loaded.luasnip.unlink_current() end end, { desc = 'unlink all active snippets' })