2024-01-04 19:43:56 +00:00
|
|
|
|
local M = {}
|
|
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
|
local luasnip = require("luasnip")
|
|
|
|
|
local types = require("luasnip.util.types")
|
2024-01-04 19:43:56 +00:00
|
|
|
|
|
2024-01-05 16:58:57 +00:00
|
|
|
|
function M.visual_isn(pos)
|
2024-01-05 19:17:54 +00:00
|
|
|
|
local d = luasnip.dynamic_node
|
|
|
|
|
local i = luasnip.insert_node
|
2024-01-05 15:45:33 +00:00
|
|
|
|
local isn = luasnip.indent_snippet_node
|
2024-01-05 19:17:54 +00:00
|
|
|
|
local sn = luasnip.snippet_node
|
2024-01-05 15:45:33 +00:00
|
|
|
|
|
2024-01-05 19:17:54 +00:00
|
|
|
|
return isn(pos, d(1, function(_, parent)
|
|
|
|
|
local res, env = {}, parent.snippet.env
|
|
|
|
|
for _, ele in ipairs(env.LS_SELECT_DEDENT or {}) do
|
|
|
|
|
table.insert(res, ele)
|
|
|
|
|
end
|
|
|
|
|
return sn(nil, i(1, res))
|
2024-03-05 03:10:57 +00:00
|
|
|
|
end), "$PARENT_INDENT\t")
|
2024-01-05 15:45:33 +00:00
|
|
|
|
end
|
2024-01-04 19:43:56 +00:00
|
|
|
|
|
2024-01-05 11:59:51 +00:00
|
|
|
|
function M.choice_index(choice_node)
|
|
|
|
|
for i, c in ipairs(choice_node.choices) do
|
|
|
|
|
if c == choice_node.active_choice then
|
|
|
|
|
return i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return 1
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-04 19:43:56 +00:00
|
|
|
|
function M.setup()
|
|
|
|
|
luasnip.config.setup {
|
2024-03-05 03:10:57 +00:00
|
|
|
|
region_check_events = "InsertEnter",
|
|
|
|
|
delete_check_events = "InsertLeave",
|
|
|
|
|
store_selection_keys = "<tab>",
|
2024-01-24 17:47:35 +00:00
|
|
|
|
enable_autosnippets = true,
|
2024-01-04 19:43:56 +00:00
|
|
|
|
ext_opts = {
|
|
|
|
|
[types.snippet] = {
|
|
|
|
|
active = {
|
2024-03-05 03:10:57 +00:00
|
|
|
|
virt_text = { { "●", "DiagnosticWarn" } },
|
2024-01-04 19:43:56 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2024-01-05 19:17:54 +00:00
|
|
|
|
[types.insertNode] = {
|
|
|
|
|
passive = {
|
2024-03-05 03:10:57 +00:00
|
|
|
|
hl_group = "DiagnosticVirtualTextWarn",
|
2024-01-05 19:17:54 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2024-01-04 19:43:56 +00:00
|
|
|
|
[types.choiceNode] = {
|
|
|
|
|
active = {
|
2024-03-05 03:10:57 +00:00
|
|
|
|
hl_group = "DiagnosticVirtualTextHint",
|
|
|
|
|
virt_text = { { "⧨", "DiagnosticHint" } },
|
2024-01-04 19:43:56 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 15:45:33 +00:00
|
|
|
|
-- Track where we are expanding the snippet.
|
2024-03-05 03:10:57 +00:00
|
|
|
|
luasnip.env_namespace("INFO", {
|
2024-01-05 15:45:33 +00:00
|
|
|
|
init = function(info) return { POS = info.pos } end,
|
|
|
|
|
})
|
|
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
|
vim.keymap.set({ "i", "s" }, "<c-e>", function()
|
2024-01-04 23:02:10 +00:00
|
|
|
|
if luasnip.choice_active() then
|
2024-03-05 03:10:57 +00:00
|
|
|
|
return "<Plug>luasnip-next-choice"
|
2024-01-04 23:02:10 +00:00
|
|
|
|
else
|
2024-03-05 03:10:57 +00:00
|
|
|
|
return "<c-e>"
|
2024-01-04 23:02:10 +00:00
|
|
|
|
end
|
|
|
|
|
end, { silent = true, expr = true, remap = true })
|
|
|
|
|
|
2024-03-05 03:10:57 +00:00
|
|
|
|
vim.keymap.set({ "i", "s" }, "<c-y>", function()
|
2024-01-04 23:02:10 +00:00
|
|
|
|
if luasnip.choice_active() then
|
2024-03-05 03:10:57 +00:00
|
|
|
|
return "<Plug>luasnip-prev-choice"
|
2024-01-04 23:02:10 +00:00
|
|
|
|
else
|
2024-03-05 03:10:57 +00:00
|
|
|
|
return "<c-y>"
|
2024-01-04 23:02:10 +00:00
|
|
|
|
end
|
|
|
|
|
end, { silent = true, expr = true, remap = true })
|
|
|
|
|
|
2024-01-04 19:43:56 +00:00
|
|
|
|
-- Allow aborting the active snippet at any point in time.
|
2024-03-05 03:10:57 +00:00
|
|
|
|
vim.keymap.set({ "n", "i", "s" }, "<c-l>", "<cmd>LuaSnipUnlinkCurrent<cr>")
|
2024-01-26 00:47:12 +00:00
|
|
|
|
|
|
|
|
|
-- Allow toggling autoexpansion on and off. This is more or less the intended
|
|
|
|
|
-- approach: https://github.com/L3MON4D3/LuaSnip/issues/832#issuecomment-1474993417
|
|
|
|
|
luasnip.expand_auto_on = true
|
|
|
|
|
|
|
|
|
|
local expand_auto = luasnip.expand_auto
|
|
|
|
|
local toggle_expand_auto = function()
|
|
|
|
|
if luasnip.expand_auto_on then
|
|
|
|
|
luasnip.expand_auto = function() end
|
|
|
|
|
else
|
|
|
|
|
luasnip.expand_auto = expand_auto
|
|
|
|
|
end
|
|
|
|
|
luasnip.expand_auto_on = not luasnip.expand_auto_on
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
toggle_expand_auto()
|
2024-03-05 03:10:57 +00:00
|
|
|
|
vim.keymap.set({ "n", "i", "s" }, "", toggle_expand_auto)
|
2024-01-04 19:43:56 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return M
|