Add sections snippet
This commit is contained in:
parent
0abf2af78b
commit
2595c635b7
3 changed files with 78 additions and 32 deletions
|
|
@ -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),
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
49
lua/custom/snippets/util.lua
Normal file
49
lua/custom/snippets/util.lua
Normal 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
|
||||
Loading…
Reference in a new issue