Compare commits

...

3 Commits

Author SHA1 Message Date
35ca0f7b7a
nvim: clean up snippets
it looks readable now
2024-04-04 16:39:45 -04:00
8edc4d84a8
nvim: make box snippet allow multiline input 2024-04-04 16:30:05 -04:00
f912c761c4
nvim: box snippet 2024-04-03 21:57:22 -04:00
3 changed files with 129 additions and 10 deletions

View File

@ -140,7 +140,7 @@ local servers = {
}, },
diagnostics = { diagnostics = {
-- get it to stop complaining about luasnip -- get it to stop complaining about luasnip
globals = {'s', 'f', 't', "fmt", "c", "sn", "i", "rep", "d", "k"}, globals = {'s', 'f', 't', "fmt", "c", "sn", "i", "rep", "d", "k", "events"},
}, },
} }
} }

View File

@ -23,22 +23,91 @@ local function rep_node(args, snip)
return ret return ret
end end
--------------------------------
-- fbox snippet helper functions
--------------------------------
---format box sides
---@param style_code string?
---@param side string
---@param sz integer
---@param width integer?
---@return string
local function box_line(style_code, side, sz, width)
local shorthand = {
r = "round",
h = "heavy",
s = "sharp",
}
local styles = {
round = "╭─╮││╰─╯",
heavy = "┏━┓┃┃┗━┛",
sharp = "┌─┐││└─┘",
}
type = shorthand[style_code] or "round"
-- array of characters (because lua strings are not c-strings 😔)
-- nor does nvim lua have utf-8 support 😔
-- https://stackoverflow.com/a/27941567
local style = {}
for code in styles[type]:gmatch("[%z\1-\127\194-\244][\128-\191]*") do
table.insert(style, code)
end
if side == "left" then
return style[4]
elseif side == "right" then
return string.rep(" ", (width or sz) - sz) .. style[5]
end
-- offset into the style
local o = 1
if side == "top" then o = 1 end
if side == "bottom" then o = 6 end
-- +2 for padding
return style[o] .. string.rep(style[o + 1], sz) .. style[o + 2]
end
local function box_fct(args, snip, side)
local content = args[1] or { "" }
local content_width = 0
for _, line in ipairs(content) do
content_width = math.max(content_width, string.len(line))
end
return box_line(snip.captures[1], side, content_width - 6)
end
return { return {
-------------------------------- --------------------------------
-------------------------------- --------------------------------
-- date and timekeeping snippets -- date and timekeeping snippets
-------------------------------- --------------------------------
-------------------------------- --------------------------------
s({ trig = "today", desc = "YYYY-MM-DD date of today" }, f(function() s({
trig = "today",
name = "Today date format",
desc = "YYYY-MM-DD date of today"
}, f(function()
return os.date("%Y-%m-%d") return os.date("%Y-%m-%d")
end)), end)),
s({ trig = "yesterday", desc = "YYYY-MM-DD date of yesterday" }, f(function() s({
trig = "yesterday",
name = "Yesterday date format",
desc = "YYYY-MM-DD date of yesterday"
}, f(function()
local t = os.date("*t") local t = os.date("*t")
return os.date("%Y-%m-%d", os.time { year = t.year, month = t.month, day = t.day - 1 }) return os.date("%Y-%m-%d", os.time { year = t.year, month = t.month, day = t.day - 1 })
end)), end)),
s({ trig = "timestamp", desc = "Unix day timestamp (locked to midnight)" }, f(function() s({
trig = "timestamp",
name = "Unix day timestamp",
desc = "Unix time for today (locked to midnight)"
}, f(function()
local t = os.date("*t") local t = os.date("*t")
return tostring(os.time { year = t.year, month = t.month, day = t.day, hour = 0 }) return tostring(os.time { year = t.year, month = t.month, day = t.day, hour = 0 })
end)), end)),
@ -46,9 +115,10 @@ return {
s( s(
{ {
trig = "datestamp (%d-)-(%d-)-(%d-)", trig = "datestamp (%d-)-(%d-)-(%d-)",
name = "Unix timestamp based on date",
regTrig = true, regTrig = true,
desc = desc =
"YYYY-MM-DD to Unix day timestamp (locked to midnight)" "YYYY-MM-DD to Unix day timestamp conversion (locked to midnight)"
}, f(function(_, snip) }, f(function(_, snip)
return tostring(os.time { year = snip.captures[1], month = snip.captures[2], day = snip.captures[3], hour = 0 }) return tostring(os.time { year = snip.captures[1], month = snip.captures[2], day = snip.captures[3], hour = 0 })
end)), end)),
@ -58,14 +128,17 @@ return {
-- miscellaneous snippets -- miscellaneous snippets
-------------------------------- --------------------------------
-------------------------------- --------------------------------
s({ trig = "shrug", desc = "shrug emoticon (not escaped)" }, t("¯\\_(ツ)_/¯")), s({
trig = "shrug",
desc = "shrug emoticon (not escaped)"
}, t("¯\\_(ツ)_/¯")),
s( s(
{ {
trig = "(%d*)segm(%d-)", trig = "(%d*)segm(%d-)",
regTrig = true, regTrig = true,
name = "section/segment header title comment.",
desc = { desc = {
"section/segment header title comment.", "",
"number in front is height (default 1), second number is width.", "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.", "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", "see also: https://www.pathsensitive.com/2023/12/should-you-split-that-file.html",
@ -96,4 +169,41 @@ return {
cont = i(2), cont = i(2),
surr2 = f(rep_node, { 1 }) surr2 = f(rep_node, { 1 })
})), })),
s(
{
trig = "fbox(%a?)",
regTrig = true,
name = "format box",
desc = { "draw a unicode box around some text.", "use fboxs for sharp corners (round default), fboxh for heavy" }
},
fmt([[
{surr1}
{content}
{surr2}
]],
{
content = i(1, "contents", {
node_callbacks = {
[events.leave] = function(node)
local t = node:get_text()
local width = 0
for _, line in ipairs(t) do
width = math.max(width, string.len(line))
end
local new_t = {}
for _, l in ipairs(t) do
local sz = string.len(l)
local style_code = node.parent.captures[1]
table.insert(new_t,
box_line(style_code, "left", sz, width) ..
" " .. l .. " " .. box_line(style_code, "right", sz, width))
end
node:set_text(new_t)
end
}
}),
surr1 = f(box_fct, { 1 }, { user_args = { "top" } }),
surr2 = f(box_fct, { 1 }, { user_args = { "bottom" } }),
})),
} }

View File

@ -1,5 +1,14 @@
return { return {
s({ trig = "snipf", desc = "meta snippet (snippet for making snippets)" }, fmt([[ s({
s({{trig="{}", desc="{}"}}, {}), trig = "snipf",
]], { i(1, "trigger"), i(2, "human-readable description"), i(3, "<body nodes>") })) desc = "meta snippet (snippet for making snippets)"
}, fmt([[
s({{
trig="{}",
name="{}",
desc="{}",
}},
{}
),
]], { i(1, "trigger"), i(2, "human-readable name"), i(3, "human-readable description"), i(4, "body") }))
} }