[nvim] cleanup: move tex snippets to ftplugin
move tex snippets to after/ftplugin/tex.lua add copy function to utils
This commit is contained in:
parent
28d21abbda
commit
9f940e19da
@ -19,13 +19,107 @@ utils.map("i", "EE", "É")
|
||||
utils.map("i", "<Space>etc", "\\,etc.")
|
||||
utils.map("i", "<Space>:", "\\,:")
|
||||
|
||||
-- Centered point
|
||||
utils.map("i", ";.", "\\textperiodcentered")
|
||||
-- Italics
|
||||
utils.map("i", "II", "\\textit{}<Esc>i")
|
||||
-- Bold
|
||||
utils.map("i", "BB", "\\textbf{}<Esc>i")
|
||||
-- Bold + Italics
|
||||
utils.map("i", "BI", "\\textit{\\textbf{}}<Esc>hi")
|
||||
-- Smallcaps
|
||||
utils.map("i", "SC", "\\fsc{}<Esc>i")
|
||||
-- Snippets
|
||||
local ls = require("luasnip")
|
||||
|
||||
-- some shorthands...
|
||||
local s = ls.snippet
|
||||
local sn = ls.snippet_node
|
||||
local t = ls.text_node
|
||||
local i = ls.insert_node
|
||||
local f = ls.function_node
|
||||
local c = ls.choice_node
|
||||
local d = ls.dynamic_node
|
||||
|
||||
local copy = utils.copy
|
||||
|
||||
-- 'recursive' dynamic snippet. Expands to some text followed by itself.
|
||||
local function rec_ls()
|
||||
return sn(
|
||||
nil,
|
||||
c(1, {
|
||||
-- Order is important, sn(...) first would cause infinite loop of expansion.
|
||||
t(""),
|
||||
sn(nil, { t({ "", "", "\t\\item " }), i(1), d(2, rec_ls, {}) }),
|
||||
})
|
||||
)
|
||||
end
|
||||
|
||||
ls.snippets = {
|
||||
tex = {
|
||||
-- rec_ls is self-referencing. That makes this snippet 'infinite' eg. have as many
|
||||
-- \item as necessary by utilizing a choiceNode.
|
||||
s("ls", {
|
||||
t({ "\\begin{itemize}", "\t\\item " }),
|
||||
i(1),
|
||||
t({ "" }),
|
||||
d(2, rec_ls, {}),
|
||||
t({ "", "\\end{itemize}", "" }),
|
||||
}),
|
||||
s("lsl", {
|
||||
t({ "\\begin{itemize}[label=" }),
|
||||
i(1),
|
||||
t( { "]", "\t\\item " }),
|
||||
i(2),
|
||||
t({ "" }),
|
||||
d(3, rec_ls, {}),
|
||||
t({ "", "\\end{itemize}", "" }),
|
||||
}),
|
||||
s("frm", {
|
||||
t({ "\\begin{" }),
|
||||
i(1),
|
||||
t({ "}[" }),
|
||||
i(2),
|
||||
t({ "]{" }),
|
||||
f(copy, 1),
|
||||
t({ ":" }),
|
||||
i(3, "foo"),
|
||||
t({ "}", "\t" }),
|
||||
i(0),
|
||||
t({ "", "\\end{" }),
|
||||
f(copy, 1),
|
||||
t({ "}", "" }),
|
||||
}),
|
||||
s("env", {
|
||||
t({ "\\begin{" }),
|
||||
i(1, "foo"),
|
||||
t({ "}", "\t" }),
|
||||
i(2),
|
||||
t({ "", "\\end{" }),
|
||||
f(copy,1),
|
||||
t({ "}", "" }),
|
||||
}),
|
||||
s("tikz", {
|
||||
t({ "\\begin{center}" }),
|
||||
t({ "", "\t\\begin{tikzpicture}[main/.style = {draw,circle}]", "\t\t" }),
|
||||
i(1),
|
||||
t({ "", "\t\\end{tikzpicture}", "\\end{center}" }),
|
||||
}),
|
||||
s("ra", { t( "\\rightarrow" )}),
|
||||
s("Ra", { t("\\Rightarrow" )}),
|
||||
s("la", { t("\\leftarrow" )}),
|
||||
s("La", { t("\\Leftarrow" )}),
|
||||
s("lra", { t("\\leftrightarrow" )}),
|
||||
s("Lra", { t("\\Leftrightarrow" )}),
|
||||
s("bb", {
|
||||
t({ "\\textbf{" }),
|
||||
i(1),
|
||||
t({ "} "}),
|
||||
}),
|
||||
s("tt", {
|
||||
t({ "\\texttt{" }),
|
||||
i(1),
|
||||
t({ "} "}),
|
||||
}),
|
||||
s("ii", {
|
||||
t({ "\\textit{" }),
|
||||
i(1),
|
||||
t({ "} "}),
|
||||
}),
|
||||
s("fsc", {
|
||||
t({ "\\fsc{" }),
|
||||
i(1),
|
||||
t({ "} "}),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
@ -42,109 +42,6 @@ ls.config.set_config({
|
||||
enable_autosnippets = true,
|
||||
})
|
||||
|
||||
-- args is a table, where 1 is the text in Placeholder 1, 2 the text in
|
||||
-- placeholder 2,...
|
||||
local function copy(args)
|
||||
return args[1]
|
||||
end
|
||||
|
||||
-- 'recursive' dynamic snippet. Expands to some text followed by itself.
|
||||
local rec_ls
|
||||
rec_ls = function()
|
||||
return sn(
|
||||
nil,
|
||||
c(1, {
|
||||
-- Order is important, sn(...) first would cause infinite loop of expansion.
|
||||
t(""),
|
||||
sn(nil, { t({ "", "", "\t\\item " }), i(1), d(2, rec_ls, {}) }),
|
||||
})
|
||||
)
|
||||
end
|
||||
|
||||
-- complicated function for dynamicNode.
|
||||
local function jdocsnip(args, _, old_state)
|
||||
local nodes = {
|
||||
t({ "/**", " * " }),
|
||||
i(1, "A short Description"),
|
||||
t({ "", "" }),
|
||||
}
|
||||
|
||||
-- These will be merged with the snippet; that way, should the snippet be updated,
|
||||
-- some user input eg. text can be referred to in the new snippet.
|
||||
local param_nodes = {}
|
||||
|
||||
if old_state then
|
||||
nodes[2] = i(1, old_state.descr:get_text())
|
||||
end
|
||||
param_nodes.descr = nodes[2]
|
||||
|
||||
-- At least one param.
|
||||
if string.find(args[2][1], ", ") then
|
||||
vim.list_extend(nodes, { t({ " * ", "" }) })
|
||||
end
|
||||
|
||||
local insert = 2
|
||||
for indx, arg in ipairs(vim.split(args[2][1], ", ", true)) do
|
||||
-- Get actual name parameter.
|
||||
arg = vim.split(arg, " ", true)[2]
|
||||
if arg then
|
||||
local inode
|
||||
-- if there was some text in this parameter, use it as static_text for this new snippet.
|
||||
if old_state and old_state[arg] then
|
||||
inode = i(insert, old_state["arg" .. arg]:get_text())
|
||||
else
|
||||
inode = i(insert)
|
||||
end
|
||||
vim.list_extend(
|
||||
nodes,
|
||||
{ t({ " * @param " .. arg .. " " }), inode, t({ "", "" }) }
|
||||
)
|
||||
param_nodes["arg" .. arg] = inode
|
||||
|
||||
insert = insert + 1
|
||||
end
|
||||
end
|
||||
|
||||
if args[1][1] ~= "void" then
|
||||
local inode
|
||||
if old_state and old_state.ret then
|
||||
inode = i(insert, old_state.ret:get_text())
|
||||
else
|
||||
inode = i(insert)
|
||||
end
|
||||
|
||||
vim.list_extend(
|
||||
nodes,
|
||||
{ t({ " * ", " * @return " }), inode, t({ "", "" }) }
|
||||
)
|
||||
param_nodes.ret = inode
|
||||
insert = insert + 1
|
||||
end
|
||||
|
||||
if vim.tbl_count(args[3]) ~= 1 then
|
||||
local exc = string.gsub(args[3][2], " throws ", "")
|
||||
local ins
|
||||
if old_state and old_state.ex then
|
||||
ins = i(insert, old_state.ex:get_text())
|
||||
else
|
||||
ins = i(insert)
|
||||
end
|
||||
vim.list_extend(
|
||||
nodes,
|
||||
{ t({ " * ", " * @throws " .. exc .. " " }), ins, t({ "", "" }) }
|
||||
)
|
||||
param_nodes.ex = ins
|
||||
insert = insert + 1
|
||||
end
|
||||
|
||||
vim.list_extend(nodes, { t({ " */" }) })
|
||||
|
||||
local snip = sn(nil, nodes)
|
||||
-- Error on attempting overwrite.
|
||||
snip.old_state = param_nodes
|
||||
return snip
|
||||
end
|
||||
|
||||
-- Make sure to not pass an invalid command, as io.popen() may write over nvim-text.
|
||||
local function bash(_, _, command)
|
||||
local file = io.popen(command, "r")
|
||||
@ -157,7 +54,7 @@ end
|
||||
|
||||
-- Returns a snippet_node wrapped around an insert_node whose initial
|
||||
-- text value is set to the current date in the desired format.
|
||||
local date_input = function(args, state, fmt)
|
||||
local function date_input(fmt)
|
||||
local fmt = fmt or "%Y-%m-%d"
|
||||
return sn(nil, i(1, os.date(fmt)))
|
||||
end
|
||||
@ -425,55 +322,6 @@ ls.snippets = {
|
||||
fmt("use {} only", { t("this"), t("not this") }, { strict = false })
|
||||
),
|
||||
},
|
||||
tex = {
|
||||
-- rec_ls is self-referencing. That makes this snippet 'infinite' eg. have as many
|
||||
-- \item as necessary by utilizing a choiceNode.
|
||||
s("ls", {
|
||||
t({ "\\begin{itemize}", "\t\\item " }),
|
||||
i(1),
|
||||
t({ "" }),
|
||||
d(2, rec_ls, {}),
|
||||
t({ "", "\\end{itemize}", "" }),
|
||||
}),
|
||||
s("lsra", {
|
||||
t({ "\\begin{itemize}[label=$\\rightarrow$]", "\t\\item " }),
|
||||
i(1),
|
||||
t({ "" }),
|
||||
d(2, rec_ls, {}),
|
||||
t({ "", "\\end{itemize}", "" }),
|
||||
}),
|
||||
s("frm", {
|
||||
t({ "\\begin{" }),
|
||||
i(1),
|
||||
t({ "}[" }),
|
||||
i(2),
|
||||
t({ "]{" }),
|
||||
f(copy, 1),
|
||||
t({ ":" }),
|
||||
i(3, "foo"),
|
||||
t({ "}", "\t" }),
|
||||
i(0),
|
||||
t({ "", "\\end{" }),
|
||||
f(copy, 1),
|
||||
t({ "}", "" }),
|
||||
}),
|
||||
s("env", {
|
||||
t({ "\\begin{" }),
|
||||
i(1, "foo"),
|
||||
t({ "}", "\t" }),
|
||||
i(2),
|
||||
t({ "", "\\end{" }),
|
||||
f(copy,1),
|
||||
t({ "}", "" }),
|
||||
}),
|
||||
s("tikz", {
|
||||
t({ "\\begin{center}" }),
|
||||
t({ "", "\t\\begin{tikzpicture}[main/.style = {draw,circle}]", "\t\t" }),
|
||||
i(1),
|
||||
t({ "", "\t\\end{tikzpicture}", "\\end{center}" }),
|
||||
}),
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
-- autotriggered snippets have to be defined in a separate table, luasnip.autosnippets.
|
||||
|
@ -46,6 +46,10 @@ function M.lines_from(file)
|
||||
return lines
|
||||
end
|
||||
|
||||
function M.copy(args)
|
||||
return args[1]
|
||||
end
|
||||
|
||||
-- Make it accessible everywhere
|
||||
_G.utils = M
|
||||
-- Export the module
|
||||
|
Reference in New Issue
Block a user