[nvim] fix: snippets config

This commit is contained in:
David JULIEN 2022-04-29 00:20:18 +02:00
parent 6581d9af42
commit 6fd5067bc4
3 changed files with 367 additions and 368 deletions

View File

@ -20,32 +20,32 @@ local f = ls.function_node
local copy = utils.copy local copy = utils.copy
ls.snippets = { ls.add_snippets( "c", {
c = { s("main", {
s("main", { t({ "int main(int argc, char* argv[])" }),
t({ "int main(int argc, char* argv[])" }), t({ "", "{", "\t" }),
t({ "", "{", "\t" }), i(0),
t({ "", "\treturn 0;"}),
t({ "", "}"}),
}),
s("fn", {
-- Simple static text.
t("//Parameters: "),
-- function, first parameter is the function, second the Placeholders
-- whose text it gets as input.
f(copy, 2),
t({ "", "" }),
-- Placeholder/Insert.
i(1),
t("("),
-- Placeholder with initial text.
i(2, "int foo"),
-- Linebreak
t({ ")", "{", "\t" }),
i(0), i(0),
t({ "", "\treturn 0;"}), t({ "", "\treturn 0;"}),
t({ "", "}"}), t({ "", "}"}),
}), }),
s("fn", { }, {
-- Simple static text. key = "c"
t("//Parameters: "), })
-- function, first parameter is the function, second the Placeholders
-- whose text it gets as input.
f(copy, 2),
t({ "", "" }),
-- Placeholder/Insert.
i(1),
t("("),
-- Placeholder with initial text.
i(2, "int foo"),
-- Linebreak
t({ ")", "{", "\t" }),
i(0),
t({ "", "\treturn 0;"}),
t({ "", "}"}),
}),
},
}

View File

