Beginner luasnip configuration for terraform

This commit is contained in:
anmiller 2024-05-13 19:44:17 +00:00
parent 4360067486
commit 7f35d0baed

View File

@ -34,7 +34,8 @@ ls.config.set_config {
-- Expand if expandable, or jump forward if jumpable -- Expand if expandable, or jump forward if jumpable
-- If neither, do nothing queitly -- If neither, do nothing queitly
vim.keymap.set({ "i", "s" }, "<C-K>", function() -- vim.keymap.set({ "i", "s" }, "<C-K>", function()
vim.keymap.set({ "i", "s" }, "<Tab>", function()
if ls.expand_or_jumpable() then if ls.expand_or_jumpable() then
ls.expand_or_jump() ls.expand_or_jump()
end end
@ -42,41 +43,56 @@ end, { silent = true })
-- end, { silent = true }) -- end, { silent = true })
-- Jump backwards to the previous jumpable point -- Jump backwards to the previous jumpable point
vim.keymap.set({ "i", "s" }, "<C-J>", function() -- vim.keymap.set({ "i", "s" }, "<C-J>", function()
vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
if ls.jumpable(-1) then if ls.jumpable(-1) then
ls.jump(-1) ls.jump(-1)
end end
end, { silent = true }) end, { silent = true })
-- Selecting from a list of options in a choice node -- Selecting from a list of options in a choice node
vim.keymap.set({ "i" }, "<C-L>", function() -- vim.keymap.set({ "i" }, "<C-L>", function()
vim.keymap.set({ "i" }, "<A-Tab>", function()
if ls.choice_active() then if ls.choice_active() then
ls.change_choice(1) ls.change_choice(1)
end end
end) end)
-- Allow us to hot reload snippets -- Allow us to hot reload snippets by unloading snippets then resourcing file
vim.keymap.set("n", "<leader><leader>s", "<cmd>source ~/.config/nvim/lua/user/luasnip.lua<CR>") local ls_cleanup = function()
ls.cleanup()
vim.cmd("source ~/.config/nvim/lua/user/luasnip.lua")
end
vim.keymap.set("n", "<leader><leader>s", ls_cleanup)
ls.add_snippets('lua', { ls.add_snippets(nil, {
ls.parser.parse_snippet("expand", "-- this is what was expanded!"), terraform = {
ls.parser.parse_snippet("lf", "local $1 = function($2)\n $0\nend"), ls.parser.parse_snippet("res", "resource \"$1\" \"$2\" {\n $3\n}"),
ls.parser.parse_snippet("mf", "$1.$2 = function($3)\n $0\nend"), ls.parser.parse_snippet("dat", "data \"$1\" \"$2\" {\n $3\n}"),
-- Example from snippet writing guide: https://github.com/L3MON4D3/LuaSnip/blob/master/Examples/snippets.lua#L190 ls.parser.parse_snippet("var", "variable \"$1\" {\n type = $2\n description = $3\n}"),
s( ls.parser.parse_snippet("loc", "locals {\n $1\n}"),
"fmt1", ls.parser.parse_snippet("mod", "module {\n source = $1\n}"),
fmt("To {title} {} {}.", { },
i(2, "Name"), lua = {
i(3, "Surname"), ls.parser.parse_snippet("expand", "-- this is what was expanded!"),
title = c(1, { t("Mr."), t("Ms.") }), 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
-- Example from video: https://www.youtube.com/watch?v=Dn800rlPIho s(
s( "fmt1",
"req", fmt("To {title} {} {}.", {
fmt("local {} = require('{}')", { i(2, "Name"),
i(1, "default"), i(3, "Surname"),
rep(1) 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)
})
),
}
}) })