Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ Both methods automatically place the library in the plugin root directory.
fold_toggle_recursive = "zA", -- Toggle fold recursively
fold_open_all = "zR", -- Open all folds in tree
fold_close_all = "zM", -- Close all folds in tree
scroll_down = "<C-d>", -- Scroll diff panes down half a page
scroll_up = "<C-u>", -- Scroll diff panes up half a page
},
history = {
select = "<CR>", -- Select commit/file or toggle expand
Expand All @@ -287,6 +289,8 @@ Both methods automatically place the library in the plugin root directory.
fold_toggle_recursive = "zA", -- Toggle fold recursively
fold_open_all = "zR", -- Open all folds in tree
fold_close_all = "zM", -- Close all folds in tree
scroll_down = "<C-d>", -- Scroll diff panes down half a page
scroll_up = "<C-u>", -- Scroll diff panes up half a page
},
conflict = {
accept_incoming = "<leader>ct", -- Accept incoming (theirs/left) change
Expand Down
4 changes: 4 additions & 0 deletions doc/codediff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ Setup entry point:
fold_toggle_recursive = "zA",
fold_open_all = "zR",
fold_close_all = "zM",
scroll_down = "<C-d>",
scroll_up = "<C-u>",
},
history = {
select = "<CR>",
Expand All @@ -371,6 +373,8 @@ Setup entry point:
fold_toggle_recursive = "zA",
fold_open_all = "zR",
fold_close_all = "zM",
scroll_down = "<C-d>",
scroll_up = "<C-u>",
},
conflict = {
accept_incoming = "<leader>ct",
Expand Down
4 changes: 4 additions & 0 deletions lua/codediff/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ M.defaults = {
fold_toggle_recursive = "zA", -- Toggle fold recursively
fold_open_all = "zR", -- Open all folds in tree
fold_close_all = "zM", -- Close all folds in tree
scroll_down = "<C-d>", -- Scroll diff panes down half a page
scroll_up = "<C-u>", -- Scroll diff panes up half a page
},
history = {
select = "<CR>", -- Select commit/file or toggle expand
Expand All @@ -161,6 +163,8 @@ M.defaults = {
fold_toggle_recursive = "zA",
fold_open_all = "zR",
fold_close_all = "zM",
scroll_down = "<C-d>",
scroll_up = "<C-u>",
},
-- Conflict mode keymaps (only active in merge conflict views)
conflict = {
Expand Down
4 changes: 4 additions & 0 deletions lua/codediff/ui/keymap_help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ local function build_sections(keymaps, is_bound, shape)
{ ekm.fold_toggle_recursive, "Toggle fold recursively" },
{ ekm.fold_open_all, "Open all folds" },
{ ekm.fold_close_all, "Close all folds" },
{ ekm.scroll_down, "Scroll diff down" },
{ ekm.scroll_up, "Scroll diff up" },
}, is_bound)
)
end
Expand All @@ -131,6 +133,8 @@ local function build_sections(keymaps, is_bound, shape)
{ hkm.fold_toggle_recursive, "Toggle fold recursively" },
{ hkm.fold_open_all, "Open all folds" },
{ hkm.fold_close_all, "Close all folds" },
{ hkm.scroll_down, "Scroll diff down" },
{ hkm.scroll_up, "Scroll diff up" },
}, is_bound)
)
end
Expand Down
16 changes: 15 additions & 1 deletion lua/codediff/ui/lib/tree_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function M.get_root_node(tree)
return find_root(node)
end

-- Setup all fold-related keymaps on a tree buffer.
-- Setup fold and diff-scroll keymaps on a tree buffer.
-- @param opts table { tree, keymaps, bufnr, tabpage }
function M.setup_fold_keymaps(opts)
local tree = opts.tree
Expand Down Expand Up @@ -138,6 +138,18 @@ function M.setup_fold_keymaps(opts)
update_tree_view(root)
end