@ -44,86 +44,86 @@ local function rec_ls()
) )
end end
ls.snippets = { ls.add_snippets("tex", {
tex = { -- rec_ls is self-referencing. That makes this snippet 'infinite' eg. have as many
-- rec_ls is self-referencing. That makes this snippet 'infinite' eg. have as many -- \item as necessary by utilizing a choiceNode.
-- \item as necessary by utilizing a choiceNode. s("ls", {
s("ls", { t({ "\\begin{itemize}", "\t\\item " }),
t({ "\\begin{itemize}", "\t\\item " }), i(1),
i(1), t({ "" }),
t({ "" }), d(2, rec_ls, {}),
d(2, rec_ls, {}), t({ "", "\\end{itemize}", "" }),
t({ "", "\\end{itemize}", "" }), }),
}), s("lsl", {
s("lsl", { t({ "\\begin{itemize}[label=" }),
t({ "\\begin{itemize}[label=" }), i(1),
i(1), t( { "]", "\t\\item " }),
t( { "]", "\t\\item " }), i(2),
i(2), t({ "" }),
t({ "" }), d(3, rec_ls, {}),
d(3, rec_ls, {}), t({ "", "\\end{itemize}", "" }),
t({ "", "\\end{itemize}", "" }), }),
}), s("frm", {
s("frm", { t({ "\\begin{" }),
t({ "\\begin{" }), i(1),
i(1), t({ "}[" }),
t({ "}[" }), i(2),
i(2), t({ "]{" }),
t({ "]{" }), f(copy, 1),
f(copy, 1), t({ ":" }),
t({ ":" }), i(3, "foo"),
i(3, "foo"), t({ "}", "\t" }),
t({ "}", "\t" }), i(0),
i(0), t({ "", "\\end{" }),
t({ "", "\\end{" }), f(copy, 1),
f(copy, 1), t({ "}", "" }),
t({ "}", "" }), }),
}), s("env", {
s("env", { t({ "\\begin{" }),
t({ "\\begin{" }), i(1, "foo"),
i(1, "foo"), t({ "}", "\t" }),
t({ "}", "\t" }), i(2),
i(2), t({ "", "\\end{" }),
t({ "", "\\end{" }), f(copy,1),
f(copy,1), t({ "}"}),
t({ "}"}), }),
}), s("tikz", {
s("tikz", { t({ "\\begin{center}" }),
t({ "\\begin{center}" }), t({ "", "\t\\begin{tikzpicture}[main/.style = {draw,circle}]", "\t\t" }),
t({ "", "\t\\begin{tikzpicture}[main/.style = {draw,circle}]", "\t\t" }), i(1),
i(1), t({ "", "\t\\end{tikzpicture}", "\\end{center}" }),
t({ "", "\t\\end{tikzpicture}", "\\end{center}" }), }),
}), s("ra", { t( "\\rightarrow" )}),
s("ra", { t( "\\rightarrow" )}), s("Ra", { t("\\Rightarrow" )}),
s("Ra", { t("\\Rightarrow" )}), s("la", { t("\\leftarrow" )}),
s("la", { t("\\leftarrow" )}), s("La", { t("\\Leftarrow" )}),
s("La", { t("\\Leftarrow" )}), s("lra", { t("\\leftrightarrow" )}),
s("lra", { t("\\leftrightarrow" )}), s("Lra", { t("\\Leftrightarrow" )}),
s("Lra", { t("\\Leftrightarrow" )}), s("bb", {
s("bb", { t({ "\\textbf{" }),
t({ "\\textbf{" }),
i(1),
t({ "}"}),
}),
s("tt", {
t({ "\\texttt{" }),
i(1), i(1),
t({ "}"}), t({ "}"}),
}), }),
s("ii", { s("tt", {
t({ "\\textit{" }), t({ "\\texttt{" }),
i(1), i(1),
t({ "}"}), t({ "}"}),
}), }),
s("uu", { s("ii", {
t({ "\\underline{" }), t({ "\\textit{" }),
i(1), i(1),
t({ "}"}), t({ "}"}),
}), }),
s("fsc", { s("uu", {
t({ "\\textsc{" }), t({ "\\underline{" }),
i(1), i(1),
t({ "}"}), t({ "}"}),
}), }),
}, s("fsc", {
} t({ "\\textsc{" }),
i(1),
t({ "}"}),
}),
}, {
key = "tex"
})

View File

