Skip to content

Commit 5d9a076

Browse files
committed
feat: add postfix snippets, also undo the weird error to init.lua
1 parent 43c8102 commit 5d9a076

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

lua/luasnip-latex-snippets/init.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
{
2-
[""] = {},
3-
all = {}
4-
}
1+
snippet_path = debug.getinfo(1).source:sub(2):gsub("init.lua", "luasnippets")
2+
require("luasnip.loaders.from_lua").lazy_load({ paths = snippet_path })

lua/luasnip-latex-snippets/luasnippets/tex/math-commands.lua

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local tex = require("luasnip-latex-snippets.luasnippets.tex.utils.conditions")
3636
local auto_backslash_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").auto_backslash_snippet
3737
local symbol_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").symbol_snippet
3838
local single_command_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").single_command_snippet
39+
local postfix_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").postfix_snippet
3940

4041
M = {
4142
-- superscripts
@@ -339,4 +340,91 @@ for k, v in pairs(single_command_math_specs) do
339340
end
340341
vim.list_extend(M, single_command_math_snippets)
341342

343+
local postfix_math_specs = {
344+
mbb = {
345+
context = {
346+
name = "mathbb",
347+
dscr = "math blackboard bold",
348+
},
349+
command = {
350+
pre = [[\mathbb{]],
351+
post = [[}]],
352+
}
353+
},
354+
mcal = {
355+
context = {
356+
name = "mathcal",
357+
dscr = "math calligraphic",
358+
},
359+
command = {
360+
pre = [[\mathcal{]],
361+
post = [[}]],
362+
}
363+
},
364+
mscr = {
365+
context = {
366+
name = "mathscr",
367+
dscr = "math script",
368+
},
369+
command = {
370+
pre = [[\mathscr{]],
371+
post = [[}]],
372+
},
373+
},
374+
mfr = {
375+
context = {
376+
name = "mathfrak",
377+
dscr = "mathfrak",
378+
},
379+
command = {
380+
pre = [[\mathfrak{]],
381+
post = [[}]],
382+
},
383+
},
384+
hat = {
385+
context = {
386+
name = "hat",
387+
dscr = "hat",
388+
},
389+
command = {
390+
pre = [[\hat{]],
391+
post = [[}]],
392+
}
393+
},
394+
bar = {
395+
context = {
396+
name = "bar",
397+
dscr = "bar (overline)",
398+
},
399+
command = {
400+
pre = [[\overline{]],
401+
post = [[}]]
402+
}
403+
},
404+
tld = {
405+
context = {
406+
name = "tilde",
407+
priority = 500,
408+
dscr = "tilde",
409+
},
410+
command = {
411+
pre = [[\tilde{]],
412+
post = [[}]]
413+
}
414+
}
415+
}
416+
417+
local postfix_math_snippets = {}
418+
for k, v in pairs(postfix_math_specs) do
419+
table.insert(
420+
postfix_math_snippets,
421+
postfix_snippet(
422+
vim.tbl_deep_extend("keep", { trig = k, snippetType = "autosnippet" }, v.context),
423+
v.command,
424+
{ condition = tex.in_math }
425+
)
426+
)
427+
end
428+
vim.list_extend(M, postfix_math_snippets)
429+
342430
return M

lua/luasnip-latex-snippets/luasnippets/tex/utils/scaffolding.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ local autosnippet = ls.extend_decorator.apply(s, { snippetType = "autosnippet" }
3232

3333
M = {}
3434

35+
-- postfix helper function - generates dynamic node
36+
local generate_postfix_dynamicnode = function(_, parent, _, user_arg1, user_arg2)
37+
local capture = parent.snippet.env.POSTFIX_MATCH
38+
if #capture > 0 then
39+
return sn(nil, fmta([[
40+
<><><><>
41+
]],
42+
{t(user_arg1), t(capture), t(user_arg2), i(0)}))
43+
else
44+
local visual_placeholder = parent.snippet.env.SELECT_RAW
45+
return sn(nil, fmta([[
46+
<><><><>
47+
]],
48+
{t(user_arg1), i(1, visual_placeholder), t(user_arg2), i(0)}))
49+
end
50+
end
3551

3652
-- visual util to add insert node - thanks ejmastnak!
3753
M.get_visual = function(args, parent)
@@ -106,4 +122,18 @@ M.single_command_snippet = function(context, command, opts, ext)
106122
)
107123
end
108124

125+
M.postfix_snippet = function (context, command, opts)
126+
opts = opts or {}
127+
if not context.trig then
128+
error("context doesn't include a `trig` key which is mandatory", 2)
129+
end
130+
if not context.trig then
131+
error("context doesn't include a `trig` key which is mandatory", 2)
132+
end
133+
context.dscr = context.dscr
134+
context.name = context.name or context.dscr
135+
context.docstring = command.pre .. [[(POSTFIX_MATCH|VISUAL|<1>)]] .. command.post
136+
return postfix(context, {d(1, generate_postfix_dynamicnode, {}, { user_args = {command.pre, command.post} })}, opts)
137+
end
138+
109139
return M

0 commit comments

Comments
 (0)