diff --git a/tests/integration/choice_spec.lua b/tests/integration/choice_spec.lua index 3b4d28d1..f0b6bb75 100644 --- a/tests/integration/choice_spec.lua +++ b/tests/integration/choice_spec.lua @@ -523,14 +523,146 @@ screen:expect({ {2:-- SELECT --} | ]] }) + feed("aa") + -- simulate vim.ui.select that modifies the cursor. + -- Can happen in the wild with plugins like dressing.nvim (although + -- those usually just leave INSERT), and we would like to prevent it. + exec_lua[[ + vim.ui.select = function(_,_,cb) + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes( + "", + true, + false, + true + ), + "nix", + true) + + cb(nil, 2) + end + ]] -- re-selecting correctly highlights text again (test by editing so the test does not pass immediately, without any changes!) - feed("lua require('luasnip.extras.select_choice')()2val") + exec_lua("require('luasnip.extras.select_choice')()") screen:expect({ grid = [[ - for val^ in do | + for a^a in do | | {2:-- INSERT --} | ]] +}) + end) + + it("updates the active node before changing choice.", function() + exec_lua[[ + ls.setup({ + link_children = true + }) + ls.snip_expand(s("trig", { + t":", + c(1, { + {r(1, "key", d(1, function(args) + if not args[1] then + return sn(nil, {i(1, "aa", {key = "i"})}) + else + return sn(nil, {i(1, "cc"), i(2, args[1]:gsub("a", "ee"), {key = "i"})}) + end + end, { opt(k("i")) }, {snippetstring_args = true}))}, + {t".", r(1, "key"), t"."} + }, {restore_cursor = true}), + t":" + })) + ]] + exec_lua"ls.jump(1)" + feed("i aa ") +screen:expect({ + grid = [[ + :ccee a^a ee: | + {0:~ }| + {2:-- INSERT --} | + ]] +}) + -- if we wouldn't update before the change_choice, the last_args of the + -- restored dynamicNode would not fit its current content, and we'd + -- lose the text inserted until now due to the update (as opposed to + -- a proper restore of dynamicNode.snip, which should occur in a + -- restoreNode). + exec_lua"ls.change_choice(1)" +screen:expect({ + grid = [[ + :.ccee ee^ee ee.: | + {0:~ }| + {2:-- INSERT --} | + ]] +}) + exec_lua"ls.set_choice(2)" +screen:expect({ unchanged = true }) + + -- test some more wild stuff, just because. + feed(" ") + exec_lua[[ + ls.snip_expand(s("trig", { + t":", + c(1, { + {r(1, "key", d(1, function(args) + if not args[1] then + return sn(nil, {i(1, "aa", {key = "i"})}) + else + return sn(nil, {i(1, "cc"), i(2, args[1]:gsub("a", "ee"), {key = "i"})}) + end + end, { opt(k("i")) }, {snippetstring_args = true}))}, + {t".", r(1, "key"), t"."} + }, {restore_cursor = true}), + t":" + })) + ]] + +screen:expect({ + grid = [[ + :.ccee e :^c{3:c}eeee:eee ee.: | + {0:~ }| + {2:-- SELECT --} | + ]] +}) + exec_lua"ls.jump(1)" + feed("i aa ") + exec_lua"ls.set_choice(2)" +screen:expect({ + grid = [[ + :.ccee e :.ccee ee^ee ee.:eee ee.: | + {0:~ }| + {2:-- INSERT --} | + ]] +}) + + -- reselect outer choiceNode + exec_lua"ls.jump(-1)" + exec_lua"ls.jump(-1)" + exec_lua"ls.jump(-1)" + exec_lua"ls.jump(1)" +screen:expect({ + grid = [[ + :.cc^e{3:e e :.ccee eeee ee.:eee ee}.: | + {0:~ }| + {2:-- SELECT --} | + ]] +}) + exec_lua"ls.change_choice(1)" +screen:expect({ + grid = [[ + :cc^e{3:e e :.ccee eeee ee.:eee ee}: | + {0:~ }| + {2:-- SELECT --} | + ]] +}) + exec_lua"ls.jump(1)" + exec_lua"ls.jump(1)" +screen:expect({ + grid = [[ + :ccee e :.cc^e{3:e eeee ee}.:eee ee: | + {0:~ }| + {2:-- SELECT --} | + ]] }) end) end) diff --git a/tests/integration/dynamic_spec.lua b/tests/integration/dynamic_spec.lua index afd2d328..900385ea 100644 --- a/tests/integration/dynamic_spec.lua +++ b/tests/integration/dynamic_spec.lua @@ -427,4 +427,36 @@ screen:expect({ ]] }) end) + + it("cursor-position is moved with text-manipulations.", function() + exec_lua[[ + ls.snip_expand(s("trig", { + d(1, function(args) + if not args[1] then + return sn(nil, {i(1, "asdf", {key = "ins"})}) + else + return sn(nil, {i(1, args[1]:gsub("a", "ee"), {key = "ins"})}) + end + end, {opt(k("ins"))}, {snippetstring_args = true}) + })) + ]] + +screen:expect({ + grid = [[ + ^e{3:esdf} | + {0:~ }| + {2:-- SELECT --} | + ]] +}) + feed("aaaaaa") +screen:expect({ + grid = [[ + eeeeee^eeeeee | + {0:~ }| + | + ]] +}) + end) + + it("") end) diff --git a/tests/integration/restore_spec.lua b/tests/integration/restore_spec.lua index 9a77cd5c..ac2f9400 100644 --- a/tests/integration/restore_spec.lua +++ b/tests/integration/restore_spec.lua @@ -462,7 +462,7 @@ screen:expect({ end) -- make sure store and update_restore propagate. - it("correctly restores snippets (3).", function() + it("correctly restores snippets (4).", function() exec_lua([[ ls.setup({link_children = true})