@ -59,270 +59,269 @@ local function date_input(fmt)
return sn(nil, i(1, os.date(fmt))) return sn(nil, i(1, os.date(fmt)))
end end
ls.snippets = { -- snippets are added via ls.add_snippets(filetype, snippets[, opts]), where
-- When trying to expand a snippet, luasnip first searches the tables for -- opts may specify the `type` of the snippets ("snippets" or "autosnippets",
-- each filetype specified in 'filetype' followed by 'all'. -- for snippets that should expand directly after the trigger is typed).
-- If ie. the filetype is 'lua.c' --
-- - luasnip.lua -- opts can also specify a key. By passing an unique key to each add_snippets, it's possible to reload snippets by
-- - luasnip.c -- re-`:luafile`ing the file in which they are defined (eg. this one).
-- - luasnip.all ls.add_snippets( "all", {
-- are searched in that order.
all = {
-- trigger is fn. -- trigger is fn.
s("fn", { s("fn", {
-- Simple static text. -- Simple static text.
t("//Parameters: "), t("//Parameters: "),
-- function, first parameter is the function, second the Placeholders -- function, first parameter is the function, second the Placeholders
-- whose text it gets as input. -- whose text it gets as input.
f(copy, 2), f(copy, 2),
t({ "", "function " }), t({ "", "function " }),
-- Placeholder/Insert. -- Placeholder/Insert.
i(1), i(1),
t("("), t("("),
-- Placeholder with initial text. -- Placeholder with initial text.
i(2, "int foo"), i(2, "int foo"),
-- Linebreak -- Linebreak
t({ ") {", "\t" }), t({ ") {", "\t" }),
-- Last Placeholder, exit Point of the snippet. EVERY 'outer' SNIPPET NEEDS Placeholder 0. -- Last Placeholder, exit Point of the snippet. EVERY 'outer' SNIPPET NEEDS Placeholder 0.
i(0), i(0),
t({ "", "}" }), t({ "", "}" }),
}), }),
s("class", { s("class", {
-- Choice: Switch between two different Nodes, first parameter is its position, second a list of nodes. -- Choice: Switch between two different Nodes, first parameter is its position, second a list of nodes.
c(1, { c(1, {
t("public "), t("public "),
t("private "), t("private "),
}), }),
t("class "), t("class "),
i(2), i(2),
t(" "), t(" "),
c(3, { c(3, {
t("{"), t("{"),
-- sn: Nested Snippet. Instead of a trigger, it has a position, just like insert-nodes. !!! These don't expect a 0-node!!!! -- sn: Nested Snippet. Instead of a trigger, it has a position, just like insert-nodes. !!! These don't expect a 0-node!!!!
-- Inside Choices, Nodes don't need a position as the choice node is the one being jumped to. -- Inside Choices, Nodes don't need a position as the choice node is the one being jumped to.
sn(nil, { sn(nil, {
t("extends "), t("extends "),
i(1), i(1),
t(" {"), t(" {"),
}), }),
sn(nil, { sn(nil, {
t("implements "), t("implements "),
i(1), i(1),
t(" {"), t(" {"),
}), }),
}), }),
t({ "", "\t" }), t({ "", "\t" }),
i(0), i(0),
t({ "", "}" }), t({ "", "}" }),
}), }),
-- Use a dynamic_node to interpolate the output of a -- Use a dynamic_node to interpolate the output of a
-- function (see date_input above) into the initial -- function (see date_input above) into the initial
-- value of an insert_node. -- value of an insert_node.
s("novel", { s("novel", {
t("It was a dark and stormy night on "), t("It was a dark and stormy night on "),
d(1, date_input, {}, "%A, %B %d of %Y"), d(1, date_input, {}, "%A, %B %d of %Y"),
t(" and the clocks were striking thirteen."), t(" and the clocks were striking thirteen."),
}), }),
-- Parsing snippets: First parameter: Snippet-Trigger, Second: Snippet body. -- Parsing snippets: First parameter: Snippet-Trigger, Second: Snippet body.
-- Placeholders are parsed into choices with 1. the placeholder text(as a snippet) and 2. an empty string. -- Placeholders are parsed into choices with 1. the placeholder text(as a snippet) and 2. an empty string.
-- This means they are not SELECTed like in other editors/Snippet engines. -- This means they are not SELECTed like in other editors/Snippet engines.
ls.parser.parse_snippet( ls.parser.parse_snippet(
"lspsyn", "lspsyn",
"Wow! This ${1:Stuff} really ${2:works. ${3:Well, a bit.}}" "Wow! This ${1:Stuff} really ${2:works. ${3:Well, a bit.}}"
), ),
-- When wordTrig is set to false, snippets may also expand inside other words. -- When wordTrig is set to false, snippets may also expand inside other words.
ls.parser.parse_snippet( ls.parser.parse_snippet(
{ trig = "te", wordTrig = false }, { trig = "te", wordTrig = false },
"${1:cond} ? ${2:true} : ${3:false}" "${1:cond} ? ${2:true} : ${3:false}"
), ),
-- When regTrig is set, trig is treated like a pattern, this snippet will expand after any number. -- When regTrig is set, trig is treated like a pattern, this snippet will expand after any number.
ls.parser.parse_snippet({ trig = "%d", regTrig = true }, "A Number!!"), ls.parser.parse_snippet({ trig = "%d", regTrig = true }, "A Number!!"),
-- Using the condition, it's possible to allow expansion only in specific cases. -- Using the condition, it's possible to allow expansion only in specific cases.
s("cond", { s("cond", {
t("will only expand in c-style comments"), t("will only expand in c-style comments"),
}, { }, {
condition = function(line_to_cursor, matched_trigger, captures) condition = function(line_to_cursor, matched_trigger, captures)
-- optional whitespace followed by // -- optional whitespace followed by //
return line_to_cursor:match("%s*//") return line_to_cursor:match("%s*//")
end, end,
}), }),
-- there's some built-in conditions in "luasnip.extras.expand_conditions". -- there's some built-in conditions in "luasnip.extras.expand_conditions".
s("cond2", { s("cond2", {
t("will only expand at the beginning of the line"), t("will only expand at the beginning of the line"),
}, { }, {
condition = conds.line_begin, condition = conds.line_begin,
}), }),
-- The last entry of args passed to the user-function is the surrounding snippet. -- The last entry of args passed to the user-function is the surrounding snippet.
s( s(
{ trig = "a%d", regTrig = true }, { trig = "a%d", regTrig = true },
f(function(_, snip) f(function(_, snip)
return "Triggered with " .. snip.trigger .. "." return "Triggered with " .. snip.trigger .. "."
end, {}) end, {})
), ),
-- It's possible to use capture-groups inside regex-triggers. -- It's possible to use capture-groups inside regex-triggers.
s( s(
{ trig = "b(%d)", regTrig = true }, { trig = "b(%d)", regTrig = true },
f(function(_, snip) f(function(_, snip)
return "Captured Text: " .. snip.captures[1] .. "." return "Captured Text: " .. snip.captures[1] .. "."
end, {}) end, {})
), ),
s({ trig = "c(%d+)", regTrig = true }, { s({ trig = "c(%d+)", regTrig = true }, {
t("will only expand for even numbers"), t("will only expand for even numbers"),
}, { }, {
condition = function(line_to_cursor, matched_trigger, captures) condition = function(line_to_cursor, matched_trigger, captures)
return tonumber(captures[1]) % 2 == 0 return tonumber(captures[1]) % 2 == 0
end, end,
}), }),
-- Use a function to execute any shell command and print its text. -- Use a function to execute any shell command and print its text.
s("bash", f(bash, {}, "ls")), s("bash", f(bash, {}, "ls")),
-- Short version for applying String transformations using function nodes. -- Short version for applying String transformations using function nodes.
s("transform", { s("transform", {
i(1, "initial text"), i(1, "initial text"),
t({ "", "" }), t({ "", "" }),
-- lambda nodes accept an l._1,2,3,4,5, which in turn accept any string transformations. -- lambda nodes accept an l._1,2,3,4,5, which in turn accept any string transformations.
-- This list will be applied in order to the first node given in the second argument. -- This list will be applied in order to the first node given in the second argument.
l(l._1:match("[^i]*$"):gsub("i", "o"):gsub(" ", "_"):upper(), 1), l(l._1:match("[^i]*$"):gsub("i", "o"):gsub(" ", "_"):upper(), 1),
}), }),
s("transform2", { s("transform2", {
i(1, "initial text"), i(1, "initial text"),
t("::"), t("::"),
i(2, "replacement for e"), i(2, "replacement for e"),
t({ "", "" }), t({ "", "" }),
-- Lambdas can also apply transforms USING the text of other nodes: -- Lambdas can also apply transforms USING the text of other nodes:
l(l._1:gsub("e", l._2), { 1, 2 }), l(l._1:gsub("e", l._2), { 1, 2 }),
}), }),
s({ trig = "trafo(%d+)", regTrig = true }, { s({ trig = "trafo(%d+)", regTrig = true }, {
-- env-variables and captures can also be used: -- env-variables and captures can also be used:
l(l.CAPTURE1:gsub("1", l.TM_FILENAME), {}), l(l.CAPTURE1:gsub("1", l.TM_FILENAME), {}),
}), }),
-- Set store_selection_keys = "<Tab>" (for example) in your -- Set store_selection_keys = "<Tab>" (for example) in your
-- luasnip.config.setup() call to access TM_SELECTED_TEXT. In -- luasnip.config.setup() call to access TM_SELECTED_TEXT. In
-- this case, select a URL, hit Tab, then expand this snippet. -- this case, select a URL, hit Tab, then expand this snippet.
s("link_url", { s("link_url", {
t('<a href="'), t('<a href="'),
f(function(_, snip) f(function(_, snip)
return snip.env.TM_SELECTED_TEXT[1] or {} return snip.env.TM_SELECTED_TEXT[1] or {}
end, {}), end, {}),
t('">'), t('">'),
i(1), i(1),
t("</a>"), t("</a>"),
i(0), i(0),
}), }),
-- Shorthand for repeating the text in a given node. -- Shorthand for repeating the text in a given node.
s("repeat", { i(1, "text"), t({ "", "" }), r(1) }), s("repeat", { i(1, "text"), t({ "", "" }), r(1) }),
-- Directly insert the ouput from a function evaluated at runtime. -- Directly insert the ouput from a function evaluated at runtime.
s("part", p(os.date, "%Y")), s("part", p(os.date, "%Y")),
-- use matchNodes to insert text based on a pattern/function/lambda-evaluation. -- use matchNodes to insert text based on a pattern/function/lambda-evaluation.
s("mat", { s("mat", {
i(1, { "sample_text" }), i(1, { "sample_text" }),
t(": "), t(": "),
m(1, "%d", "contains a number", "no number :("), m(1, "%d", "contains a number", "no number :("),
}), }),
-- The inserted text defaults to the first capture group/the entire -- The inserted text defaults to the first capture group/the entire
-- match if there are none -- match if there are none
s("mat2", { s("mat2", {
i(1, { "sample_text" }), i(1, { "sample_text" }),
t(": "), t(": "),
m(1, "[abc][abc][abc]"), m(1, "[abc][abc][abc]"),
}), }),
-- It is even possible to apply gsubs' or other transformations -- It is even possible to apply gsubs' or other transformations
-- before matching. -- before matching.
s("mat3", { s("mat3", {
i(1, { "sample_text" }), i(1, { "sample_text" }),
t(": "), t(": "),
m( m(
1, 1,
l._1:gsub("[123]", ""):match("%d"), l._1:gsub("[123]", ""):match("%d"),
"contains a number that isn't 1, 2 or 3!" "contains a number that isn't 1, 2 or 3!"
), ),
}), }),
-- `match` also accepts a function, which in turn accepts a string -- `match` also accepts a function, which in turn accepts a string
-- (text in node, \n-concatted) and returns any non-nil value to match. -- (text in node, \n-concatted) and returns any non-nil value to match.
-- If that value is a string, it is used for the default-inserted text. -- If that value is a string, it is used for the default-inserted text.
s("mat4", { s("mat4", {
i(1, { "sample_text" }), i(1, { "sample_text" }),
t(": "), t(": "),
m(1, function(text) m(1, function(text)
return (#text % 2 == 0 and text) or nil return (#text % 2 == 0 and text) or nil
end), end),
}), }),
-- The nonempty-node inserts text depending on whether the arg-node is -- The nonempty-node inserts text depending on whether the arg-node is
-- empty. -- empty.
s("nempty", { s("nempty", {
i(1, "sample_text"), i(1, "sample_text"),
n(1, "i(1) is not empty!"), n(1, "i(1) is not empty!"),
}), }),
-- dynamic lambdas work exactly like regular lambdas, except that they -- dynamic lambdas work exactly like regular lambdas, except that they
-- don't return a textNode, but a dynamicNode containing one insertNode. -- don't return a textNode, but a dynamicNode containing one insertNode.
-- This makes it easier to dynamically set preset-text for insertNodes. -- This makes it easier to dynamically set preset-text for insertNodes.
s("dl1", { s("dl1", {
i(1, "sample_text"), i(1, "sample_text"),
t({ ":", "" }), t({ ":", "" }),
dl(2, l._1, 1), dl(2, l._1, 1),
}), }),
-- Obviously, it's also possible to apply transformations, just like lambdas. -- Obviously, it's also possible to apply transformations, just like lambdas.
s("dl2", { s("dl2", {
i(1, "sample_text"), i(1, "sample_text"),
i(2, "sample_text_2"), i(2, "sample_text_2"),
t({ "", "" }), t({ "", "" }),
dl(3, l._1:gsub("\n", " linebreak ") .. l._2, { 1, 2 }), dl(3, l._1:gsub("\n", " linebreak ") .. l._2, { 1, 2 }),
}), }),
-- Alternative printf-like notation for defining snippets. It uses format -- Alternative printf-like notation for defining snippets. It uses format
-- string with placeholders similar to the ones used with Python's .format(). -- string with placeholders similar to the ones used with Python's .format().
s( s(
"fmt1", "fmt1",
fmt("To {title} {} {}.", { fmt("To {title} {} {}.", {
i(2, "Name"), i(2, "Name"),
i(3, "Surname"), i(3, "Surname"),
title = c(1, { t("Mr."), t("Ms.") }), title = c(1, { t("Mr."), t("Ms.") }),
}) })
), ),
-- To escape delimiters use double them, e.g. `{}` -> `{{}}`. -- To escape delimiters use double them, e.g. `{}` -> `{{}}`.
-- Multi-line format strings by default have empty first/last line removed. -- Multi-line format strings by default have empty first/last line removed.
-- Indent common to all lines is also removed. Use the third `opts` argument -- Indent common to all lines is also removed. Use the third `opts` argument
-- to control this behaviour. -- to control this behaviour.
s( s(
"fmt2", "fmt2",
fmt( fmt(
[[ [[
foo({1}, {3}) {{ foo({1}, {3}) {{
return {2} * {4} return {2} * {4}
}} }}
]], ]],
{ {
i(1, "x"), i(1, "x"),
r(1), r(1),
i(2, "y"), i(2, "y"),
r(2), r(2),
} }
) )
), ),
-- Empty placeholders are numbered automatically starting from 1 or the last -- Empty placeholders are numbered automatically starting from 1 or the last
-- value of a numbered placeholder. Named placeholders do not affect numbering. -- value of a numbered placeholder. Named placeholders do not affect numbering.
s( s(
"fmt3", "fmt3",
fmt("{} {a} {} {1} {}", { fmt("{} {a} {} {1} {}", {
t("1"), t("1"),
t("2"), t("2"),
a = t("A"), a = t("A"),
}) })
), ),
-- The delimiters can be changed from the default `{}` to something else. -- The delimiters can be changed from the default `{}` to something else.
s( s(
"fmt4", "fmt4",
fmt("foo() { return []; }", i(1, "x"), { delimiters = "[]" }) fmt("foo() { return []; }", i(1, "x"), { delimiters = "[]" })
), ),
-- `fmta` is a convenient wrapper that uses `<>` instead of `{}`. -- `fmta` is a convenient wrapper that uses `<>` instead of `{}`.
s("fmt5", fmta("foo() { return <>; }", i(1, "x"))), s("fmt5", fmta("foo() { return <>; }", i(1, "x"))),
-- By default all args must be used. Use strict=false to disable the check -- By default all args must be used. Use strict=false to disable the check
s( s(
"fmt6", "fmt6",
fmt("use {} only", { t("this"), t("not this") }, { strict = false }) fmt("use {} only", { t("this"), t("not this") }, { strict = false })
), ),
}, }, {
} key = "all",
})
-- autotriggered snippets have to be defined in a separate table, luasnip.autosnippets. -- autotriggered snippets have to be defined in a separate table, luasnip.autosnippets.
ls.autosnippets = { ls.autosnippets = {