From 14bfbece879fbb97f12f28e1dc0f7240c5ee4627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Wed, 9 Jul 2025 11:26:38 +0200 Subject: [PATCH] nvim/cmp: load snippets per filetype --- .config/nvim/ftplugin/go.lua | 16 +++++ .config/nvim/ftplugin/haskell.lua | 4 ++ .config/nvim/ftplugin/typst.lua | 27 +++++++ .config/nvim/plugin/cmp.lua | 114 +----------------------------- 4 files changed, 50 insertions(+), 111 deletions(-) diff --git a/.config/nvim/ftplugin/go.lua b/.config/nvim/ftplugin/go.lua index c37615bc..c03f8e08 100644 --- a/.config/nvim/ftplugin/go.lua +++ b/.config/nvim/ftplugin/go.lua @@ -1 +1,17 @@ vim.bo.expandtab = false + +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 + + luasnip.add_snippets("go", { + s("ie", { + t("if err != nil {"), cr(), + t("\t"), i(0), cr(), + t("}"), + }), + }) +end diff --git a/.config/nvim/ftplugin/haskell.lua b/.config/nvim/ftplugin/haskell.lua index 9a1eb6f3..29240dcf 100644 --- a/.config/nvim/ftplugin/haskell.lua +++ b/.config/nvim/ftplugin/haskell.lua @@ -3,3 +3,7 @@ opts.matchit_let_in() vim.bo.shiftwidth = 2 vim.bo.expandtab = true + +local luasnip = require("luasnip") +local haskell_snippets = require("haskell-snippets").all +luasnip.add_snippets("haskell", haskell_snippets, { key = "haskell" }) diff --git a/.config/nvim/ftplugin/typst.lua b/.config/nvim/ftplugin/typst.lua index 929f7c77..e92277bb 100644 --- a/.config/nvim/ftplugin/typst.lua +++ b/.config/nvim/ftplugin/typst.lua @@ -7,3 +7,30 @@ vim.keymap.set("n", "f", function() vim.cmd([[silent exec "%!typstyle -c 100"]]) vim.fn.winrestview(saved) end, { desc = "ft(typst): Format with typstyle" }) + +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 diff --git a/.config/nvim/plugin/cmp.lua b/.config/nvim/plugin/cmp.lua index d7e20769..60fc87b4 100644 --- a/.config/nvim/plugin/cmp.lua +++ b/.config/nvim/plugin/cmp.lua @@ -28,132 +28,24 @@ local has_words_before = function() return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil end ------------------ --- Lazy loader -- ------------------ -require("luasnip.loaders.from_vscode").lazy_load { paths = { "./snippets" } } - ----------- --- Init -- ----------- luasnip.setup { update_events = { "TextChanged", "TextChangedI" } } ------------ --- Typst -- ------------ -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(), - }), -}) - -local function get_cms() - assert(vim.bo.commentstring ~= "", "comment string is not set") - local left = vim.bo.commentstring:gsub("%s*%%s.*", "") - local right = vim.bo.commentstring:gsub(".*%%s%s*", "") - if right == "" then - right = left - end - return { left = left, right = right } -end -local function horizon(args) - local cms = get_cms() - local chr = cms.left:sub(-1) - local len = vim.fn.strdisplaywidth(args[1][1]) - - local acc = cms.left - for _ = cms.left:len(), len + cms.right:len() + 1, 1 do - acc = acc .. chr - end - acc = acc .. cms.right - - return acc -end -local function left() - local cms = get_cms() - return cms.left .. " " -end -local function right() - local cms = get_cms() - return " " .. cms.right -end - --- stylua: ignore start -luasnip.add_snippets("all", { - s("banner", { - f(horizon, { 1 }), cr(), - f(left), i(1), f(right), cr(), - f(horizon, { 1 }), - }), -}) --- stylua: ignore end - -------------- --- Haskell -- -------------- -local haskell_snippets = require("haskell-snippets").all -luasnip.add_snippets("haskell", haskell_snippets, { key = "haskell" }) - ------------- --- Golang -- ------------- --- stylua: ignore start -luasnip.add_snippets("go", { - s("ie", { - t("if err != nil {"), cr(), - t("\t"), i(0), cr(), - t("}"), - }), -}) --- stylua: ignore end - ---------------- --- Setup CMP -- ---------------- -local of_filetype = function(fts) - local ft = vim.bo.filetype - for _, v in ipairs(fts) do - if v == ft then - return true - end - end - return false -end - cmp.setup { snippet = { expand = function(args) luasnip.lsp_expand(args.body) end, }, mapping = cmp.mapping.preset.insert(), + -- TODO: how can I make some take precedence of others? sources = { { name = "luasnip" }, { name = "nvim_lsp" }, { name = "path" }, - { - name = "buffer", - keyword_length = 10, - option = { - enable_in_context = function() return of_filetype { "tex", "markdown", "typst" } end, - }, - }, + { name = "buffer" }, { name = "spell", - keyword_length = 10, + keyword_length = 10, -- PERF: option = { keep_all_entries = true, - enable_in_context = function() return of_filetype { "tex", "markdown", "typst" } end, }, }, },