2022-04-12 22:14:43 +00:00
|
|
|
if not pcall(require, "luasnip") then
|
2022-01-08 18:08:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
local api = vim.api
|
|
|
|
local fn = vim.fn
|
2022-01-08 18:08:41 +00:00
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
local ls = require "luasnip"
|
2022-02-10 09:56:02 +00:00
|
|
|
|
2022-01-13 00:18:02 +00:00
|
|
|
local snippet = ls.snippet
|
|
|
|
local t = ls.text_node
|
2022-01-12 09:19:35 +00:00
|
|
|
|
|
|
|
local shortcut = function(val)
|
|
|
|
if type(val) == "string" then
|
|
|
|
return { t { val }, i(0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(val) == "table" then
|
|
|
|
for k, v in ipairs(val) do
|
|
|
|
if type(v) == "string" then
|
|
|
|
val[k] = t { v }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
2022-02-10 09:56:02 +00:00
|
|
|
local make = function(tbl)
|
2022-01-12 09:19:35 +00:00
|
|
|
local result = {}
|
|
|
|
for k, v in pairs(tbl) do
|
|
|
|
table.insert(result, (snippet({ trig = k, desc = v.desc }, shortcut(v))))
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
local snippets = {}
|
|
|
|
|
|
|
|
for _, ft_path in ipairs(api.nvim_get_runtime_file("lua/opdavies/snippets/ft/*.lua", true)) do
|
|
|
|
local ft = fn.fnamemodify(ft_path, ":t:r")
|
|
|
|
snippets[ft] = make(loadfile(ft_path)())
|
|
|
|
|
|
|
|
ls.add_snippets(ft, snippets[ft])
|
|
|
|
end
|
|
|
|
|
|
|
|
ls.add_snippets("js", snippets.javscript)
|
|
|
|
ls.add_snippets("typescript", snippets.javscript)
|
|
|
|
ls.add_snippets("vue", snippets.javscript)
|
|
|
|
|
|
|
|
ls.config.set_config {
|
|
|
|
enable_autosnippets = true,
|
|
|
|
history = true,
|
|
|
|
updateevents = "TextChanged,TextChangedI",
|
2022-01-08 18:08:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:56:02 +00:00
|
|
|
local imap = require("opdavies.keymap").imap
|
|
|
|
local map = require("opdavies.keymap").map
|
|
|
|
local nmap = require("opdavies.keymap").nmap
|
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
-- Expand the current item or just to the next item within the snippet.
|
2022-02-10 09:56:02 +00:00
|
|
|
map {
|
|
|
|
{ "i", "s" },
|
2022-04-12 22:14:43 +00:00
|
|
|
"<c-k>",
|
2022-02-10 09:56:02 +00:00
|
|
|
function()
|
2022-04-12 22:14:43 +00:00
|
|
|
if ls.expand_or_jumpable() then
|
|
|
|
ls.expand_or_jump()
|
2022-02-10 09:56:02 +00:00
|
|
|
end
|
|
|
|
end,
|
2022-04-12 22:14:43 +00:00
|
|
|
{ silent = true },
|
2022-02-10 09:56:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
-- Jump backwards.
|
2022-02-10 09:56:02 +00:00
|
|
|
map {
|
|
|
|
{ "i", "s" },
|
2022-04-12 22:14:43 +00:00
|
|
|
"<c-j>",
|
2022-02-10 09:56:02 +00:00
|
|
|
function()
|
2022-04-12 22:14:43 +00:00
|
|
|
if ls.jumpable(-1) then
|
|
|
|
ls.jump(-1)
|
2022-02-10 09:56:02 +00:00
|
|
|
end
|
|
|
|
end,
|
2022-04-12 22:14:43 +00:00
|
|
|
{ silent = true },
|
2022-02-10 09:56:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
-- Select within a list of options.
|
2022-02-10 09:56:02 +00:00
|
|
|
imap {
|
|
|
|
"<c-l>",
|
|
|
|
function()
|
|
|
|
if ls.choice_active() then
|
|
|
|
ls.change_choice(1)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
2022-01-08 18:08:41 +00:00
|
|
|
|
2022-04-12 22:14:43 +00:00
|
|
|
nmap { "<leader><leader>s", "<cmd>source ~/.config/nvim/after/plugin/luasnip.lua<CR>" }
|