Migrate from LuaSnip #2218
-
Contributing guidelines
Module(s)mini.keymap, mini.snippets QuestionI am trying to migrate my current setup to mini.nvim. I often write documents in LaTeX format and rely heavily on LuaSnip to perform one of the following tasks:
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local d = ls.dynamic_node
local r = ls.restore_node
return {
s({ trig = ";a", snippetType = "autosnippet" }, {
t "\\alpha",
})
}
Here is an exemple: local generate_matrix = function(args, snip)
local rows = tonumber(snip.captures[2])
local cols = tonumber(snip.captures[3])
local nodes = {}
local ins_indx = 1
for j = 1, rows do
table.insert(nodes, r(ins_indx, tostring(j) .. "x1", i(1)))
ins_indx = ins_indx + 1
for k = 2, cols do
table.insert(nodes, t " & ")
table.insert(nodes, r(ins_indx, tostring(j) .. "x" .. tostring(k), i(1)))
ins_indx = ins_indx + 1
end
table.insert(nodes, t { "\\\\", "" })
end
-- fix last node.
nodes[#nodes] = t "\\\\"
return sn(nil, nodes)
end
return {
s(
{
trig = "([bBpvV])mat(%d+)x(%d+)([ar])",
name = "[bBpvV]matrix",
dscr = "matrices",
regTrig = true,
hidden = false,
},
fmta(
[[
\begin{<>}<>
<>
\end{<>}]],
{
f(function(_, snip)
return snip.captures[1] .. "matrix"
end),
f(function(_, snip)
if snip.captures[4] == "a" then
local out = string.rep("c", tonumber(snip.captures[3]) - 1)
return "[" .. out .. "|c]"
end
return ""
end),
d(1, generate_matrix),
f(function(_, snip)
return snip.captures[1] .. "matrix"
end),
}
),
-- { condition = in_math, show_condition = in_math }
{ show_condition = in_math }
),
}Is it possible to emulate such behavior with mini.snippets (with maybe the addition of other mini plugins) ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
'mini.snippets' is not really designed to have autoexpand. Mostly because I think this is not really suitable for snippets and is somewhat of an abbreviations ( However, just out of curiosity, I tried to do this in 'mini.snippets'. It is somewhat possible, but kind of not straightforward. Here is an example of full working setup:
Now opening a file named 'test.tex' and typing |
Beta Was this translation helpful? Give feedback.
-
It is also mostly possible with 'mini.snippets', but with a little bit different fundamental approach. The 'mini.snippets' module only supports LSP syntax for snippets. But it supports dynamic [computation/preparation of snippets]. It is done on every expand and can leverage different types of snippets supplied in What can be suitable here is a single function loader that is added only for dedicated LaTeX filetypes (like 'plaintex'). Here is a basic template that is meant to be put in '~/.config/nvim/after/ftplugin/plaintex.lua' file: -- Create LaTeX only dynamic snippets
local dynamic_matrix_snippet = function()
-- Get text to cursor's left and check if it matches the necessary pattern
local line = vim.api.nvim_get_current_line()
local col = vim.api.nvim_win_get_cursor(0)[2]
local text_before_cursor = line:sub(1, col):match('%S*$')
local prefix, rows, cols = text_before_cursor:match('([bBpvV]mat(%d+)x(%d+)[ar])$')
if prefix == nil then return {} end
-- Compute the body of LSP snippet based on the data from matched pattern
-- TODO: Adjust this to return text for the actual matrix body.
-- This is just a placeholder.
local body = string.rep('a', rows) .. string.rep('b', cols)
return { { prefix = prefix, body = body, desc = 'Dynamic matrix' } }
end
vim.b.minisnippets_config = { snippets = { dynamic_matrix_snippet } }Now typing something like |
Beta Was this translation helpful? Give feedback.
It is also mostly possible with 'mini.snippets', but with a little bit different fundamental approach.
The 'mini.snippets' module only supports LSP syntax for snippets. But it supports dynamic [computation/preparation of snippets]. It is done on every expand and can leverage different types of snippets supplied in
config.snippets.What can be suitable here is a single function loader that is added only for dedicated LaTeX filetyp…