From 4884f144d02d3288ac70d7c82fd91df1d743e968 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Wed, 3 Apr 2024 19:21:48 -0400 Subject: [PATCH] nvim: migrated typst snippets to luasnip this was the most absolutely painful experience ever why did i do this it was already working with ultisnips it's literally 20x more convoluted with luasnip than ultisnips for no benefit at all https://xkcd.com/1205/ --- src/.config/nvim/lua/coding.lua | 2 +- src/.config/nvim/snippets/all.lua | 1 - src/.config/nvim/snippets/lua.lua | 2 +- src/.config/nvim/snippets/typst.lua | 101 ++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/.config/nvim/snippets/typst.lua diff --git a/src/.config/nvim/lua/coding.lua b/src/.config/nvim/lua/coding.lua index 698511a..738bd61 100644 --- a/src/.config/nvim/lua/coding.lua +++ b/src/.config/nvim/lua/coding.lua @@ -140,7 +140,7 @@ local servers = { }, diagnostics = { -- get it to stop complaining about luasnip - globals = {'s', 'f', 't', "fmt", "c", "sn", "i", "rep", "d"}, + globals = {'s', 'f', 't', "fmt", "c", "sn", "i", "rep", "d", "k"}, }, } } diff --git a/src/.config/nvim/snippets/all.lua b/src/.config/nvim/snippets/all.lua index 777c58d..28c3575 100644 --- a/src/.config/nvim/snippets/all.lua +++ b/src/.config/nvim/snippets/all.lua @@ -74,7 +74,6 @@ return { {surr1} {comm} {cont} {surr2} - ]], { surr1 = f(rep_node, { 1 }), comm = d(1, function() diff --git a/src/.config/nvim/snippets/lua.lua b/src/.config/nvim/snippets/lua.lua index 28c969d..b07aad9 100644 --- a/src/.config/nvim/snippets/lua.lua +++ b/src/.config/nvim/snippets/lua.lua @@ -1,5 +1,5 @@ return { - s({ trig = "snip", desc = "meta snippet (snippet for making snippets)" }, fmt([[ + s({ trig = "snipf", desc = "meta snippet (snippet for making snippets)" }, fmt([[ s({{trig="{}", desc="{}"}}, {}), ]], { i(1, "trigger"), i(2, "human-readable description"), i(3, "") })) } diff --git a/src/.config/nvim/snippets/typst.lua b/src/.config/nvim/snippets/typst.lua new file mode 100644 index 0000000..86226f0 --- /dev/null +++ b/src/.config/nvim/snippets/typst.lua @@ -0,0 +1,101 @@ +return { + -------------------------------- + -------------------------------- + -- quick markup utilities + -------------------------------- + -------------------------------- + + s({ trig = "ss", desc = "superscript", wordTrig = false }, fmt("^({}) ", { i(1) })), + s({ trig = "qu", desc = "square (qu-artic) exponent", wordTrig = false }, t("^2 ")), + s({ trig = "cub", desc = "cub-ed exponent", wordTrig = false }, t("^3 ")), + + s({ trig = "link", desc = "labelled link" }, fmt('#link("{}{}")[{}]', { + i(1), + f(function(args, _) + -- use clipboard contents as a placeholder + if not args[1][1] or args[1][1] == "" then + return vim.fn.getreg("+") + else + return "" + end + end, { 1 }), + i(2), + })), + + -------------------------------- + -------------------------------- + -- figures + -- (biggest waste of time ever) + -- (supposedly advanced snippet practice) + -------------------------------- + -------------------------------- + s({ trig = "fig(%a?)", regTrig = true, desc = "create a figure" }, fmt([[ + #figure( + {content} + caption: [{caption}], + ) <{label}> + ]], { + caption = i(2, "Caption"), + label = i(1, "label"), + content = d(3, function(args, snip) + if not snip.captures[1] or snip.captures[1] == "" then + -- regular figure + return sn(nil, + fmt([[ + image("{path}.{ext}"), + ]], { + path = f(function() + return "fig/" .. vim.fn.expand("%:r") .. "/" .. (args[1][1] or nil) + end), + ext = c(1, { t("svg"), t("jpg") }) + }) + ) + elseif snip.captures[1] == "t" then + return sn(nil, + fmt([[ + tablef( + columns: {cols}, + table.header{head}, + {content} + ), + ]], { + head = i(1, "[Header][Header]"), + content = i(2, "[Content], [Content],"), + cols = f(function(largs) + -- the number of columns is the number of left brackets [ in the header + local _, cnt = string.gsub(largs[1][1], "%[", "") + -- the error for not converting to string was cryptic + -- wasted 10 minutes on this :( + return tostring(cnt) + end, { 1 }) + }) + ) + end + end, { 1 }), + })), + + -------------------------------- + -------------------------------- + -- document templates + -------------------------------- + -------------------------------- + s({ trig = "general", desc = "General document template" }, fmt([[ + #import "/templates/general.typ": template, lref + #import "/templates/libs.typ": * + #show: template.with( + title: "{}", + prefix: "{}", + suffix: "{}", + ) + ]], { i(1), i(2), i(3) })), + + s({ trig = "problem", desc = "Problem write-up template" }, fmt([[ + #import "/templates/problems.typ": template, source_code, status, lref + #import "/templates/libs.typ": * + #show: template.with( + problem_url: "{}", + title: "{}", + stat: "{}", + ) + ]], { i(1), i(2), i(3, "incomplete") })), +}