Add sections snippet

This commit is contained in:
a.shalimov 2026-09-21 17:20:13 +03:00
parent 0abf2af78b
commit 2595c635b7
3 changed files with 78 additions and 32 deletions

View file

@ -1,21 +1,28 @@
local ls = require "luasnip" local ls = require "luasnip"
local s = ls.snippet local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node local f = ls.function_node
local util = require "custom.snippets.util"
ls.add_snippets("all", { ls.add_snippets("all", {
s("icd", { s("icd", {
f(function() f(util.today)
return os.date("%Y-%m-%d")
end)
}), }),
s("ict", { s("ict", {
f(function() f(util.timestamp)
return os.date("| %Y-%m-%d %H:%M | ")
end)
}), }),
s("шсе", { s("шсе", {
f(function() f(util.timestamp)
return os.date("| %Y-%m-%d %H:%M | ") }),
end) s({ trig = "csection", dscr = "# centered section header #" }, {
}) t "#",
f(util.pad_left(100, 1), { 1 }),
i(1, "Some topic"),
f(util.pad_right(100, 1), { 1 }),
t "#",
t { "", "" },
i(0),
}),
}) })

View file

@ -13,6 +13,8 @@ local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta local fmta = require("luasnip.extras.fmt").fmta
local rep = require("luasnip.extras").rep local rep = require("luasnip.extras").rep
local util = require "custom.snippets.util"
ls.add_snippets("python", { ls.add_snippets("python", {
s( s(
{ trig = "ctable", dscr = "Create method or function with test stub" }, { trig = "ctable", dscr = "Create method or function with test stub" },
@ -96,26 +98,14 @@ ls.add_snippets("python", {
{ i(1), rep(1), i(2), i(0) } { i(1), rep(1), i(2), i(0) }
) )
), ),
s({ trig = "rupysection", dscr = "Secion snippet" }, s({ trig = "pysection", dscr = "Section header" }, {
fmt( t { string.rep("#", 100), "" },
[[ t "#",
##################################### Секция ############################################# f(util.pad_left(100, 1), { 1 }),
# Тема: {} i(1, "Section topic"),
########################################################################################## f(util.pad_right(100, 1), { 1 }),
{} t "#",
]], t { "", string.rep("#", 100), "" },
{ i(1), i(0) } i(0),
) }),
),
s({ trig = "pysection", dscr = "Secion snippet" },
fmt(
[[
##################################### Section ############################################
# Section topic: {}
##########################################################################################
{}
]],
{ i(1), i(0) }
)
),
}) })

View file

@ -0,0 +1,49 @@
-- Shared helper functions for use inside snippet function_node()/dynamic_node()
local M = {}
function M.today()
return os.date("%Y-%m-%d")
end
function M.timestamp()
return os.date("| %Y-%m-%d %H:%M | ")
end
function M.clipboard_or(default)
return function()
local clip = vim.fn.getreg "+"
return clip ~= "" and clip or default
end
end
-- Space padding to center `text` between fixed-width markers (e.g. "###")
-- so the whole line (markers + padding + text) is exactly `width` chars.
local function side_pad(text, width, marker_len)
local interior = width - marker_len * 2
local pad = math.max(interior - #text, 0)
local left = math.floor(pad / 2)
local right = pad - left
return left, right
end
-- Use as f(util.pad_left(100, 3), { N }) where N is the node index holding
-- the text to center, placed right after the opening marker.
function M.pad_left(width, marker_len)
width, marker_len = width or 100, marker_len or 3
return function(args)
local left = side_pad(args[1][1] or "", width, marker_len)
return string.rep(" ", left)
end
end
-- Companion to pad_left(), placed right before the closing marker.
function M.pad_right(width, marker_len)
width, marker_len = width or 100, marker_len or 3
return function(args)
local _, right = side_pad(args[1][1] or "", width, marker_len)
return string.rep(" ", right)
end
end
return M