Make cpp for snippet dynamic
This commit is contained in:
@@ -1,5 +1,48 @@
|
||||
# C++ LuaSnip Snippets
|
||||
-- C++ LuaSnip Snippets
|
||||
|
||||
local luasnip = require('luasnip')
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user