From ced11224fe6dbce36904b88b98eee75067ae2235 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 31 Oct 2024 16:10:22 +0100 Subject: feat(modules/legacy/nvim/plgs/luasnippets): Use the git config values Using hard-coded values could result in desyncs. --- .../conf/nvim/plgs/luasnip/lua/snippets/all.lua | 98 ++++++++++++++++------ 1 file changed, 72 insertions(+), 26 deletions(-) (limited to 'modules/home.legacy/conf/nvim/plgs') diff --git a/modules/home.legacy/conf/nvim/plgs/luasnip/lua/snippets/all.lua b/modules/home.legacy/conf/nvim/plgs/luasnip/lua/snippets/all.lua index c3f75058..f6cb09b0 100644 --- a/modules/home.legacy/conf/nvim/plgs/luasnip/lua/snippets/all.lua +++ b/modules/home.legacy/conf/nvim/plgs/luasnip/lua/snippets/all.lua @@ -60,6 +60,52 @@ ls.add_snippets("all", auto_pairs, { type = "snippets", key = "auto_pairs" }) 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 = "" + 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} @@ -71,20 +117,15 @@ local get_cstring = function(ctype) -- create a `{left, right}` table for it return { left, right } end -_G.luasnip = {} -_G.luasnip.vars = { - username = "@soispha", - email = "soispha@vhack.eu", -} --- 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("") + return t("(" .. _G.luasnip.vars:username() .. ")"), t("") end, date_signature = function() - return t("<" .. os.date("%Y-%m-%d") .. ">"), t("(" .. _G.luasnip.vars.username .. ")") + return t("<" .. os.date("%Y-%m-%d") .. ">"), t("(" .. _G.luasnip.vars:username() .. ")") end, date = function() return t("<" .. os.date("%Y-%m-%d") .. ">"), t("") @@ -105,9 +146,9 @@ local todo_snippet_nodes = function(alias, opts, mark_function) f(function() return get_cstring(opts.ctype)[1] -- get end), - t(alias), -- [name-of-comment] + t(alias), -- [name-of-comment] signature_node, - i(0), -- {comment-text} + i(0), -- {comment-text} date_node, f(function() return get_cstring(opts.ctype)[2] -- get @@ -127,13 +168,18 @@ local todo_snippet = function(context, alias, opts, mark_function) 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 + 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 + return s(context, comment_node, opts) -- the final todo-snippet constructed from our parameters end ---@param context table: The luasnip context @@ -153,20 +199,20 @@ local process_marks = function(context, aliases, opts, marks) 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 } }, + { { 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 = "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 } }, + { { trig = "noteb" }, { "NOTE", "INFO" }, { ctype = 2 } }, } local todo_comment_snippets = {} -- cgit 1.4.1