nvim: make box snippet allow multiline input
This commit is contained in:
parent
f912c761c4
commit
8edc4d84a8
@ -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"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,18 @@ local function rep_node(args, snip)
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
-- see fbox snippet
|
|
||||||
local function box_line(args, snip, side)
|
--------------------------------
|
||||||
|
-- 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 = {
|
local shorthand = {
|
||||||
r = "round",
|
r = "round",
|
||||||
h = "heavy",
|
h = "heavy",
|
||||||
@ -36,7 +46,7 @@ local function box_line(args, snip, side)
|
|||||||
sharp = "┌─┐││└─┘",
|
sharp = "┌─┐││└─┘",
|
||||||
}
|
}
|
||||||
|
|
||||||
type = shorthand[snip.captures[1]] or "round"
|
type = shorthand[style_code] or "round"
|
||||||
|
|
||||||
-- array of characters (because lua strings are not c-strings 😔)
|
-- array of characters (because lua strings are not c-strings 😔)
|
||||||
-- nor does nvim lua have utf-8 support 😔
|
-- nor does nvim lua have utf-8 support 😔
|
||||||
@ -46,13 +56,10 @@ local function box_line(args, snip, side)
|
|||||||
table.insert(style, code)
|
table.insert(style, code)
|
||||||
end
|
end
|
||||||
|
|
||||||
local content = (args[1] or { "" })[1]
|
|
||||||
local content_len = string.len(content)
|
|
||||||
|
|
||||||
if side == "left" then
|
if side == "left" then
|
||||||
return style[4]
|
return style[4]
|
||||||
elseif side == "right" then
|
elseif side == "right" then
|
||||||
return style[5]
|
return string.rep(" ", (width or sz) - sz) .. style[5]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- offset into the style
|
-- offset into the style
|
||||||
@ -62,7 +69,15 @@ local function box_line(args, snip, side)
|
|||||||
if side == "bottom" then o = 6 end
|
if side == "bottom" then o = 6 end
|
||||||
|
|
||||||
-- +2 for padding
|
-- +2 for padding
|
||||||
return style[o] .. string.rep(style[o + 1], content_len + 2) .. style[o + 2]
|
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
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -140,20 +155,34 @@ return {
|
|||||||
})),
|
})),
|
||||||
|
|
||||||
s(
|
s(
|
||||||
{ trig = "fbox(%a?)", regTrig = true, desc = { "draw a unicode box around some text.", "use fboxs for sharp corners (round default), fboxh for heavy" } },
|
{ 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([[
|
fmt([[
|
||||||
{topline}
|
{surr1}
|
||||||
{lside} {content} {rside}
|
{content}
|
||||||
{bottomline}
|
{surr2}
|
||||||
]], {
|
]],
|
||||||
content = i(1, "contents"),
|
{
|
||||||
topline = f(box_line, { 1 }, { user_args = { "top" } }),
|
content = i(1, "contents", {
|
||||||
bottomline = f(box_line, { 1 }, { user_args = { "bottom" } }),
|
node_callbacks = {
|
||||||
lside = f(box_line, { 1 }, { user_args = { "left" } }),
|
[events.leave] = function(node)
|
||||||
rside = f(box_line, { 1 }, { user_args = { "right" } }),
|
local t = node:get_text()
|
||||||
})),
|
local width = 0
|
||||||
|
for _, line in ipairs(t) do
|
||||||
s({trig="trigger", desc="human-readable description"}, d(1, f(function (args)
|
width = math.max(width, string.len(line))
|
||||||
return "durr" .. args[1][1] .. "durr"
|
end
|
||||||
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" } }),
|
||||||
|
})),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user