From ad373b8243279833f5033b89c102499f706bee4d Mon Sep 17 00:00:00 2001 From: przepompownia Date: Wed, 26 Mar 2025 02:10:53 +0100 Subject: [PATCH] feat: allow cycle within single commit on prev/next file --- lua/diffview/actions.lua | 2 ++ lua/diffview/config.lua | 4 ++++ .../views/file_history/file_history_panel.lua | 20 ++++++++++--------- .../views/file_history/file_history_view.lua | 8 ++++---- .../scene/views/file_history/listeners.lua | 6 ++++++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lua/diffview/actions.lua b/lua/diffview/actions.lua index 6b89e1fa..021a2140 100644 --- a/lua/diffview/actions.lua +++ b/lua/diffview/actions.lua @@ -634,7 +634,9 @@ local action_names = { "restore_entry", "select_entry", "select_next_entry", + "select_next_entry_in_commit", "select_prev_entry", + "select_prev_entry_in_commit", "select_first_entry", "select_last_entry", "select_next_commit", diff --git a/lua/diffview/config.lua b/lua/diffview/config.lua index f1f6d0f1..f7d31a1a 100644 --- a/lua/diffview/config.lua +++ b/lua/diffview/config.lua @@ -124,6 +124,8 @@ M.defaults = { -- tabpage is a Diffview. { "n", "", actions.select_next_entry, { desc = "Open the diff for the next file" } }, { "n", "", actions.select_prev_entry, { desc = "Open the diff for the previous file" } }, + { "n", "]k", actions.select_next_entry_in_commit, { desc = "Open the diff for the next file within commit" } }, + { "n", "[k", actions.select_prev_entry_in_commit, { desc = "Open the diff for the previous file within commit" } }, { "n", "[F", actions.select_first_entry, { desc = "Open the diff for the first file" } }, { "n", "]F", actions.select_last_entry, { desc = "Open the diff for the last file" } }, { "n", "gf", actions.goto_file_edit, { desc = "Open the file in the previous tabpage" } }, @@ -236,6 +238,8 @@ M.defaults = { { "n", "", actions.scroll_view(0.25), { desc = "Scroll the view down" } }, { "n", "", actions.select_next_entry, { desc = "Open the diff for the next file" } }, { "n", "", actions.select_prev_entry, { desc = "Open the diff for the previous file" } }, + { "n", "]k", actions.select_next_entry_in_commit, { desc = "Open the diff for the next file within commit" } }, + { "n", "[k", actions.select_prev_entry_in_commit, { desc = "Open the diff for the previous file within commit" } }, { "n", "[F", actions.select_first_entry, { desc = "Open the diff for the first file" } }, { "n", "]F", actions.select_last_entry, { desc = "Open the diff for the last file" } }, { "n", "gf", actions.goto_file_edit, { desc = "Open the file in the previous tabpage" } }, diff --git a/lua/diffview/scene/views/file_history/file_history_panel.lua b/lua/diffview/scene/views/file_history/file_history_panel.lua index 2240452f..38a2a28c 100644 --- a/lua/diffview/scene/views/file_history/file_history_panel.lua +++ b/lua/diffview/scene/views/file_history/file_history_panel.lua @@ -386,11 +386,13 @@ end ---@param offset integer ---@return LogEntry? ---@return FileEntry? -function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset) +function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset, cycle_in_commit) local cur_entry = self.entries[entry_idx] - if cur_entry.files[file_idx + offset] then - return cur_entry, cur_entry.files[file_idx + offset] + local entryPos = cycle_in_commit and ((file_idx + offset - 1) % #cur_entry.files + 1) or (file_idx + offset) + + if cur_entry.files[entryPos] then + return cur_entry, cur_entry.files[entryPos] end local sign = utils.sign(offset) @@ -410,7 +412,7 @@ function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset) end end -function FileHistoryPanel:set_file_by_offset(offset) +function FileHistoryPanel:set_file_by_offset(offset, cycle_in_commit) if self:num_items() == 0 then return end local entry, file = self.cur_item[1], self.cur_item[2] @@ -425,7 +427,7 @@ function FileHistoryPanel:set_file_by_offset(offset) local file_idx = utils.vec_indexof(entry.files, file) if entry_idx ~= -1 and file_idx ~= -1 then - local next_entry, next_file = self:_get_entry_by_file_offset(entry_idx, file_idx, offset) + local next_entry, next_file = self:_get_entry_by_file_offset(entry_idx, file_idx, offset, cycle_in_commit) self:set_cur_item({ next_entry, next_file }) if next_entry ~= entry then @@ -440,12 +442,12 @@ function FileHistoryPanel:set_file_by_offset(offset) end end -function FileHistoryPanel:prev_file() - return self:set_file_by_offset(-vim.v.count1) +function FileHistoryPanel:prev_file(cycle_in_commit) + return self:set_file_by_offset(-vim.v.count1, cycle_in_commit) end -function FileHistoryPanel:next_file() - return self:set_file_by_offset(vim.v.count1) +function FileHistoryPanel:next_file(cycle_in_commit) + return self:set_file_by_offset(vim.v.count1, cycle_in_commit) end ---@param item LogEntry|FileEntry diff --git a/lua/diffview/scene/views/file_history/file_history_view.lua b/lua/diffview/scene/views/file_history/file_history_view.lua index 1710b942..498241c3 100644 --- a/lua/diffview/scene/views/file_history/file_history_view.lua +++ b/lua/diffview/scene/views/file_history/file_history_view.lua @@ -124,13 +124,13 @@ FileHistoryView._set_file = async.void(function(self, file) end end) -function FileHistoryView:next_item() +function FileHistoryView:next_item(cycle_in_commit) self:ensure_layout() if self:file_safeguard() then return end if self.panel:num_items() > 1 or self.nulled then - local cur = self.panel:next_file() + local cur = self.panel:next_file(cycle_in_commit) if cur then self.panel:highlight_item(cur) @@ -142,13 +142,13 @@ function FileHistoryView:next_item() end end -function FileHistoryView:prev_item() +function FileHistoryView:prev_item(cycle_in_commit) self:ensure_layout() if self:file_safeguard() then return end if self.panel:num_items() > 1 or self.nulled then - local cur = self.panel:prev_file() + local cur = self.panel:prev_file(cycle_in_commit) if cur then self.panel:highlight_item(cur) diff --git a/lua/diffview/scene/views/file_history/listeners.lua b/lua/diffview/scene/views/file_history/listeners.lua index c10a4ff3..b68df076 100644 --- a/lua/diffview/scene/views/file_history/listeners.lua +++ b/lua/diffview/scene/views/file_history/listeners.lua @@ -59,6 +59,12 @@ return function(view) select_prev_entry = function() view:prev_item() end, + select_next_entry_in_commit = function() + view:next_item(true) + end, + select_prev_entry_in_commit = function() + view:prev_item(true) + end, select_first_entry = function() local entry = view.panel.entries[1] if entry and #entry.files > 0 then