Skip to content

feat(file_history): allow cycle within single commit on prev/next file #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions lua/diffview/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions lua/diffview/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ M.defaults = {
-- tabpage is a Diffview.
{ "n", "<tab>", actions.select_next_entry, { desc = "Open the diff for the next file" } },
{ "n", "<s-tab>", 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" } },
Expand Down Expand Up @@ -236,6 +238,8 @@ M.defaults = {
{ "n", "<c-f>", actions.scroll_view(0.25), { desc = "Scroll the view down" } },
{ "n", "<tab>", actions.select_next_entry, { desc = "Open the diff for the next file" } },
{ "n", "<s-tab>", 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" } },
Expand Down
20 changes: 11 additions & 9 deletions lua/diffview/scene/views/file_history/file_history_panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lua/diffview/scene/views/file_history/file_history_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lua/diffview/scene/views/file_history/listeners.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down