From 28142707626e05f46a2bc151a95c9e511afbc358 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Wed, 4 Dec 2024 14:46:59 +0000 Subject: [PATCH] Add C/C++ include guard snippet 'once' --- snippets/c.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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