dotfiles/.config/nvim/snippets/cpp.lua

40 lines
1.2 KiB
Lua

-- <https://github.com/L3MON4D3/LuaSnip/blob/69cb81cf7490666890545fef905d31a414edc15b/lua/luasnip/config.lua#L82-L104>
local s = require("luasnip.nodes.snippet").S
local f = require("luasnip.nodes.functionNode").F
local i = require("luasnip.nodes.insertNode").I
local fmt = require("luasnip.extras.fmt").fmt
local t = require("luasnip.nodes.textNode").T
local my = require('my.functions')
return {
s({ trig = 'main',
name = 'main',
dscr = 'main function'
}, {
t({ 'int main(int argc, char *argv[]) {' }),
i(0),
t({ '', '}' })
}),
s({ trig = 'includeguard',
name = 'includeguard',
dscr = 'add include guard'
},
fmt([[#ifndef {project}_{basename}
#define {project}_{basename}
{cursor}
#endif // {project}_{basename}]], {
project = f(function()
local root = my.get_project_root()
if root then return root:gsub('.*/', ''):upper() end
return 'INSERT_PROJECT'
end),
basename = f(function()
return vim.fs.basename(vim.api.nvim_buf_get_name(0)):upper()
:gsub('%.', '_')
end),
cursor = i(0)
}))
}