nvim/snippets/c.lua

51 lines
1.2 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 dynamic = luasnip.dynamic_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(opts)
return opts[1][1]
end, key('guard')),
text({ '', '#define ' }),
dynamic(1, function()
return snip(nil, {
insert(1, getIncludeGuardName(), { key = 'guard' }),
})
end),
text({ '', '', '', }),
insert(0, ''),
text({ '', '', '#endif // ' }),
func(function(opts)
return opts[1][1]
end, key('guard')),
}, {}),
}
return snippets