aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/nv/nvim/plgs/luasnip
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/default.nix86
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/lua/luasnip.lua7
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/all.lua211
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/html/html.lua102
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/delimiter.lua28
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/greek.lua37
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/snippets/all.lua322
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/delimiter.lua41
-rw-r--r--modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/greek.lua49
9 files changed, 493 insertions, 390 deletions
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/default.nix b/modules/by-name/nv/nvim/plgs/luasnip/default.nix
index f67c9899..222b5070 100644
--- a/modules/by-name/nv/nvim/plgs/luasnip/default.nix
+++ b/modules/by-name/nv/nvim/plgs/luasnip/default.nix
@@ -1,3 +1,12 @@
+# nixos-config - My current NixOS configuration
+#
+# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This file is part of my nixos-config.
+#
+# You should have received a copy of the License along with this program.
+# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
lib,
config,
@@ -7,14 +16,81 @@
cfg = config.soispha.programs.nvim;
in {
home-manager.users.soispha.programs.nixvim = lib.mkIf cfg.enable {
+ keymaps = [
+ {
+ key = "<C-K>";
+ mode = ["i"];
+ action.__raw = ''
+ function()
+ require('luasnip').expand()
+ end
+ '';
+ options = {
+ # silent = true;
+ desc = "Expand the snippet";
+ };
+ }
+ {
+ key = "<C-L>";
+ mode = ["i" "s"];
+ action.__raw = ''
+ function()
+ require('luasnip').jump(1)
+ end
+ '';
+ options = {
+ # silent = true;
+ desc = "Jump forward in snippet insert nodes";
+ };
+ }
+ {
+ key = "<C-J>";
+ mode = ["i" "s"];
+ action.__raw = ''
+ function()
+ require('luasnip').jump(-1)
+ end
+ '';
+ options = {
+ # silent = true;
+ desc = "Jump backwards in snippet insert nodes";
+ };
+ }
+ {
+ key = "<C-E>";
+ mode = ["i" "s"];
+ action.__raw = ''
+ function()
+ if require('luasnip').choice_active() then
+ require('luasnip').change_choice(1)
+ end
+ end
+ '';
+ options = {
+ # silent = true;
+ desc = "Cycle through the snippets choice nodes";
+ };
+ }
+ ];
plugins.luasnip = {
enable = true;
+
+ settings = {
+ # Enable auto triggered snippets.
+ enable_autosnippets = true;
+
+ # Use Tab to trigger visual selection.
+ store_selection_keys = "<Tab>";
+ };
+
+ fromLua = [
+ {
+ paths = ./snippets;
+ lazyLoad = true;
+ }
+ ];
};
- extraConfigLuaPost = ''
- ${lib.strings.fileContents ./lua/luasnip.lua};
- require("luasnip.loaders.from_lua").load({paths = "${./lua/snippets}"});
- require("luasnip.loaders.from_lua").lazy_load({paths = "${./lua/snippets}"});
- '';
+
extraPlugins = [
# needed for the todo-comments snippets
pkgs.vimPlugins.comment-nvim
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/lua/luasnip.lua b/modules/by-name/nv/nvim/plgs/luasnip/lua/luasnip.lua
deleted file mode 100644
index a05fa57f..00000000
--- a/modules/by-name/nv/nvim/plgs/luasnip/lua/luasnip.lua
+++ /dev/null
@@ -1,7 +0,0 @@
-require("luasnip").config.set_config({
- -- Enable auto triggered snippets
- enable_autosnippets = true,
-
- -- Use Tab (or some other key if you prefer) to trigger visual selection
- store_selection_keys = "<Tab>",
-})
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/all.lua b/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/all.lua
deleted file mode 100644
index 2b923f20..00000000
--- a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/all.lua
+++ /dev/null
@@ -1,211 +0,0 @@
-local ls = require("luasnip")
--- auto_pairs {{{
-local get_visual = function(args, parent)
- if #parent.snippet.env.SELECT_RAW > 0 then
- return sn(nil, i(1, parent.snippet.env.SELECT_RAW))
- else
- return sn(nil, i(1, ""))
- end
-end
-local function char_count_same(c1, c2)
- local line = vim.api.nvim_get_current_line()
- -- '%'-escape chars to force explicit match (gsub accepts patterns).
- -- second return value is number of substitutions.
- local _, ct1 = string.gsub(line, "%" .. c1, "")
- local _, ct2 = string.gsub(line, "%" .. c2, "")
- return ct1 == ct2
-end
-
-local function even_count(c, ...)
- local line = vim.api.nvim_get_current_line()
- local _, ct = string.gsub(line, c, "")
- return ct % 2 == 0
-end
-
--- This makes creation of pair-type snippets easier.
-local function pair(pair_begin, pair_end, file_types, condition_function)
- -- FIXME(@Soispha): This only works if file_types == nil, otherwise the snippet does not expand.
- -- It would be nice, if it would support both an empty array (`{}`) and nil <2023-08-27>
- -- file_types = file_types or {};
-
- return s(
- { trig = pair_begin, wordTrig = false, snippetType = "autosnippet" },
- { t({ pair_begin }), d(1, get_visual), t({ pair_end }) },
- {
- condition = function()
- local filetype_check = true
- if file_types ~= nil then filetype_check = file_types[vim.bo.filetype] or false end
- return (not condition_function(pair_begin, pair_end)) and filetype_check
- end,
- }
- )
-end
-
-local auto_pairs = {
- pair("(", ")", nil, char_count_same),
- pair("{", "}", nil, char_count_same),
- pair("[", "]", nil, char_count_same),
- pair("<", ">", { ["rust"] = true, ["tex"] = true }, char_count_same),
- pair("'", "'", nil, even_count),
- pair("\"", "\"", nil, even_count),
- pair("`", "`", nil, even_count),
-}
-
-ls.add_snippets("all", auto_pairs, { type = "snippets", key = "auto_pairs" })
--- }}}
-
--- todo_comments {{{
-local calculate_comment_string = require("Comment.ft").calculate
-local utils = require("Comment.utils")
-
-local read_git_config = function(config_value)
- local command = string.format("git config \"%s\"", config_value)
- local handle = io.popen(command)
- if handle == nil then return error(string.format("Failed to call `%s`.", command)) end
- local result = handle:read("*a")
- handle:close()
- -- stripped = string.gsub(str, '%s+', '')
- return string.gsub(result, "\n", "")
-end
-
-local name_to_handle = function(name)
- -- from: https://stackoverflow.com/a/7615129
- local split = function(inputstr, sep)
- local t = {}
- for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
- table.insert(t, str)
- end
- return t
- end
-
- local parts = split(name, "%s")
-
- local output_name = ""
- if #parts > 2 then
- for _, val in ipairs(parts) do
- output_name = string.format("%s%s", output_name, val:sub(1, 1))
- end
- elseif #parts == 2 then
- output_name = string.format("%s%s", parts[1]:sub(1, 1), parts[2])
- elseif #parts == 1 then
- output_name = parts[1]
- else
- -- parts is 0
- output_name = "<NoName>"
- end
- return string.format("@%s", output_name:lower())
-end
-
-_G.luasnip = {}
-_G.luasnip.vars = {
- username = function() return name_to_handle(read_git_config("user.name")) end,
- email = function() return read_git_config("user.email") end,
-}
-
---- Get the comment string {beg,end} table
----@param ctype integer 1 for `line`-comment and 2 for `block`-comment
----@return table comment_strings {begcstring, endcstring}
-local get_cstring = function(ctype)
- -- use the `Comments.nvim` API to fetch the comment string for the region (eq. '--%s' or '--[[%s]]' for `lua`)
- local cstring = calculate_comment_string({ ctype = ctype, range = utils.get_region() }) or vim.bo.commentstring
- -- as we want only the strings themselves and not strings ready for using `format` we want to split the left and right side
- local left, right = utils.unwrap_cstr(cstring)
- -- create a `{left, right}` table for it
- return { left, right }
-end
-
---- Options for marks to be used in a TODO comment
----@return table,table: The first table contains a node for the date, the second for the signature
-local marks = {
- signature = function() return t("(" .. _G.luasnip.vars:username() .. ")"), t("") end,
- date_signature = function() return t("<" .. os.date("%Y-%m-%d") .. ">"), t("(" .. _G.luasnip.vars:username() .. ")") end,
- date = function() return t("<" .. os.date("%Y-%m-%d") .. ">"), t("") end,
- empty = function() return t(""), t("") end,
-}
-
----@param alias string
----@param opts table
----@param mark_function function: This function should return two nodes
----@return table: Returns the comment node
-local todo_snippet_nodes = function(alias, opts, mark_function)
- local date_node, signature_node = mark_function()
- -- format them into the actual snippet
- local comment_node = fmta("<> <><>: <> <> <>", {
- f(function()
- return get_cstring(opts.ctype)[1] -- get <comment-string[1]>
- end),
- t(alias), -- [name-of-comment]
- signature_node,
- i(0), -- {comment-text}
- date_node,
- f(function()
- return get_cstring(opts.ctype)[2] -- get <comment-string[2]>
- end),
- })
- return comment_node
-end
-
---- Generate a TODO comment snippet with an automatic description and docstring
----@param context table merged with the generated context table `trig` must be specified
----@param alias string of aliases for the todo comment (ex.: {FIX, ISSUE, FIXIT, BUG})
----@param opts table merged with the snippet opts table
----@param mark_function function: The function used to get the marks
-local todo_snippet = function(context, alias, opts, mark_function)
- opts = opts or {}
- context = context or {}
- if not context.trig then
- return error("context doesn't include a `trig` key which is mandatory", 2) -- all we need from the context is the trigger
- end
- opts.ctype = opts.ctype or 1 -- comment type can be passed in the `opts` table, but if it is not, we have to ensure, it is defined
- local alias_string = alias -- `choice_node` documentation
- context.name = context.name or (alias_string .. " comment") -- generate the `name` of the snippet if not defined
- context.dscr = context.dscr or (alias_string .. " comment with a signature-mark") -- generate the `dscr` if not defined
- context.docstring = context.docstring or (" {1:" .. alias_string .. "}: {3} <{2:mark}>{0} ") -- generate the `docstring` if not defined
- local comment_node = todo_snippet_nodes(alias, opts, mark_function)
- return s(context, comment_node, opts) -- the final todo-snippet constructed from our parameters
-end
-
----@param context table: The luasnip context
----@param opts table: The luasnip opts table, needs to have a ctype set
----@param aliases string: All aliases for a name
----@param marks table: Possible marks to account in snipped generation
----@return table: All possible snippets build from the marks
-local process_marks = function(context, aliases, opts, marks)
- local output = {}
- for mark_name, mark_function in pairs(marks) do
- local contex_trig_local = context.trig
- context.trig = context.trig .. "-" .. mark_name
- output[#output + 1] = todo_snippet(context, aliases, opts, mark_function)
- context.trig = contex_trig_local
- end
- return output
-end
-
-local todo_snippet_specs = {
- { { trig = "todo" }, { "TODO" }, { ctype = 1 } },
- { { trig = "fix" }, { "FIXME", "ISSUE" }, { ctype = 1 } },
- { { trig = "hack" }, { "HACK" }, { ctype = 1 } },
- { { trig = "warn" }, { "WARNING" }, { ctype = 1 } },
- { { trig = "perf" }, { "PERFORMANCE", "OPTIMIZE" }, { ctype = 1 } },
- { { trig = "note" }, { "NOTE", "INFO" }, { ctype = 1 } },
-
- -- NOTE: Block commented todo-comments
- { { trig = "todob" }, { "TODO" }, { ctype = 2 } },
- { { trig = "fixb" }, { "FIXME", "ISSUE" }, { ctype = 2 } },
- { { trig = "hackb" }, { "HACK" }, { ctype = 2 } },
- { { trig = "warnb" }, { "WARNING" }, { ctype = 2 } },
- { { trig = "perfb" }, { "PERF", "PERFORMANCE", "OPTIM", "OPTIMIZE" }, { ctype = 2 } },
- { { trig = "noteb" }, { "NOTE", "INFO" }, { ctype = 2 } },
-}
-
-local todo_comment_snippets = {}
-for _, v in ipairs(todo_snippet_specs) do
- local snippets = process_marks(v[1], v[2][1], v[3], marks)
- for _, value in pairs(snippets) do
- table.insert(todo_comment_snippets, value)
- end
-end
-
-ls.add_snippets("all", todo_comment_snippets, { type = "snippets", key = "todo_comments" })
-
--- }}}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/html/html.lua b/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/html/html.lua
deleted file mode 100644
index 568c97ec..00000000
--- a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/html/html.lua
+++ /dev/null
@@ -1,102 +0,0 @@
-local get_visual = function(args, parent)
- if #parent.snippet.env.SELECT_RAW > 0 then
- return sn(nil, i(1, parent.snippet.env.SELECT_RAW))
- else
- return sn(nil, i(1))
- end
-end
-
-local line_begin = require("luasnip.extras.expand_conditions").line_begin
-
-return {
- -- HEADER
- s(
- {
- trig = "h([123456])",
- regTrig = true,
- wordTrig = false,
- snippetType = "autosnippet",
- },
- fmt(
- [[
- <h{}>{}</h{}>
- ]],
- {
- f(function(_, snip) return snip.captures[1] end),
- d(1, get_visual),
- f(function(_, snip) return snip.captures[1] end),
- }
- ),
- { condition = line_begin }
- ), -- PARAGRAPH
- s(
- { trig = "pp", snippetType = "autosnippet" },
- fmt(
- [[
- <p>{}</p>
- ]],
- { d(1, get_visual) }
- ),
- { condition = line_begin }
- ), -- UNORDERED LIST
- s(
- { trig = "itt", snippetType = "autosnippet" },
- fmt(
- [[
- <ul>
- <li>{}</li>{}
- </ul>
- ]],
- { i(1), i(0) }
- ),
- { condition = line_begin }
- ), -- LIST ITEM
- s(
- { trig = "ii", snippetType = "autosnippet" },
- fmt(
- [[
- <li>{}</li>
- ]],
- { d(1, get_visual) }
- ),
- { condition = line_begin }
- ),
- -- DOCUMENT TEMPLATE
- s(
- { trig = "base" },
- fmt(
- [[
- <!doctype HTML>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>{}</title>
- </head>
- <body>
- {}
- </body>
- </html>
- ]],
- { i(1, "FooBar"), i(0) }
- ),
- { condition = line_begin }
- ), -- ANCHOR TAG
- s(
- {
- trig = "([^%l])aa",
- regTrig = true,
- wordTrig = false,
- snippetType = "autosnippet",
- },
- fmt(
- [[
- {}<a href="{}">{}</a>
- ]],
- {
- f(function(_, snip) return snip.captures[1] end),
- i(1),
- d(2, get_visual),
- }
- )
- ),
-}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/delimiter.lua b/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/delimiter.lua
deleted file mode 100644
index ef453973..00000000
--- a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/delimiter.lua
+++ /dev/null
@@ -1,28 +0,0 @@
-local get_visual = function(args, parent)
- if #parent.snippet.env.SELECT_RAW > 0 then
- return sn(nil, i(1, parent.snippet.env.SELECT_RAW))
- else
- return sn(nil, i(1, ""))
- end
-end
-
-local translation_table = { ["("] = ")", ["{"] = "}", ["["] = "]" }
-
--- Return snippet tables
-return {
- -- LEFT/RIGHT ALL BRACES
- s(
- {
- trig = "([^%a])l([%(%[%{])",
- regTrig = true,
- wordTrig = false,
- snippetType = "autosnippet",
- },
- fmta("<>\\left<><>\\right<>", {
- f(function(_, snip) return snip.captures[1] end),
- f(function(_, snip) return snip.captures[2] end),
- d(1, get_visual),
- f(function(_, snip) return translation_table[snip.captures[2]] end),
- })
- ),
-}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/greek.lua b/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/greek.lua
deleted file mode 100644
index ebf4f9d7..00000000
--- a/modules/by-name/nv/nvim/plgs/luasnip/lua/snippets/tex/greek.lua
+++ /dev/null
@@ -1,37 +0,0 @@
--- Return snippet tables
-return {
- s({ trig = ";a", snippetType = "autosnippet" }, { t("\\alpha") }),
- s({ trig = ";b", snippetType = "autosnippet" }, { t("\\beta") }),
- s({ trig = ";g", snippetType = "autosnippet" }, { t("\\gamma") }),
- s({ trig = ";G", snippetType = "autosnippet" }, { t("\\Gamma") }),
- s({ trig = ";d", snippetType = "autosnippet" }, { t("\\delta") }),
- s({ trig = ";D", snippetType = "autosnippet" }, { t("\\Delta") }),
- s({ trig = ";e", snippetType = "autosnippet" }, { t("\\epsilon") }),
- s({ trig = ";ve", snippetType = "autosnippet" }, { t("\\varepsilon") }),
- s({ trig = ";z", snippetType = "autosnippet" }, { t("\\zeta") }),
- s({ trig = ";h", snippetType = "autosnippet" }, { t("\\eta") }),
- s({ trig = ";o", snippetType = "autosnippet" }, { t("\\theta") }),
- s({ trig = ";vo", snippetType = "autosnippet" }, { t("\\vartheta") }),
- s({ trig = ";O", snippetType = "autosnippet" }, { t("\\Theta") }),
- s({ trig = ";k", snippetType = "autosnippet" }, { t("\\kappa") }),
- s({ trig = ";l", snippetType = "autosnippet" }, { t("\\lambda") }),
- s({ trig = ";L", snippetType = "autosnippet" }, { t("\\Lambda") }),
- s({ trig = ";m", snippetType = "autosnippet" }, { t("\\mu") }),
- s({ trig = ";n", snippetType = "autosnippet" }, { t("\\nu") }),
- s({ trig = ";x", snippetType = "autosnippet" }, { t("\\xi") }),
- s({ trig = ";X", snippetType = "autosnippet" }, { t("\\Xi") }),
- s({ trig = ";i", snippetType = "autosnippet" }, { t("\\pi") }),
- s({ trig = ";I", snippetType = "autosnippet" }, { t("\\Pi") }),
- s({ trig = ";r", snippetType = "autosnippet" }, { t("\\rho") }),
- s({ trig = ";s", snippetType = "autosnippet" }, { t("\\sigma") }),
- s({ trig = ";S", snippetType = "autosnippet" }, { t("\\Sigma") }),
- s({ trig = ";t", snippetType = "autosnippet" }, { t("\\tau") }),
- s({ trig = ";f", snippetType = "autosnippet" }, { t("\\phi") }),
- s({ trig = ";vf", snippetType = "autosnippet" }, { t("\\varphi") }),
- s({ trig = ";F", snippetType = "autosnippet" }, { t("\\Phi") }),
- s({ trig = ";c", snippetType = "autosnippet" }, { t("\\chi") }),
- s({ trig = ";p", snippetType = "autosnippet" }, { t("\\psi") }),
- s({ trig = ";P", snippetType = "autosnippet" }, { t("\\Psi") }),
- s({ trig = ";w", snippetType = "autosnippet" }, { t("\\omega") }),
- s({ trig = ";W", snippetType = "autosnippet" }, { t("\\Omega") }),
-}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/snippets/all.lua b/modules/by-name/nv/nvim/plgs/luasnip/snippets/all.lua
new file mode 100644
index 00000000..8c30063e
--- /dev/null
+++ b/modules/by-name/nv/nvim/plgs/luasnip/snippets/all.lua
@@ -0,0 +1,322 @@
+-- nixos-config - My current NixOS configuration
+--
+-- Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+-- SPDX-License-Identifier: GPL-3.0-or-later
+--
+-- This file is part of my nixos-config.
+--
+-- You should have received a copy of the License along with this program.
+-- If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+local ls = require("luasnip")
+local fmt = require("luasnip.extras.fmt").fmt
+
+--- Get the comment string {begin,end} table
+---
+---@param comment_type integer 1 for `line`-comment and 2 for `block`-comment
+---@return table comment_strings {["begin"]=begin_comment_string, ["end"]=end_comment_string}
+local get_comment_string = function(comment_type)
+ local calculate_comment_string = require("Comment.ft").calculate
+ local utils = require("Comment.utils")
+
+ -- use the `Comments.nvim` API to fetch the comment string for the region (eq. '--%s' or '--[[%s]]' for `lua`)
+ local cstring = calculate_comment_string({ ctype = comment_type, range = utils.get_region() })
+
+ if cstring == nil then
+ -- TODO: Use `vim.bo.commentstring` <2025-05-02>
+
+ -- Use some useful default values.
+ return { ["begin"] = "#", ["end"] = "" }
+ end
+
+ -- as we want only the strings themselves and not strings ready for using `format` we want to split the left and right side
+ local left, right = utils.unwrap_cstr(cstring)
+
+ -- create a `{left, right}` table for it
+ return { ["begin"] = left, ["end"] = right }
+end
+
+--- Wraps a table of snippet nodes in two comment function nodes.
+---
+---@param comment_type integer 1 for `line`-comment and 2 for `block`-comment
+---@param nodes table The nodes that should be wrapped
+---@return table wrapped_nodes The now wrapped `nodes` table.
+local wrap_snippet_in_comments = function(comment_type, nodes)
+ local output = {}
+
+ table.insert(output, ls.function_node(function() return get_comment_string(comment_type)["begin"] end))
+
+ for _, v in ipairs(nodes) do
+ table.insert(output, v)
+ end
+
+ table.insert(output, ls.function_node(function() return get_comment_string(comment_type)["end"] end))
+
+ return output
+end
+
+-- auto_pairs {{{
+local get_visual = function(_, parent)
+ if #parent.snippet.env.SELECT_RAW > 0 then
+ return ls.snippet_node(nil, ls.insert_node(1, parent.snippet.env.SELECT_RAW))
+ else
+ return ls.snippet_node(nil, ls.insert_node(1, ""))
+ end
+end
+
+local function char_count_same(c1, c2)
+ local line = vim.api.nvim_get_current_line()
+ -- '%'-escape chars to force explicit match (gsub accepts patterns).
+ -- second return value is number of substitutions.
+ local _, ct1 = string.gsub(line, "%" .. c1, "")
+ local _, ct2 = string.gsub(line, "%" .. c2, "")
+ return ct1 == ct2
+end
+
+local function even_count(c, ...)
+ local line = vim.api.nvim_get_current_line()
+ local _, ct = string.gsub(line, c, "")
+ return ct % 2 == 0
+end
+
+-- This makes creation of pair-type snippets easier.
+local function pair(pair_begin, pair_end, file_types, condition_function)
+ -- FIXME(@Soispha): This only works if file_types == nil, otherwise the snippet does not expand.
+ -- It would be nice, if it would support both an empty array (`{}`) and nil <2023-08-27>
+ -- file_types = file_types or {};
+
+ return ls.snippet({
+ trig = pair_begin,
+ wordTrig = false,
+ snippetType = "autosnippet",
+ }, {
+ ls.text_node({ pair_begin }),
+ ls.dynamic_node(1, get_visual),
+ ls.text_node({ pair_end }),
+ }, {
+ condition = function()
+ local filetype_check = true
+
+ if file_types ~= nil then filetype_check = file_types[vim.bo.filetype] or false end
+
+ return (not condition_function(pair_begin, pair_end)) and filetype_check
+ end,
+ })
+end
+
+local auto_pairs = {
+ pair("(", ")", nil, char_count_same),
+ pair("{", "}", nil, char_count_same),
+ pair("[", "]", nil, char_count_same),
+ pair("<", ">", { ["rust"] = true, ["tex"] = true }, char_count_same),
+ pair("'", "'", nil, even_count),
+ pair("\"", "\"", nil, even_count),
+ pair("`", "`", nil, even_count),
+}
+
+ls.add_snippets("all", auto_pairs, { type = "snippets", key = "auto_pairs" })
+-- }}}
+
+-- todo_comments {{{
+local read_git_config = function(config_value)
+ local command = string.format("git config \"%s\"", config_value)
+
+ local handle = io.popen(command)
+ if handle == nil then return error(string.format("Failed to call `%s`.", command)) end
+
+ local result = handle:read("*a")
+ handle:close()
+
+ -- stripped = string.gsub(str, '%s+', '')
+ return string.gsub(result, "\n", "")
+end
+
+--- Create a @handle from a full name.
+---
+--- Example:
+--- “Benedikt Peetz” -> “@bpeetz”
+local handle_from_name = function(name)
+ -- from: https://stackoverflow.com/a/7615129
+ local split = function(inputstr, sep)
+ local t = {}
+ for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
+ table.insert(t, str)
+ end
+ return t
+ end
+
+ -- Split on spaces
+ local parts = split(name, "%s")
+
+ local output_name = ""
+
+ if #parts > 2 then
+ -- Only use the first chars.
+ --
+ -- Example:
+ -- “Richard Matthew Stallman” -> “rms”
+ for _, val in ipairs(parts) do
+ output_name = string.format("%s%s", output_name, val:sub(1, 1))
+ end
+ elseif #parts == 2 then
+ output_name = string.format("%s%s", parts[1]:sub(1, 1), parts[2])
+ elseif #parts == 1 then
+ output_name = parts[1]
+ elseif #parts == 0 then
+ output_name = "<NoName>"
+ end
+
+ return string.format("@%s", output_name:lower())
+end
+
+--- Generate a comment snippet
+---
+---@param trig string The trigger
+---@param name string name for the comment (ex.: {FIX, ISSUE, FIXIT, BUG})
+---@param comment_type integer The comment type.
+---@param mark_function function: The function used to get the marks
+local todo_snippet = function(trig, name, comment_type, mark_function)
+ assert(trig, "context doesn't include a `trig` key which is mandatory")
+ assert(comment_type == 1 or comment_type == 2)
+
+ local context = {}
+ context.name = name .. " comment"
+ context.trig = trig
+
+ local date_node, signature_node = mark_function()
+
+ local nodes = fmt(
+ "{} {}{}: {} {} {}",
+ wrap_snippet_in_comments(comment_type, {
+ ls.text_node(name),
+ signature_node,
+ ls.insert_node(1, "content"),
+ date_node,
+ })
+ )
+
+ return ls.snippet(context, nodes, { ctype = comment_type })
+end
+
+---@param trigger string: The luasnip trigger
+---@param comment_type integer: The luasnip comment type
+---@param name string: All aliases for a name
+---@return table: All possible snippets build from the marks
+local process_marks = function(trigger, name, comment_type)
+ local username = function() return handle_from_name(read_git_config("user.name")) end
+
+ local marks = {
+ signature = function() return ls.text_node("(" .. username() .. ")"), ls.text_node("") end,
+
+ date_signature = function()
+ return ls.text_node("<" .. os.date("%Y-%m-%d") .. ">"), ls.text_node("(" .. username() .. ")")
+ end,
+
+ date = function() return ls.text_node("<" .. os.date("%Y-%m-%d") .. ">"), ls.text_node("") end,
+
+ empty = function() return ls.text_node(""), ls.text_node("") end,
+ }
+
+ local output = {}
+ for mark_name, mark_function in pairs(marks) do
+ local trig = trigger .. "-" .. mark_name
+
+ output[#output + 1] = todo_snippet(trig, name, comment_type, mark_function)
+ end
+
+ return output
+end
+
+local todo_snippet_specs = {
+ { { trig = "todo" }, { "TODO" }, { ctype = 1 } },
+ { { trig = "fix" }, { "FIXME", "ISSUE" }, { ctype = 1 } },
+ { { trig = "hack" }, { "HACK" }, { ctype = 1 } },
+ { { trig = "warn" }, { "WARNING" }, { ctype = 1 } },
+ { { trig = "perf" }, { "PERFORMANCE", "OPTIMIZE" }, { ctype = 1 } },
+ { { trig = "note" }, { "NOTE", "INFO" }, { ctype = 1 } },
+
+ -- NOTE: Block commented todo-comments
+ { { trig = "todob" }, { "TODO" }, { ctype = 2 } },
+ { { trig = "fixb" }, { "FIXME", "ISSUE" }, { ctype = 2 } },
+ { { trig = "hackb" }, { "HACK" }, { ctype = 2 } },
+ { { trig = "warnb" }, { "WARNING" }, { ctype = 2 } },
+ { { trig = "perfb" }, { "PERF", "PERFORMANCE", "OPTIM", "OPTIMIZE" }, { ctype = 2 } },
+ { { trig = "noteb" }, { "NOTE", "INFO" }, { ctype = 2 } },
+}
+
+local todo_comment_snippets = {}
+for _, v in ipairs(todo_snippet_specs) do
+ local snippets = process_marks(v[1].trig, v[2][1], v[3].ctype)
+ for _, value in pairs(snippets) do
+ table.insert(todo_comment_snippets, value)
+ end
+end
+
+ls.add_snippets("all", todo_comment_snippets, { type = "snippets", key = "todo_comments" })
+-- }}}
+
+-- spdx snippets {{{
+local generate_spdx_snippet = function(comment_type, spdx_license_expr, trigger)
+ assert(trigger, "context doesn't include a `trig` key which is mandatory")
+ assert(comment_type == 1 or comment_type == 2)
+
+ local context = {}
+ context.name = trigger .. " spdx snippet expr"
+ context.trig = trigger
+
+ local nodes = {
+ fmt("{} SPDX-SnippetBegin {}", wrap_snippet_in_comments(comment_type, {})),
+
+ fmt(
+ "{} SPDX-SnippetCopyrightText: {} {} <{}> {}",
+ wrap_snippet_in_comments(comment_type, {
+ ls.insert_node(1, "year"),
+ ls.insert_node(2, "author"),
+ ls.insert_node(3, "email"),
+ })
+ ),
+
+ fmt(
+ "{} SPDX-License-Identifier: {} {}",
+ wrap_snippet_in_comments(comment_type, {
+ ls.text_node(spdx_license_expr),
+ })
+ ),
+
+ { ls.insert_node(4, "content") },
+
+ fmt("{} SPDX-SnippetEnd {}", wrap_snippet_in_comments(comment_type, {})),
+
+ { ls.insert_node(0) },
+ }
+
+ local newline_nodes = {}
+ for _, sub_nodes in ipairs(nodes) do
+ for _, node in ipairs(sub_nodes) do
+ table.insert(newline_nodes, node)
+ end
+
+ -- luasnip requires newlines to be encoded like this:
+ table.insert(newline_nodes, ls.text_node({ "", "" }))
+ end
+
+ return ls.snippet(context, newline_nodes, { ctype = comment_type })
+end
+
+local spdx = {
+ { trigger = "spdx-AGPL3+", license = "AGPL-3.0-or-later" },
+ { trigger = "spdx-GPL3+", license = "GPL-3.0-or-later" },
+ { trigger = "spdx-MIT", license = "MIT" },
+}
+
+local spdx_snippets = {}
+for _, value in ipairs(spdx) do
+ local snippet = generate_spdx_snippet(1, value.license, value.trigger)
+ table.insert(spdx_snippets, snippet)
+
+ snippet = generate_spdx_snippet(2, value.license, value.trigger .. "-block")
+ table.insert(spdx_snippets, snippet)
+end
+
+ls.add_snippets("all", spdx_snippets, { type = "snippets", key = "spdx_snippets" })
+-- }}}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/delimiter.lua b/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/delimiter.lua
new file mode 100644
index 00000000..02d285b7
--- /dev/null
+++ b/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/delimiter.lua
@@ -0,0 +1,41 @@
+-- nixos-config - My current NixOS configuration
+--
+-- Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+-- SPDX-License-Identifier: GPL-3.0-or-later
+--
+-- This file is part of my nixos-config.
+--
+-- You should have received a copy of the License along with this program.
+-- If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+local ls = require("luasnip")
+local fmt = require("luasnip.extras.fmt").fmt
+
+local get_visual = function(_, parent)
+ if #parent.snippet.env.SELECT_RAW > 0 then
+ return ls.snippet_node(nil, ls.insert_node(1, parent.snippet.env.SELECT_RAW))
+ else
+ return ls.snippet_node(nil, ls.insert_node(1, ""))
+ end
+end
+
+local translation_table = { ["("] = ")", ["{"] = "}", ["["] = "]" }
+
+-- Return snippet tables
+return {
+ -- LEFT/RIGHT ALL BRACES
+ ls.snippet(
+ {
+ trig = "([^%a])l([%(%[%{])",
+ regTrig = true,
+ wordTrig = false,
+ snippetType = "autosnippet",
+ },
+ fmt("{}\\left{}{}\\right{}", {
+ ls.function_node(function(_, snip) return snip.captures[1] end),
+ ls.function_node(function(_, snip) return snip.captures[2] end),
+ ls.dynamic_node(1, get_visual),
+ ls.function_node(function(_, snip) return translation_table[snip.captures[2]] end),
+ })
+ ),
+}
diff --git a/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/greek.lua b/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/greek.lua
new file mode 100644
index 00000000..a98f6e8c
--- /dev/null
+++ b/modules/by-name/nv/nvim/plgs/luasnip/snippets/tex/greek.lua
@@ -0,0 +1,49 @@
+-- nixos-config - My current NixOS configuration
+--
+-- Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+-- SPDX-License-Identifier: GPL-3.0-or-later
+--
+-- This file is part of my nixos-config.
+--
+-- You should have received a copy of the License along with this program.
+-- If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+local ls = require("luasnip")
+
+-- Return snippet tables
+return {
+ ls.snippet({ trig = ";a", snippetType = "autosnippet" }, { ls.text_node("\\alpha") }),
+ ls.snippet({ trig = ";b", snippetType = "autosnippet" }, { ls.text_node("\\beta") }),
+ ls.snippet({ trig = ";g", snippetType = "autosnippet" }, { ls.text_node("\\gamma") }),
+ ls.snippet({ trig = ";G", snippetType = "autosnippet" }, { ls.text_node("\\Gamma") }),
+ ls.snippet({ trig = ";d", snippetType = "autosnippet" }, { ls.text_node("\\delta") }),
+ ls.snippet({ trig = ";D", snippetType = "autosnippet" }, { ls.text_node("\\Delta") }),
+ ls.snippet({ trig = ";e", snippetType = "autosnippet" }, { ls.text_node("\\epsilon") }),
+ ls.snippet({ trig = ";ve", snippetType = "autosnippet" }, { ls.text_node("\\varepsilon") }),
+ ls.snippet({ trig = ";z", snippetType = "autosnippet" }, { ls.text_node("\\zeta") }),
+ ls.snippet({ trig = ";h", snippetType = "autosnippet" }, { ls.text_node("\\eta") }),
+ ls.snippet({ trig = ";o", snippetType = "autosnippet" }, { ls.text_node("\\theta") }),
+ ls.snippet({ trig = ";vo", snippetType = "autosnippet" }, { ls.text_node("\\vartheta") }),
+ ls.snippet({ trig = ";O", snippetType = "autosnippet" }, { ls.text_node("\\Theta") }),
+ ls.snippet({ trig = ";k", snippetType = "autosnippet" }, { ls.text_node("\\kappa") }),
+ ls.snippet({ trig = ";l", snippetType = "autosnippet" }, { ls.text_node("\\lambda") }),
+ ls.snippet({ trig = ";L", snippetType = "autosnippet" }, { ls.text_node("\\Lambda") }),
+ ls.snippet({ trig = ";m", snippetType = "autosnippet" }, { ls.text_node("\\mu") }),
+ ls.snippet({ trig = ";n", snippetType = "autosnippet" }, { ls.text_node("\\nu") }),
+ ls.snippet({ trig = ";x", snippetType = "autosnippet" }, { ls.text_node("\\xi") }),
+ ls.snippet({ trig = ";X", snippetType = "autosnippet" }, { ls.text_node("\\Xi") }),
+ ls.snippet({ trig = ";i", snippetType = "autosnippet" }, { ls.text_node("\\pi") }),
+ ls.snippet({ trig = ";I", snippetType = "autosnippet" }, { ls.text_node("\\Pi") }),
+ ls.snippet({ trig = ";r", snippetType = "autosnippet" }, { ls.text_node("\\rho") }),
+ ls.snippet({ trig = ";s", snippetType = "autosnippet" }, { ls.text_node("\\sigma") }),
+ ls.snippet({ trig = ";S", snippetType = "autosnippet" }, { ls.text_node("\\Sigma") }),
+ ls.snippet({ trig = ";t", snippetType = "autosnippet" }, { ls.text_node("\\tau") }),
+ ls.snippet({ trig = ";f", snippetType = "autosnippet" }, { ls.text_node("\\phi") }),
+ ls.snippet({ trig = ";vf", snippetType = "autosnippet" }, { ls.text_node("\\varphi") }),
+ ls.snippet({ trig = ";F", snippetType = "autosnippet" }, { ls.text_node("\\Phi") }),
+ ls.snippet({ trig = ";c", snippetType = "autosnippet" }, { ls.text_node("\\chi") }),
+ ls.snippet({ trig = ";p", snippetType = "autosnippet" }, { ls.text_node("\\psi") }),
+ ls.snippet({ trig = ";P", snippetType = "autosnippet" }, { ls.text_node("\\Psi") }),
+ ls.snippet({ trig = ";w", snippetType = "autosnippet" }, { ls.text_node("\\omega") }),
+ ls.snippet({ trig = ";W", snippetType = "autosnippet" }, { ls.text_node("\\Omega") }),
+}