add snippets for C
This commit is contained in:
parent
cc00820c8c
commit
999b7f3dba
3 changed files with 397 additions and 8 deletions
|
@ -50,7 +50,17 @@ cmp.setup({
|
|||
mapping = {
|
||||
["<C-j>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
|
||||
["<C-k>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
|
||||
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
|
||||
["<CR>"] = cmp.mapping({
|
||||
i = function(fallback)
|
||||
if cmp.visible() and cmp.get_active_entry() then
|
||||
cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end,
|
||||
s = cmp.mapping.confirm({ select = true }),
|
||||
c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }),
|
||||
}),
|
||||
["<C-e>"] = cmp.mapping({
|
||||
i = cmp.mapping.abort(),
|
||||
c = cmp.mapping.close(),
|
||||
|
@ -58,8 +68,6 @@ cmp.setup({
|
|||
["<Tab>"] = cmp.mapping(function(fallback)
|
||||
if cmp.visible() then
|
||||
cmp.select_next_item()
|
||||
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
|
||||
-- they way you will only jump inside the snippet region
|
||||
elseif luasnip.expand_or_jumpable() then
|
||||
luasnip.expand_or_jump()
|
||||
elseif has_words_before() then
|
||||
|
@ -68,7 +76,6 @@ cmp.setup({
|
|||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
|
||||
["<S-Tab>"] = cmp.mapping(function(fallback)
|
||||
if cmp.visible() then
|
||||
cmp.select_prev_item()
|
||||
|
|
|
@ -1,18 +1,42 @@
|
|||
local status_ok, luasnip = pcall(require, "LuaSnip")
|
||||
local status_ok, luasnip = pcall(require, "luasnip")
|
||||
if not status_ok then
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
luasnip.setup({
|
||||
history = true,
|
||||
snip_env = {
|
||||
s = function(...)
|
||||
local snip = luasnip.s(...)
|
||||
table.insert(getfenv(2).ls_file_snippets, snip)
|
||||
end,
|
||||
ref = function(args, _)
|
||||
return args[1][1]
|
||||
end,
|
||||
parse = function(...)
|
||||
local snip = luasnip.parser.parse_snippet(...)
|
||||
table.insert(getfenv(2).ls_file_snippets, snip)
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
local _, lloader = pcall(require, "luasnip.loaders.from_lua")
|
||||
if status_ok then
|
||||
lloader.load({ paths = os.getenv("XDG_CONFIG_HOME") .. "/nvim/lua/snippets" })
|
||||
end
|
||||
|
||||
local _, vloader = pcall(require, "luasnip.loaders.from_vscode")
|
||||
if status_ok then
|
||||
vloader.lazy_load({ paths = os.getenv("XDG_CONFIG_HOME") .. "/nvim/lua/snippets" })
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd("InsertLeave", {
|
||||
callback = function()
|
||||
if require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()]
|
||||
and not require("luasnip").session.jump_active
|
||||
if luasnip.session.current_nodes[vim.api.nvim_get_current_buf()]
|
||||
and not luasnip.session.jump_active
|
||||
then
|
||||
require("luasnip").unlink_current()
|
||||
luasnip.unlink_current()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
|
358
.config/nvim/lua/snippets/c.lua
Normal file
358
.config/nvim/lua/snippets/c.lua
Normal file
|
@ -0,0 +1,358 @@
|
|||
-- NOTE: Uncomment to fix LSP warnings
|
||||
|
||||
local ls = require("luasnip")
|
||||
local s = ls.snippet
|
||||
local ref = function(args, _)
|
||||
return args[1][1]
|
||||
end
|
||||
local t = ls.text_node
|
||||
local i = ls.insert_node
|
||||
local f = ls.function_node
|
||||
--
|
||||
-- local sn = ls.snippet_node
|
||||
-- local c = ls.choice_node
|
||||
-- local d = ls.dynamic_node
|
||||
-- local l = require("luasnip.extras").lambda
|
||||
-- local r = require("luasnip.extras").rep
|
||||
-- local p = require("luasnip.extras").partial
|
||||
-- local m = require("luasnip.extras").match
|
||||
-- local n = require("luasnip.extras").nonempty
|
||||
-- local dl = require("luasnip.extras").dynamic_lambda
|
||||
-- local fmt = require("luasnip.extras.fmt").fmt
|
||||
-- local fmta = require("luasnip.extras.fmt").fmta
|
||||
-- local types = require("luasnip.util.types")
|
||||
-- local conds = require("luasnip.extras.expand_conditions")
|
||||
-- local isn = ls.indent_snippet_node
|
||||
-- local events = require("luasnip.util.events")
|
||||
|
||||
return {
|
||||
s({ trig = "sst", name = "Standard Template" }, {
|
||||
t({ "#include <stdlib.h>",
|
||||
"#include <stdio.h>",
|
||||
"",
|
||||
"int main(int argc, char *argv[]) {",
|
||||
"\t"
|
||||
}),
|
||||
i(0),
|
||||
t({ "",
|
||||
"\treturn EXIT_SUCCESS;",
|
||||
"}"
|
||||
}),
|
||||
}),
|
||||
s({ trig = "sstp", name = "Preprocessor Template" }, {
|
||||
t({ "// #define NDEBUG // disables assert()",
|
||||
"#include <stdlib.h>",
|
||||
"#include <stddef.h>",
|
||||
"#include <stdbool.h>",
|
||||
"#include <assert.h>",
|
||||
"#include <errno.h>",
|
||||
"#include <stdio.h>",
|
||||
""
|
||||
}),
|
||||
}),
|
||||
s({ trig = "main", name = "main(int argc, char *argv[])" }, {
|
||||
t({
|
||||
"int main(int argc, char *argv[]) {",
|
||||
"\t",
|
||||
}),
|
||||
i(0),
|
||||
t({
|
||||
"",
|
||||
"\treturn EXIT_SUCCESS;",
|
||||
"}"
|
||||
}),
|
||||
}),
|
||||
s({ trig = "mainn", name = "main(void)" }, {
|
||||
t({
|
||||
"int main(void) {",
|
||||
"\t",
|
||||
}),
|
||||
i(0),
|
||||
t({
|
||||
"", "\treturn EXIT_SUCCESS;",
|
||||
"}"
|
||||
}),
|
||||
}),
|
||||
s({ trig = "#inc", name = "#include <...>" }, {
|
||||
t "#include <", i(1), t ">",
|
||||
}),
|
||||
s({ trig = "#Inc", name = "#include \"...\"" }, {
|
||||
t "#include \"", i(1), t "\"",
|
||||
}),
|
||||
s({ trig = "#def", name = "#define macro" }, {
|
||||
t "#define ", i(1),
|
||||
}),
|
||||
s({ trig = "#deff", name = "#define macro()" }, {
|
||||
t "#define ", i(1), t "(", i(2), t ") (", i(3), t ")",
|
||||
}),
|
||||
s({ trig = "#if", name = "#if" }, {
|
||||
t "#if ", i(1), t({ "", "" }),
|
||||
i(0),
|
||||
t({ "", "#endif /* if " }), f(ref, 1), t " */",
|
||||
}),
|
||||
s({ trig = "#ifdef", name = "#ifdef" }, {
|
||||
t "#ifdef ", i(1), t({ "", "" }),
|
||||
i(0),
|
||||
t({ "", "#endif /* ifdef " }), f(ref, 1), t " */",
|
||||
}),
|
||||
s({ trig = "#ifndef", name = "#ifndef" }, {
|
||||
t "#ifndef ", i(1), t({ "", "" }),
|
||||
i(0),
|
||||
t({ "", "#endif /* ifndef " }), f(ref, 1), t " */",
|
||||
}),
|
||||
s({ trig = "once", name = "Header Include Guard" }, {
|
||||
t "#ifndef ", i(1), t({ "_H", "" }),
|
||||
t "#define ", f(ref, 1), t({ "_H", "" }),
|
||||
i(0),
|
||||
t({ "", "#endif /* end of include guard: " }), f(ref, 1), t "_H */",
|
||||
}),
|
||||
s({ trig = "nocpp", name = "extern C", dscr = "Disable C++ name mangling in C headers" }, {
|
||||
t({
|
||||
"#ifdef __cplusplus",
|
||||
"extern \"C\" {",
|
||||
"#endif", ""
|
||||
}),
|
||||
i(0),
|
||||
t({
|
||||
"", "#ifdef __cplusplus",
|
||||
"} /* extern \"C\" */",
|
||||
"#endif",
|
||||
}),
|
||||
}),
|
||||
s({ trig = "#err", name = "#error" }, {
|
||||
t "#error \"", i(1), t "\"",
|
||||
}),
|
||||
s({ trig = "#warn", name = "#warning" }, {
|
||||
t "#warning \"", i(1), t "\"",
|
||||
}),
|
||||
s({ trig = "if", name = "if" }, {
|
||||
t "if (", i(1), t({ ") {", "" }),
|
||||
t "\t", i(2),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "ife", name = "if else" }, {
|
||||
t "if (", i(1), t({ ") {", "" }),
|
||||
t "\t", i(2),
|
||||
t({ "", "} else {", "\t" }),
|
||||
i(3),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "elif", name = "else if" }, {
|
||||
t "else if (", i(1), t({ ") {", "" }),
|
||||
t "\t", i(2),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "el", name = "else" }, {
|
||||
t({ "else {", "" }),
|
||||
t "\t", i(1),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "t", name = "Ternary" }, {
|
||||
i(1), t " ? ", i(2), t " : ", i(3),
|
||||
}),
|
||||
s({ trig = "switch", name = "Switch" }, {
|
||||
t "switch(", i(1), t({ ")", "\t" }),
|
||||
i(0),
|
||||
t({ "", "\tdefault:" }), i(2),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "switchn", name = "Switch Without Default" }, {
|
||||
t "switch(", i(1), t({ ")", "\t" }),
|
||||
i(2),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "case", name = "Case" }, {
|
||||
t "case", i(1), t({ ":", "\t" }),
|
||||
i(0),
|
||||
}),
|
||||
s({ trig = "while", name = "While" }, {
|
||||
t "while (", i(1), t({ ") {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "do", name = "Do While" }, {
|
||||
t({ "do {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "while (" }), i(1), t ")",
|
||||
}),
|
||||
s({ trig = "for", name = "For" }, {
|
||||
t "for (", i(1), t ({") {", "\t"}),
|
||||
i(0),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "fund", name = "Function Declaration" }, {
|
||||
i(1), t "(", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "fun", name = "Function Definition" }, {
|
||||
i(1), t "(", i(2), t({ ") {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "}" }),
|
||||
}),
|
||||
s({ trig = "st", name = "Struct" }, {
|
||||
t "struct ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "};" }),
|
||||
}),
|
||||
s({ trig = "stt", name = "Struct Type" }, {
|
||||
t "typedef struct ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "} " }), f(ref, 1), t ";",
|
||||
}),
|
||||
s({ trig = "uo", name = "Union" }, {
|
||||
t "union ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "};" }),
|
||||
}),
|
||||
s({ trig = "uot", name = "Union Type" }, {
|
||||
t "typedef union ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "} " }), f(ref, 1), t ";",
|
||||
}),
|
||||
s({ trig = "enum", name = "Enum" }, {
|
||||
t "enum ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "};" }),
|
||||
}),
|
||||
s({ trig = "enumt", name = "Enum Type" }, {
|
||||
t "typedef enum ", i(1), t({ " {", "\t" }),
|
||||
i(0),
|
||||
t({ "", "} " }), f(ref, 1), t ";",
|
||||
}),
|
||||
s({ trig = "pri", name = "printf()" }, {
|
||||
t "printf(\"", i(1, "%s"), t "\\n\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "fpri", name = "fprintf()" }, {
|
||||
t "fprintf(", i(1, "stderr"), t ", \"", i(2, "%s"), t "\\n\"", i(3), t ");",
|
||||
}),
|
||||
s({ trig = "spri", name = "sprintf()" }, {
|
||||
t "sprintf(", i(1, "buf"), t ", \"", i(2, "%s"), t "\\n\"", i(3), t ");",
|
||||
}),
|
||||
s({ trig = "snpri", name = "snprintf()" }, {
|
||||
t "snprintf(", i(1, "buf"), t ", ", i(2, "max"), t ", \"", i(3, "%s"), t "\\n\"", i(4), t ");",
|
||||
}),
|
||||
s({ trig = "sca", name = "scanf()" }, {
|
||||
t "scanf(\"", i(1, "%i"), t "\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "fsca", name = "fscanf()" }, {
|
||||
t "fscanf(", i(1, "stdin"), t ", \"", i(2, "%i"), t "\"", i(3), t ");",
|
||||
}),
|
||||
s({ trig = "ssca", name = "sscanf()" }, {
|
||||
t "sscanf(", i(1, "buf"), t ", \"", i(2, "%i"), t "\"", i(3), t ");",
|
||||
}),
|
||||
s({ trig = "priv", name = "printf()" }, {
|
||||
t "printf(\"", f(ref, 2), t " = ", i(1, "%i"), t "\\n\", ", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "warn", name = "warn()" }, {
|
||||
t "warn(\"", i(1, "%s"), t "\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "warnx", name = "warnx()" }, {
|
||||
t "warnx(\"", i(1, "%s"), t "\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "err", name = "err()" }, {
|
||||
t "err(\"", i(1, "%s"), t "\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "errx", name = "errx()" }, {
|
||||
t "errx(\"", i(1, "%s"), t "\"", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "asr", name = "assert()" }, {
|
||||
t "assert(", i(1), t ");",
|
||||
}),
|
||||
s({ trig = "stasr", name = "static_assert()" }, {
|
||||
t "assert(", i(1), t ", \"", i(2, "Fuck!"), t "\");",
|
||||
}),
|
||||
s({ trig = "Stasr", name = "static_assert() 1 Parameter" }, {
|
||||
t "static_assert(", i(1), t ");",
|
||||
}),
|
||||
s({ trig = "mlc", name = "malloc()" }, {
|
||||
i(1, "ptr"), t " = malloc(sizeof(", i(2, "int"), t "[", i(3), t "])", i(0), t ");"
|
||||
}),
|
||||
s({ trig = "clc", name = "calloc()" }, {
|
||||
i(1, "ptr"), t " = calloc(", i(2), t ", sizeof(", i(3, "int"), t ")", i(0), t ");"
|
||||
}),
|
||||
s({ trig = "rlc", name = "realloc()" }, {
|
||||
i(1, "ptr"), t " = realloc(", i(2), t ", sizeof(", i(3, "int"), t "[", i(4), t "])", i(0), t ");"
|
||||
}),
|
||||
s({ trig = "fre", name = "free()" }, {
|
||||
t "free(", i(1, "ptr"), t ");"
|
||||
}),
|
||||
s({ trig = "mlcd", name = "Malloc Declaration" }, {
|
||||
i(1, "int"), t " *", i(2, "ptr"), t " = malloc(sizeof(", f(ref, 1), t "[", i(3), t "])", i(0), t ");"
|
||||
}),
|
||||
s({ trig = "clcd", name = "Calloc Declaration" }, {
|
||||
i(1, "int"), t " *", i(2, "ptr"), t " = calloc(", i(3), t ", sizeof(", f(ref, 1), t ")", i(0), t ");"
|
||||
}),
|
||||
s({ trig = "mlct", name = "Malloc Template" }, {
|
||||
i(1, "int"), t " *", i(2, "ptr"), t " = malloc(sizeof(", f(ref, 1), t "[", i(3), t "])", t ");",
|
||||
t({ "", " if (!" }), f(ref, 2), t ") {",
|
||||
t({ "", "\tfprintf(stderr, \"" }), i(4, "Failed to malloc!"), t "\\n\"", i(5), t ");",
|
||||
t({ "", "\t" }), i(6, "exit(EXIT_FAILURE);"),
|
||||
t({ "", "}", "", "" }),
|
||||
i(0),
|
||||
t("", "", ""),
|
||||
t "free(", f(ref, 2), t ");"
|
||||
}),
|
||||
s({ trig = "clct", name = "Calloc Template" }, {
|
||||
i(1, "int"), t " *", i(2, "ptr"), t " = calloc(", i(3), t ", sizeof(", f(ref, 1), t "));",
|
||||
t({ "", " if (!" }), f(ref, 2), t ") {",
|
||||
t({ "", "\tfprintf(stderr, \"" }), i(4, "Failed to calloc!"), t "\\n\"", i(5), t ");",
|
||||
t({ "", "\t" }), i(6, "exit(EXIT_FAILURE);"),
|
||||
t({ "", "}", "", "" }),
|
||||
i(0),
|
||||
t("", "", ""),
|
||||
t "free(", f(ref, 2), t ");"
|
||||
}),
|
||||
s({ trig = "/", name = "Comment" }, {
|
||||
t "/*", i(1), t "*/"
|
||||
}),
|
||||
s({ trig = "//", name = "Multiline Comment" }, {
|
||||
t({ "/*", "" }),
|
||||
i(0),
|
||||
t({ "", "*/" })
|
||||
}),
|
||||
s({ trig = "///", name = "Multiline Double-Star Comment" }, {
|
||||
t({ "/**", "" }),
|
||||
i(0),
|
||||
t({ "", "**/" })
|
||||
}),
|
||||
s({ trig = "dox", name = "Doxygen Template" }, {
|
||||
t "/*!", i(1),
|
||||
t({ "", " * @brief " }), i(2),
|
||||
t({ "", " *" }),
|
||||
t({ "", " * " }), i(0),
|
||||
t({ "", " */" }),
|
||||
}),
|
||||
s({ trig = "dfun", name = "Doxygen Function" }, {
|
||||
t "/*!", i(3),
|
||||
t({ "", " * @brief " }), i(4),
|
||||
t({ "", " *" }),
|
||||
t({ "", " * " }), i(0),
|
||||
t({ "", " *" }),
|
||||
t({ "", " * @return " }), i(5),
|
||||
t({ "", " */" }),
|
||||
i(1), t "(", i(2), t ");",
|
||||
}),
|
||||
s({ trig = "todo", name = "Doxygen Todo" }, {
|
||||
t "/*! TODO: ", i(1),
|
||||
t({ "", " *" }),
|
||||
t({ "", " * @todo " }), i(0),
|
||||
t({ "", " */" }),
|
||||
}),
|
||||
s({ trig = "dgr", name = "Doxygen Group" }, {
|
||||
t "/*! @name ", i(1),
|
||||
t({ "", " * @{" }),
|
||||
t({ "", " */", "" }),
|
||||
i(0),
|
||||
t({ "", "/*! @}" }),
|
||||
t({ "", " */" }),
|
||||
}),
|
||||
s({ trig = "alen", name = "Array Length" }, {
|
||||
t "(sizeof ", i(1), t " / sizeof ", f(ref, 1), t "[0])"
|
||||
}),
|
||||
s({ trig = "lls", name = "Linked List" }, {
|
||||
t "typedef struct ", i(1, "Node"), f(ref, 1), t({ ";", "" }),
|
||||
t "struct ", f(ref, 1), t({ "{", "\t" }),
|
||||
i(0),
|
||||
t({ "", "\t" }), f(ref, 1), i(2, "next"), t({";", ""}),
|
||||
t"};"
|
||||
}),
|
||||
}
|
Loading…
Add table
Reference in a new issue