Skip to content

Commit

Permalink
fix: open folds by changing feed-parameters.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
L3MON4D3 committed Jan 13, 2022
1 parent 0150d5c commit 626aa5e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -131,26 +131,28 @@ 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

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("<Right>", 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)
Expand Down
100 changes: 100 additions & 0 deletions tests/integration/jump_spec.lua
Original file line number Diff line number Diff line change
@@ -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("<Esc>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)

0 comments on commit 626aa5e

Please sign in to comment.