diff --git a/snippets/c.lua b/snippets/c.lua index 1a8ffe1..7c75c8a 100644 --- a/snippets/c.lua +++ b/snippets/c.lua @@ -7,6 +7,11 @@ local insert = luasnip.insert_node local func = luasnip.function_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 = { snip('#include', { text('#include '), @@ -20,6 +25,21 @@ local snippets = { end end, key('open')), }), + + snip('once', { + text('#ifndef '), + func(function(args) + return args[1][1] + end, key('name')), + text({ '', '#define ' }), + insert(1, getIncludeGuardName(), { key = 'name' }), + text({ '', '', '', }), + insert(0, ''), + text({ '', '', '#endif // ' }), + func(function(args) + return args[1][1] + end, key('name')), + }), } return snippets