diff --git a/lua/custom/snippets/all.lua b/lua/custom/snippets/all.lua index 97f5a05..ee2f699 100644 --- a/lua/custom/snippets/all.lua +++ b/lua/custom/snippets/all.lua @@ -1,21 +1,28 @@ local ls = require "luasnip" local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node local f = ls.function_node +local util = require "custom.snippets.util" + ls.add_snippets("all", { s("icd", { - f(function() - return os.date("%Y-%m-%d") - end) + f(util.today) }), s("ict", { - f(function() - return os.date("| %Y-%m-%d %H:%M | ") - end) + f(util.timestamp) }), s("шсе", { - f(function() - return os.date("| %Y-%m-%d %H:%M | ") - end) - }) + f(util.timestamp) + }), + 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), + }), }) diff --git a/lua/custom/snippets/python.lua b/lua/custom/snippets/python.lua index e3bed3b..ea404f6 100644 --- a/lua/custom/snippets/python.lua +++ b/lua/custom/snippets/python.lua @@ -13,6 +13,8 @@ local fmt = require("luasnip.extras.fmt").fmt local fmta = require("luasnip.extras.fmt").fmta local rep = require("luasnip.extras").rep +local util = require "custom.snippets.util" + ls.add_snippets("python", { s( { 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) } ) ), - s({ trig = "rupysection", dscr = "Secion snippet" }, - fmt( - [[ - ##################################### Секция ############################################# - # Тема: {} - ########################################################################################## - {} - ]], - { i(1), i(0) } - ) - ), - s({ trig = "pysection", dscr = "Secion snippet" }, - fmt( - [[ - ##################################### Section ############################################ - # Section topic: {} - ########################################################################################## - {} - ]], - { i(1), i(0) } - ) - ), + s({ trig = "pysection", dscr = "Section header" }, { + 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(0), + }), }) diff --git a/lua/custom/snippets/util.lua b/lua/custom/snippets/util.lua new file mode 100644 index 0000000..c053a60 --- /dev/null +++ b/lua/custom/snippets/util.lua @@ -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