46 lines
1.0 KiB
Lua
46 lines
1.0 KiB
Lua
# C LuaSnip Snippets
|
|
|
|
local luasnip = require('luasnip')
|
|
local snip = luasnip.snippet
|
|
local text = luasnip.text_node
|
|
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 '),
|
|
insert(1, '<', { key = 'open' }),
|
|
insert(2, 'header'),
|
|
func(function(args)
|
|
if args[1][1] == '<' then
|
|
return '>'
|
|
else
|
|
return '"'
|
|
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
|