From 5f34897169ffa5e1bcb8d57d65de3f5f0756b28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Mon, 28 Jul 2025 00:23:28 +0200 Subject: [PATCH] nvim/typst: lazyload snippets only once --- .config/nvim/ftplugin/typst.lua | 64 +++++++++++++++++---------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/.config/nvim/ftplugin/typst.lua b/.config/nvim/ftplugin/typst.lua index e92277bb..9d1df8fe 100644 --- a/.config/nvim/ftplugin/typst.lua +++ b/.config/nvim/ftplugin/typst.lua @@ -2,35 +2,39 @@ vim.bo.shiftwidth = 2 vim.bo.tabstop = 2 vim.bo.textwidth = 100 -vim.keymap.set("n", "f", function() - local saved = vim.fn.winsaveview() - vim.cmd([[silent exec "%!typstyle -c 100"]]) - vim.fn.winrestview(saved) -end, { desc = "ft(typst): Format with typstyle" }) +local once = require("once") -do - local luasnip = require("luasnip") - local s = luasnip.snippet - local t = luasnip.text_node - local cr = function() return t { "", "" } end - local i = luasnip.insert_node - local f = luasnip.function_node +once.test_and_load("ft_typst", function() + vim.keymap.set("n", "f", function() + local saved = vim.fn.winsaveview() + vim.cmd([[silent exec "%!typstyle -c 100"]]) + vim.fn.winrestview(saved) + end, { desc = "ft(typst): Format with typstyle" }) - local function show_date_typst_entry() return os.date('"%Y-%m-%d %H:%M:%S"') end - luasnip.add_snippets("typst", { - s("entry", { - t("#entry("), - f(show_date_typst_entry), - t { ")[", "" }, - t(" "), - i(0), - t { "", "]" }, - }), - s("lang", { - t('#set text(lang: "'), - i(0), - t('")'), - cr(), - }), - }) -end + do + local luasnip = require("luasnip") + local s = luasnip.snippet + local t = luasnip.text_node + local cr = function() return t { "", "" } end + local i = luasnip.insert_node + local f = luasnip.function_node + + local function show_date_typst_entry() return os.date('"%Y-%m-%d %H:%M:%S"') end + luasnip.add_snippets("typst", { + s("entry", { + t("#entry("), + f(show_date_typst_entry), + t { ")[", "" }, + t(" "), + i(0), + t { "", "]" }, + }), + s("lang", { + t('#set text(lang: "'), + i(0), + t('")'), + cr(), + }), + }) + end +end)