From 626aa5e62e385e9cd73bad80d84bcd06dfa387e6 Mon Sep 17 00:00:00 2001 From: L3MON4D3 Date: Thu, 13 Jan 2022 17:20:28 +0100 Subject: [PATCH] fix: open folds by changing feed-parameters. Normally, if one moves into a fold (via k, j, ...) the fold is opened. This does not happen by default for movements from feedkeys, setting "t" makes keys seem like they were typed. (:h feedkeys) This change paves the way for not needing to leave INSERT at all (at least when both source- and target-mode are INSERT). --- lua/luasnip/util/util.lua | 22 +++---- tests/integration/jump_spec.lua | 100 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 tests/integration/jump_spec.lua diff --git a/lua/luasnip/util/util.lua b/lua/luasnip/util/util.lua index fcc9ee962..fbe40cff9 100644 --- a/lua/luasnip/util/util.lua +++ b/lua/luasnip/util/util.lua @@ -111,16 +111,16 @@ local function normal_move_before(new_cur_pos) tostring(new_cur_pos[1] + 1) .. "G0" .. tostring(new_cur_pos[2] - 1) - -- open folds! - .. "lzv", - "n", + .. "l", + -- passing only "n" doesn't open folds (:h feedkeys). + "nt", true ) elseif new_cur_pos[2] - 1 == 0 then - vim.api.nvim_feedkeys(tostring(new_cur_pos[1] + 1) .. "G0zv", "n", true) + vim.api.nvim_feedkeys(tostring(new_cur_pos[1] + 1) .. "G0", "nt", true) else -- column is 0, includes end of previous line. Move there. - vim.api.nvim_feedkeys(tostring(new_cur_pos[1]) .. "G$zv", "n", true) + vim.api.nvim_feedkeys(tostring(new_cur_pos[1]) .. "G$", "nt", true) end end @@ -131,12 +131,13 @@ local function normal_move_on(new_cur_pos) .. "G0" .. tostring(new_cur_pos[2]) -- open folds! - .. "lzv", - "n", + .. "l", + -- passing only "n" doesn't open folds (:h feedkeys). + "nt", true ) else - vim.api.nvim_feedkeys(tostring(new_cur_pos[1] + 1) .. "G0", "n", true) + vim.api.nvim_feedkeys(tostring(new_cur_pos[1] + 1) .. "G0", "nt", true) end end @@ -144,13 +145,14 @@ local function normal_move_on_insert(new_cur_pos) local keys = vim.api.nvim_replace_termcodes( tostring(new_cur_pos[1] + 1) -- open folds! - .. "G0zvi" + .. "G0i" .. string.rep("", new_cur_pos[2]), true, false, true ) - vim.api.nvim_feedkeys(keys, "n", true) + -- passing only "n" doesn't open folds (:h feedkeys). + vim.api.nvim_feedkeys(keys, "nt", true) end local function multiline_equal(t1, t2) diff --git a/tests/integration/jump_spec.lua b/tests/integration/jump_spec.lua new file mode 100644 index 000000000..471179135 --- /dev/null +++ b/tests/integration/jump_spec.lua @@ -0,0 +1,100 @@ + +local helpers = require("test.functional.helpers")(after_each) +local exec_lua, feed = helpers.exec_lua, helpers.feed +local ls_helpers = require("helpers") +local Screen = require("test.functional.ui.screen") + +describe("Jumping", function() + local screen + + before_each(function() + helpers.clear() + ls_helpers.session_setup_luasnip() + + screen = Screen.new(50, 5) + screen:attach() + screen:set_default_attr_ids({ + [0] = { bold = true, foreground = Screen.colors.Blue }, + [1] = { bold = true, foreground = Screen.colors.Brown }, + [2] = { bold = true }, + [3] = { background = Screen.colors.LightGray }, + [4] = {background = Screen.colors.LightGrey, foreground = Screen.colors.DarkBlue}; + }) + end) + + after_each(function() + screen:detach() + end) + + it("Folds are opened when jumped into", function() + local snip = [[ + s("aug", { + t("augroup "), + i(1, "GroupName"), + t({ "AuGroup", "\t" }), + t({ "au!", "\tau " }), + i(2, "CursorHold * redrawstatus"), + i(0), + t({ "", "augroup end" }), + }) + ]] + + helpers.exec("set foldenable foldmethod=manual") + + exec_lua("ls.snip_expand("..snip..")") + screen:expect{grid=[[ + augroup ^G{3:roupName}AuGroup | + au! | + au CursorHold * redrawstatus | + augroup end | + {2:-- SELECT --} | + ]]} + + -- fold middle-lines. + feed("jzfj") + screen:expect{grid=[[ + augroup GroupNameAuGroup | + {4:^+-- 2 lines: au!·································}| + augroup end | + {0:~ }| + | + ]]} + + -- folded lines are opened correctly when jumped into them. + exec_lua("ls.jump(1)") + screen:expect{grid=[[ + augroup GroupNameAuGroup | + au! | + au ^C{3:ursorHold * redrawstatus} | + augroup end | + {2:-- SELECT --} | + ]]} + end) + + it("jumps correctly when multibyte-characters are present.", function() + local snip = [[ + s("trig", { + t{"asdf", "핓s㕥f"}, i(1, "asdf"), + t{"", "asdf"}, i(2, "핓sdf"), + }) + ]] + + exec_lua("ls.snip_expand("..snip..")") + screen:expect{grid=[[ + asdf | + 핓s㕥f^a{3:sdf} | + asdf핓sdf | + {0:~ }| + {2:-- SELECT --} | + ]]} + + exec_lua("ls.jump(1)") + screen:expect{grid=[[ + asdf | + 핓s㕥fasdf | + asdf^핓{3:sdf} | + {0:~ }| + {2:-- SELECT --} | + ]]} + end) +end)