Make cpp for snippet dynamic

This commit is contained in:
Kenneth Benzie 2024-06-01 10:30:52 +01:00
parent 0410dfc23d
commit 59d3ea0978
5 changed files with 58 additions and 15 deletions

View File

@ -138,8 +138,8 @@ return {
}), }),
sources = { sources = {
{ name = 'nvim_lsp' },
{ name = 'luasnip' }, { name = 'luasnip' },
{ name = 'nvim_lsp' },
{ name = 'buffer' }, { name = 'buffer' },
{ name = 'path' }, { name = 'path' },
{ name = 'git' }, { name = 'git' },

View File

@ -22,5 +22,15 @@ return {
luasnip.jump(-1) luasnip.jump(-1)
end end
end, { silent = true }) end, { silent = true })
-- vim.keymap.set('s', '<M-l>', function()
-- if luasnip.choice_active() then
-- luasnip.change_choice(1)
-- end
-- end, { silent = true })
-- vim.keymap.set('s', '<M-h>', function()
-- if luasnip.choice_active() then
-- luasnip.change_choice(-1)
-- end
-- end, { silent = true })
end end
} }

View File

@ -6,7 +6,7 @@ local snip = luasnip.snippet
local text = luasnip.text_node local text = luasnip.text_node
-- local isn = luasnip.indent_snippet_node -- local isn = luasnip.indent_snippet_node
-- local t = luasnip.text_node -- local t = luasnip.text_node
local insert = luasnip.insert_node local ins = luasnip.insert_node
local func = luasnip.function_node local func = luasnip.function_node
-- local c = luasnip.choice_node -- local c = luasnip.choice_node
-- local d = luasnip.dynamic_node -- local d = luasnip.dynamic_node
@ -67,7 +67,7 @@ for _, name in ipairs({ 'fixme', 'todo', 'hack', 'warn', 'note' }) do
snip(name, { snip(name, {
func(comment_prefix), func(comment_prefix),
text(string.upper(name) .. ': '), text(string.upper(name) .. ': '),
insert(0), ins(0),
func(comment_suffix), func(comment_suffix),
}) })
) )

View File

@ -1,5 +1,48 @@
# C++ LuaSnip Snippets -- C++ LuaSnip Snippets
local luasnip = require('luasnip') local luasnip = require('luasnip')
luasnip.filetype_extend('cpp', { 'c' }) luasnip.filetype_extend('cpp', { 'c' })
local snip = luasnip.snippet
local text = luasnip.text_node
local ins = luasnip.insert_node
local dyn = luasnip.dynamic_node
local node = luasnip.snippet_node
local key = require("luasnip.nodes.key_indexer").new_key
local snippets = {
snip('for', {
text('for ('),
ins(1, 'auto'),
text(' '),
ins(2, 'index'),
text(' '),
ins(3, '='),
dyn(4, function(args)
local choice = args[2][1]
if choice == '=' then -- index based for loop
local var = args[1][1]
return node(nil, {
text(' '),
ins(1, '0'),
text('; ' .. var .. ' < '),
ins(2, 'count'),
text('; '),
ins(3, var .. '++'),
})
elseif choice == ':' then -- range based for loop
return node(nil, {
text(' '),
ins(1, 'container')
})
end
return node(nil, {})
end, { 2, 3 }, key('var')),
text({ ') {', '\t' }),
ins(0, ''),
text({ '', '}' })
}),
}
return snippets

View File

@ -2,16 +2,6 @@
extends c extends c
snippet for Range based
for (${1:auto} ${2:value} : ${3:container}) {
$0
}
snippet for Index based
for (${1:int} i = ${2:0}; i < ${3:count}; i++) {
$0
}
snippet namespace snippet namespace
namespace ${1} { namespace ${1} {
$0 $0