From 5e80cc2081ec616c828a0abe82d246c4dc0a4334 Mon Sep 17 00:00:00 2001 From: bh Date: Wed, 19 Nov 2025 14:50:22 +0800 Subject: Fixed snippets and also a little refactoring on the theme --- lua/config/.bak/.luasnip.bak.bak | 56 +++++++++++++++++++++++++++++++++ lua/config/completion.lua | 59 +++++++++++++++++++++++++++++++++++ lua/config/luasnip.lua | 67 +++++++++++++++++----------------------- lua/config/snippets.lua | 19 ------------ 4 files changed, 143 insertions(+), 58 deletions(-) create mode 100644 lua/config/.bak/.luasnip.bak.bak create mode 100644 lua/config/completion.lua delete mode 100644 lua/config/snippets.lua (limited to 'lua') diff --git a/lua/config/.bak/.luasnip.bak.bak b/lua/config/.bak/.luasnip.bak.bak new file mode 100644 index 0000000..e0b5617 --- /dev/null +++ b/lua/config/.bak/.luasnip.bak.bak @@ -0,0 +1,56 @@ +-- ~/.config/nvim/lua/config/luasnip.lua +return function() + local ls = require("luasnip") + local s = ls.snippet + local t = ls.text_node + local i = ls.insert_node + + -- Minimal test snippets + ls.snippets = { + all = { + s("hi", t("Hello, world!")), -- trigger: hi + }, + lua = { + s("fn", { -- trigger: fn + t("function "), i(1, "name"), t("("), i(2), t({")", "\t"}), + i(0), + t({"", "end"}) + }), + s("fori", { -- for loop snippet + t("for "), i(1, "i"), t(" = "), i(2, "1"), t(", "), i(3, "10"), t(" do"), + t({"", "\t"}), i(0), + t({"", "end"}) + }), + }, + } + + -- Load VSCode-style snippets (optional) + require("luasnip.loaders.from_vscode").lazy_load() + + -- Load Lua snippets from a separate file (optional) + -- require("luasnip.loaders.from_lua").load({ paths = "~/.config/nvim/lua/config/snippets.lua" }) + + local opts = { silent = true, noremap = true } + + -- Expand snippet or jump forward + vim.keymap.set({ "i", "s" }, "", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end + end, opts) + + -- Jump backward + vim.keymap.set({ "i", "s" }, "", function() + if ls.jumpable(-1) then + ls.jump(-1) + end + end, opts) + + -- Cycle choice nodes + vim.keymap.set({ "i", "s" }, "", function() + if ls.choice_active() then + ls.change_choice(1) + end + end, opts) +end + diff --git a/lua/config/completion.lua b/lua/config/completion.lua new file mode 100644 index 0000000..09a934e --- /dev/null +++ b/lua/config/completion.lua @@ -0,0 +1,59 @@ +-- return a function for lazy.nvim +return function() + local cmp = require("cmp") + local luasnip = require("luasnip") + local lspkind = require("lspkind") + + -- load friendly-snippets + require("luasnip.loaders.from_vscode").lazy_load() + + cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), -- manual trigger + [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "path" }, + { name = "luasnip" }, + }, { + { name = "buffer" }, + }), + formatting = { + format = lspkind.cmp_format({ with_text = true, maxwidth = 50 }), + }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + completion = { + autocomplete = false, -- only trigger with + }, + }) +end + diff --git a/lua/config/luasnip.lua b/lua/config/luasnip.lua index 54ba5de..4524322 100644 --- a/lua/config/luasnip.lua +++ b/lua/config/luasnip.lua @@ -1,47 +1,36 @@ +-- Return a function so we can call it from Lazy.nvim return function() - local ls = require("luasnip") - local s = ls.snippet - local t = ls.text_node - local i = ls.insert_node + local ls = require("luasnip") - -- Minimal test snippet - ls.snippets = { - all = { - s("hi", t("Hello, world!")), - }, - lua = { - s("fn", { - t("function "), i(1, "name"), t("("), i(2), t({")", "\t"}), - i(0), - t({"", "end"}) - }), - }, - } + -- Load snippets from friendly-snippets + require("luasnip.loaders.from_vscode").lazy_load() - local opts = { silent = true, noremap = true } + -- Example Lua snippets + ls.add_snippets("lua", { + ls.parser.parse_snippet("hi", "print('Hello, world!')"), + ls.parser.parse_snippet("fn", "function ${1:name}(${2:args})\n\t$0\nend"), + }) - -- Expand or jump forward - vim.keymap.set({ "i", "s" }, "", function() - if ls.expand_or_jumpable() then - ls.expand_or_jump() - end - end, opts) + local opts = { silent = true, noremap = true } - -- Jump backward - vim.keymap.set({ "i", "s" }, "", function() - if ls.jumpable(-1) then - ls.jump(-1) - end - end, opts) + -- Expand snippet or jump forward + vim.keymap.set({ "i", "s" }, "", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end + end, opts) - -- Cycle choice nodes - vim.keymap.set({ "i", "s" }, "", function() - if ls.choice_active() then - ls.change_choice(1) - end - end, opts) + -- Jump backward + vim.keymap.set({ "i", "s" }, "", function() + if ls.jumpable(-1) then + ls.jump(-1) + end + end, opts) - -- Load snippets - require("luasnip.loaders.from_vscode").lazy_load() + -- Change choice in choice node + vim.keymap.set({ "i", "s" }, "", function() + if ls.choice_active() then + ls.change_choice(1) + end + end, opts) end - diff --git a/lua/config/snippets.lua b/lua/config/snippets.lua deleted file mode 100644 index 8b0d30b..0000000 --- a/lua/config/snippets.lua +++ /dev/null @@ -1,19 +0,0 @@ --- ~/.config/nvim/lua/snippets.lua -local ls = require("luasnip") -local s = ls.snippet -local t = ls.text_node -local i = ls.insert_node - -ls.snippets = { - all = { - s("hi", t("Hello, world!")), -- Type "hi" -> expand to Hello, world! - }, - lua = { - s("fn", { -- Type "fn" -> expand to a function template - t("function "), i(1, "name"), t("("), i(2), t({")", "\t"}), - i(0), - t({"", "end"}) - }), - }, -} - -- cgit v1.2.3