1
0
Fork 0
mirror of https://github.com/pnx/dotfiles synced 2026-06-16 03:14:55 +02:00

nvim add c/cpp snippets

This commit is contained in:
Henrik Hautakoski 2025-12-09 01:50:22 +01:00
parent a011d86b24
commit ce00d0e5c9
2 changed files with 46 additions and 0 deletions

23
nvim/snippets/c.lua Normal file
View file

@ -0,0 +1,23 @@
local ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local f = ls.function_node
local i = ls.insert_node
-- Helper: convert current filename to uppercase header guard
local function header_guard(_, snip)
local name = snip.env.TM_FILENAME or "HEADER"
-- Replace dots and non-alphanumerics with underscores
name = name:gsub("%.", "_"):gsub("%W", "_"):upper()
return name .. "_"
end
return {
s("guard", {
t("#ifndef "), f(header_guard, {}),
t({ "", "#define " }), f(header_guard, {}),
t({ "", "", "" }),
i(0),
t({ "", "", "#endif // " }), f(header_guard, {}),
}),
}

23
nvim/snippets/cpp.lua Normal file
View file

@ -0,0 +1,23 @@
local ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local f = ls.function_node
local i = ls.insert_node
-- Helper: convert current filename to uppercase header guard
local function header_guard(_, snip)
local name = snip.env.TM_FILENAME or "HEADER"
-- Replace dots and non-alphanumerics with underscores
name = name:gsub("%.", "_"):gsub("%W", "_"):upper()
return name .. "_"
end
return {
s("guard", {
t("#ifndef "), f(header_guard, {}),
t({ "", "#define " }), f(header_guard, {}),
t({ "", "", "" }),
i(0),
t({ "", "", "#endif // " }), f(header_guard, {}),
}),
}