dotfiles/.config/nvim/lua/snippets/helpers.lua

41 lines
854 B
Lua
Raw Normal View History

2024-02-24 12:28:09 -08:00
local ls = require("luasnip")
local sn = ls.snippet_node
local i = ls.insert_node
local M = {}
2024-02-24 12:28:09 -08:00
M.fill = function(_, parent, args)
local title = ""
local chars = {}
2024-02-24 12:28:09 -08:00
if #parent.snippet.env.LS_SELECT_RAW > 0 then
2024-02-27 20:58:28 -08:00
for _, ele in ipairs(parent.snippet.env.LS_SELECT_RAW) do
title = title .. ele .. " "
end
2024-02-24 12:28:09 -08:00
end
for str in string.gmatch(args, "([^%%]+)") do
table.insert(chars, str)
end
2024-02-24 12:28:09 -08:00
local snip = chars[1] .. " " .. title
local _, c = unpack(vim.api.nvim_win_get_cursor(0))
while #snip < (vim.bo.tw - c - 1) do
snip = snip .. chars[2]
end
return snip
end
2024-02-24 12:28:09 -08:00
M.get_visual = function(_, parent)
if #parent.snippet.env.LS_SELECT_RAW > 0 then
return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW))
else -- If LS_SELECT_RAW is empty, return a blank insert node
return sn(nil, i(1))
end
end
return M