Beginning binding/config for luasnip

This commit is contained in:
Andrew R. M 2025-04-04 17:42:35 +00:00
parent 8b29c959fb
commit 67d46f99c7
2 changed files with 83 additions and 0 deletions

View File

@ -7,6 +7,7 @@ vim.g.mapleader = ','
require('user.lspconfig')
require('user.indentblanklines')
require('user.luasnip')
EOF

View File

@ -0,0 +1,82 @@
local ls = require("luasnip")
local fmt = require('luasnip.extras.fmt')
local rep = require('luasnip.extras').rep
-- some shorthands...
-- Taken from video/guide. Honestly I don't like these should probably work on learning their names
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local c = ls.choice_node
local d = ls.dynamic_node
local r = ls.restore_node
local l = require("luasnip.extras").lambda
local rep = require("luasnip.extras").rep
local p = require("luasnip.extras").partial
local m = require("luasnip.extras").match
local n = require("luasnip.extras").nonempty
local dl = require("luasnip.extras").dynamic_lambda
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local types = require("luasnip.util.types")
local conds = require("luasnip.extras.conditions")
local conds_expand = require("luasnip.extras.conditions.expand")
ls.config.set_config {
history = true,
-- This allows dynamic snippets to update as you type
updateevents = "TextChanged,TextChangedI"
}
-- Expand if expandable, or jump forward if jumpable
-- If neither, do nothing queitly
vim.keymap.set({ "i", "s" }, "<C-K>", function()
if ls.expand_or_jumpable() then
ls.expand_or_jump()
end
end, { silent = true })
-- end, { silent = true })
-- Jump backwards to the previous jumpable point
vim.keymap.set({ "i", "s" }, "<C-J>", function()
if ls.jumpable(-1) then
ls.jump(-1)
end
end, { silent = true })
-- Selecting from a list of options in a choice node
vim.keymap.set({ "i" }, "<C-L>", function()
if ls.choice_active() then
ls.change_choice(1)
end
end)
-- Allow us to hot reload snippets
vim.keymap.set("n", "<leader><leader>s", "<cmd>source ~/.config/nvim/lua/user/luasnip.lua<CR>")
ls.add_snippets('lua', {
ls.parser.parse_snippet("expand", "-- this is what was expanded!"),
ls.parser.parse_snippet("lf", "local $1 = function($2)\n $0\nend"),
ls.parser.parse_snippet("mf", "$1.$2 = function($3)\n $0\nend"),
-- Example from snippet writing guide: https://github.com/L3MON4D3/LuaSnip/blob/master/Examples/snippets.lua#L190
s(
"fmt1",
fmt("To {title} {} {}.", {
i(2, "Name"),
i(3, "Surname"),
title = c(1, { t("Mr."), t("Ms.") }),
})
),
-- Example from video: https://www.youtube.com/watch?v=Dn800rlPIho
s(
"req",
fmt("local {} = require('{}')", {
i(1, "default"),
rep(1)
})
),
})