From 5fb723ee298ad260b7aa718d85f38be734e62f7a Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Wed, 3 Apr 2024 16:03:15 -0400 Subject: [PATCH] nvim: add more snippets - shrug - section header comment (i like this one) --- src/.config/nvim/lua/coding.lua | 6 ++- src/.config/nvim/snippets/all.lua | 80 +++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/.config/nvim/lua/coding.lua b/src/.config/nvim/lua/coding.lua index bae3221..698511a 100644 --- a/src/.config/nvim/lua/coding.lua +++ b/src/.config/nvim/lua/coding.lua @@ -137,7 +137,11 @@ local servers = { library = { vim.env.VIMRUNTIME, }, - } + }, + diagnostics = { + -- get it to stop complaining about luasnip + globals = {'s', 'f', 't', "fmt", "c", "sn", "i", "rep", "d"}, + }, } } }, diff --git a/src/.config/nvim/snippets/all.lua b/src/.config/nvim/snippets/all.lua index bb4fece..777c58d 100644 --- a/src/.config/nvim/snippets/all.lua +++ b/src/.config/nvim/snippets/all.lua @@ -1,12 +1,38 @@ +-------------------------------- +-------------------------------- +-- utility functions +-------------------------------- +-------------------------------- + +-- if nil or empty, use placeholder +local function get_str(str, placeholder) + if not str or str == "" then + return placeholder + else + return str + end +end + +-- repeat i() node some amount of times +local function rep_node(args, snip) + local ret = {} + local line = string.rep(args[1][1], get_str(snip.captures[2], 16)) + for _ = 1, get_str(snip.captures[1], 1) do + table.insert(ret, line) + end + return ret +end + return { - -------------------------------- - -------------------------------- + -------------------------------- + -------------------------------- -- date and timekeeping snippets - -------------------------------- - -------------------------------- + -------------------------------- + -------------------------------- s({ trig = "today", desc = "YYYY-MM-DD date of today" }, f(function() return os.date("%Y-%m-%d") end)), + s({ trig = "yesterday", desc = "YYYY-MM-DD date of yesterday" }, f(function() local t = os.date("*t") return os.date("%Y-%m-%d", os.time { year = t.year, month = t.month, day = t.day - 1 }) @@ -16,6 +42,7 @@ return { local t = os.date("*t") return tostring(os.time { year = t.year, month = t.month, day = t.day, hour = 0 }) end)), + s( { trig = "datestamp (%d-)-(%d-)-(%d-)", @@ -25,4 +52,49 @@ return { }, f(function(_, snip) return tostring(os.time { year = snip.captures[1], month = snip.captures[2], day = snip.captures[3], hour = 0 }) end)), + + -------------------------------- + -------------------------------- + -- miscellaneous snippets + -------------------------------- + -------------------------------- + s({ trig = "shrug", desc = "shrug emoticon (not escaped)" }, t("¯\\_(ツ)_/¯")), + + s( + { + trig = "(%d*)segm(%d-)", + regTrig = true, + desc = { + "section/segment header title comment.", "", + "number in front is height (default 1), second number is width.", + "to use this, type the comment symbol (e.g. # or //) then TAB, then the section title.", + "see also: https://www.pathsensitive.com/2023/12/should-you-split-that-file.html", + } + }, fmt([[ + {surr1} + {comm} {cont} + {surr2} + + ]], { + surr1 = f(rep_node, { 1 }), + comm = d(1, function() + local ft = vim.bo.filetype + + local ft_table = { + c = "//", + cpp = "//", + rust = "//", + lua = "--", + python = "##", + } + + local comm = ft_table[ft] + if not comm then + return sn(nil, { i(1) }) + end + return sn(nil, { t(comm) }) + end), + cont = i(2), + surr2 = f(rep_node, { 1 }) + })), }