Store each node in our choice. Fix visual node.

main
Joshua Potter 2024-01-05 04:59:51 -07:00
parent 930607a63a
commit 288ed2d452
2 changed files with 53 additions and 44 deletions

View File

@ -1,13 +1,14 @@
local c = require('luasnip').choice_node
local d = require('luasnip').dynamic_node
local i = require('luasnip').insert_node
local sn = require('luasnip').snippet_node
local t = require('luasnip').text_node
local s = require('luasnip').snippet
local fmt = require('luasnip.extras.fmt').fmt local fmt = require('luasnip.extras.fmt').fmt
local ls = require('luasnip')
local ul = require('utils.luasnip')
local VISUAL = require('utils.luasnip').VISUAL local c = ls.choice_node
local d = ls.dynamic_node
local i = ls.insert_node
local r = ls.restore_node
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
return { return {
s( s(
@ -17,41 +18,36 @@ for {} in {}:
{}]], {}]],
{ {
c(1, { c(1, {
i(1, 'i'), i(nil, '_1'),
i(2, 'k'), i(nil, '_2'),
i(3, 'v'), i(nil, '_3'),
sn(4, { i(1, 'k'), t(', '), i(2, 'v') }), { i(1, '_4'), t(', '), i(2, '_5') },
}), }),
d(2, function(args, _, old_state) d(2, function(_, parent)
local default = i(nil, 'val') local index = ul.choice_index(parent.nodes[2])
local snip = old_state or default return sn(
nil,
if args[1][1] == 'i' then r(1, string.format('for-%d', index), ({
snip = sn(nil, c(1, { c(nil, {
{ t('range('), i(1, 'n'), t(')') }, { t('range('), i(1, 'n'), t(')') },
default, i(nil, 'val'),
})) }),
elseif args[1][1] == 'k' then c(nil, {
snip = sn(nil, c(1, { { i(1, 'dict'), t('.keys()') },
{ i(1, 'dict'), t('.keys()') }, i(nil, 'val'),
default, }),
})) c(nil, {
elseif args[1][1] == 'v' then { i(1, 'dict'), t('.values()') },
snip = sn(nil, c(1, { i(nil, 'val'),
{ i(1, 'dict'), t('.values()') }, }),
default, c(nil, {
})) { i(1, 'dict'), t('.items()') },
elseif args[1][1] == 'k, v' then i(nil, 'val'),
snip = sn(nil, c(1, { }),
{ i(1, 'dict'), t('.items()') }, })[index])
default, )
}))
end
snip.old_state = snip
return snip
end, { 1 }), end, { 1 }),
VISUAL, ul.visual_node,
} }
) )
), ),

View File

@ -2,16 +2,29 @@ local M = {}
local luasnip = require('luasnip') local luasnip = require('luasnip')
local types = require('luasnip.util.types') local types = require('luasnip.util.types')
local f = require('luasnip').function_node local function_node = require('luasnip').function_node
M.VISUAL = f(function(_, snip) M.visual_node = function_node(function(_, snip)
local res, env = {}, snip.env local env = snip.env
if type(env.LS_SELECT_RAW) ~= 'table' then
return env.LS_SELECT_RAW
end
local res = {}
for _, ele in ipairs(env.LS_SELECT_RAW) do for _, ele in ipairs(env.LS_SELECT_RAW) do
table.insert(res, ele) table.insert(res, ele)
end end
return res return res
end, {}) end, {})
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
function M.setup() function M.setup()
luasnip.config.setup { luasnip.config.setup {
region_check_events = 'InsertEnter', region_check_events = 'InsertEnter',