nvim/snippets/c.lua

51 lines
1.3 KiB
Lua

-- C LuaSnip Snippets
local luasnip = require('luasnip')
local snippet = luasnip.snippet
local text = luasnip.text_node
local insert = luasnip.insert_node
local func = luasnip.function_node
local choice = luasnip.choice_node
local restore = luasnip.restore_node
local snip = luasnip.snippet_node
local key = require("luasnip.nodes.key_indexer").new_key
local function getIncludeGuardName()
local filename = vim.fn.expand('%'):gsub('[^%w_]', '_'):upper()
return filename .. '_INCLUDED'
end
local snippets = {
snippet('#include', {
text('#include '),
choice(1, {
snip(nil, { text('<'), restore(1, 'header'), text('>') }),
snip(nil, { text('"'), restore(1, 'header'), text('"') }),
}),
}, { stored = { ['header'] = insert(1, 'header') } }),
snippet('once', {
text('#ifndef '),
func(function(args)
return args[1][1]
end, key('name')),
text({ '', '#define ' }),
insert(1, getIncludeGuardName(), { key = 'name' }),
-- FIXME: dynamic(1, function(_, _)
-- return snip(nil, {
-- insert(1, getIncludeGuardName())
-- })
-- end, { key = "name" }),
text({ '', '', '', }),
insert(0, ''),
text({ '', '', '#endif // ' }),
func(function(args)
return args[1][1]
end, key('name')),
}),
}
return snippets