local function scroll_diff(key)
return function()
local orig_win, mod_win = require("codediff.ui.lifecycle").get_windows(tabpage)
local win = mod_win or orig_win
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_call(win, function()
vim.cmd("normal! " .. vim.keycode(key))
end)
end
end
end

local fold_bindings = {
{ key = "fold_open", fn = fold_open, desc = "Open fold" },
{ key = "fold_open_recursive", fn = fold_open_recursive, desc = "Open fold recursively" },
Expand All @@ -147,6 +159,8 @@ function M.setup_fold_keymaps(opts)
{ key = "fold_toggle_recursive", fn = fold_toggle_recursive, desc = "Toggle fold recursively" },
{ key = "fold_open_all", fn = fold_open_all, desc = "Open all folds" },
{ key = "fold_close_all", fn = fold_close_all, desc = "Close all folds" },
{ key = "scroll_down", fn = scroll_diff("<C-d>"), desc = "Scroll diff down" },
{ key = "scroll_up", fn = scroll_diff("<C-u>"), desc = "Scroll diff up" },
}
local map_options = { noremap = true, silent = true, nowait = true }
local lifecycle = require("codediff.ui.lifecycle")
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/keymap_matrix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ modified o ih Hunk textobject
modified v ih Hunk textobject
panel n - Toggle stage/unstage
panel n <2-LeftMouse> Select file
panel n <C-D> Scroll diff down
panel n <C-U> Scroll diff up
panel n <CR> Select/toggle entry
panel n K Show full path
panel n R Refresh explorer
Expand Down Expand Up @@ -188,6 +190,8 @@ modified o ih Hunk textobject
modified v ih Hunk textobject
panel n - Toggle stage/unstage
panel n <2-LeftMouse> Select file
panel n <C-D> Scroll diff down
panel n <C-U> Scroll diff up
panel n <CR> Select/toggle entry
panel n K Show full path
panel n R Refresh explorer
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/keymap/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ local EXPECT = {
fold_toggle_recursive = { "explorer" },
fold_open_all = { "explorer" },
fold_close_all = { "explorer" },
scroll_down = { "explorer" },
scroll_up = { "explorer" },
},
history = {
select = { "history" },
Expand All @@ -116,6 +118,8 @@ local EXPECT = {
fold_toggle_recursive = { "history" },
fold_open_all = { "history" },
fold_close_all = { "history" },
scroll_down = { "history" },
scroll_up = { "history" },
},
conflict = {
accept_incoming = { "conflict" },
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/ui/explorer/scroll_independence_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,24 @@ describe("explorer scroll independence", function()
assert.are.equal(mod_top, topline(session.modified_win), "modified diff pane must stay put while the explorer scrolls")
assert.are.equal(orig_top, topline(session.original_win), "original diff pane must stay put while the explorer scrolls")
end)

it("scrolls the diff panes from the explorer with scroll_down/scroll_up", function()
repo = busy_repo()
local _, session, explorer = open_codediff_and_wait(repo)
vim.api.nvim_win_set_cursor(session.modified_win, { 1, 0 })
vim.api.nvim_set_current_win(explorer.winid)
local explorer_top = topline(explorer.winid)

vim.api.nvim_feedkeys(vim.keycode("<C-d><C-d>"), "x", false)
vim.cmd("redraw")
local mod_top = topline(session.modified_win)
assert.is_true(mod_top > 1, "modified diff pane should scroll down")
assert.is_true(topline(session.original_win) > 1, "original diff pane should follow via scrollbind")
assert.are.equal(explorer.winid, vim.api.nvim_get_current_win(), "focus must stay in the explorer")
assert.are.equal(explorer_top, topline(explorer.winid), "explorer must not scroll")

vim.api.nvim_feedkeys(vim.keycode("<C-u>"), "x", false)
vim.cmd("redraw")
assert.is_true(topline(session.modified_win) < mod_top, "modified diff pane should scroll back up")
end)
end)