Skip to content

Commit 318ce58

Browse files
przepompowniadlyongemallo
authored andcommitted
feat(file-history): cycle within commit on prev/next (sindrets#566)
1 parent 0875d9c commit 318ce58

5 files changed

Lines changed: 27 additions & 13 deletions

File tree

lua/diffview/actions.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ local action_names = {
763763
"restore_entry",
764764
"select_entry",
765765
"select_next_entry",
766+
"select_next_entry_in_commit",
766767
"select_prev_entry",
768+
"select_prev_entry_in_commit",
767769
"select_first_entry",
768770
"select_last_entry",
769771
"select_next_commit",

lua/diffview/config.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ M.defaults = {
136136
-- tabpage is a Diffview.
137137
{ "n", "<tab>", actions.select_next_entry, { desc = "Open the diff for the next file" } },
138138
{ "n", "<s-tab>", actions.select_prev_entry, { desc = "Open the diff for the previous file" } },
139+
{ "n", "]k", actions.select_next_entry_in_commit, { desc = "Open the diff for the next file within commit" } },
140+
{ "n", "[k", actions.select_prev_entry_in_commit, { desc = "Open the diff for the previous file within commit" } },
139141
{ "n", "[F", actions.select_first_entry, { desc = "Open the diff for the first file" } },
140142
{ "n", "]F", actions.select_last_entry, { desc = "Open the diff for the last file" } },
141143
{ "n", "gf", actions.goto_file_edit, { desc = "Open the file in the previous tabpage" } },
@@ -253,6 +255,8 @@ M.defaults = {
253255
{ "n", "<c-f>", actions.scroll_view(0.25), { desc = "Scroll the view down" } },
254256
{ "n", "<tab>", actions.select_next_entry, { desc = "Open the diff for the next file" } },
255257
{ "n", "<s-tab>", actions.select_prev_entry, { desc = "Open the diff for the previous file" } },
258+
{ "n", "]k", actions.select_next_entry_in_commit, { desc = "Open the diff for the next file within commit" } },
259+
{ "n", "[k", actions.select_prev_entry_in_commit, { desc = "Open the diff for the previous file within commit" } },
256260
{ "n", "[F", actions.select_first_entry, { desc = "Open the diff for the first file" } },
257261
{ "n", "]F", actions.select_last_entry, { desc = "Open the diff for the last file" } },
258262
{ "n", "gf", actions.goto_file_edit, { desc = "Open the file in the previous tabpage" } },

lua/diffview/scene/views/file_history/file_history_panel.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,13 @@ end
386386
---@param offset integer
387387
---@return LogEntry?
388388
---@return FileEntry?
389-
function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset)
389+
function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset, cycle_in_commit)
390390
local cur_entry = self.entries[entry_idx]
391391

392-
if cur_entry.files[file_idx + offset] then
393-
return cur_entry, cur_entry.files[file_idx + offset]
392+
local entryPos = cycle_in_commit and ((file_idx + offset - 1) % #cur_entry.files + 1) or (file_idx + offset)
393+
394+
if cur_entry.files[entryPos] then
395+
return cur_entry, cur_entry.files[entryPos]
394396
end
395397

396398
local sign = utils.sign(offset)
@@ -410,7 +412,7 @@ function FileHistoryPanel:_get_entry_by_file_offset(entry_idx, file_idx, offset)
410412
end
411413
end
412414

413-
function FileHistoryPanel:set_file_by_offset(offset)
415+
function FileHistoryPanel:set_file_by_offset(offset, cycle_in_commit)
414416
if self:num_items() == 0 then return end
415417

416418
local entry, file = self.cur_item[1], self.cur_item[2]
@@ -425,7 +427,7 @@ function FileHistoryPanel:set_file_by_offset(offset)
425427
local file_idx = utils.vec_indexof(entry.files, file)
426428

427429
if entry_idx ~= -1 and file_idx ~= -1 then
428-
local next_entry, next_file = self:_get_entry_by_file_offset(entry_idx, file_idx, offset)
430+
local next_entry, next_file = self:_get_entry_by_file_offset(entry_idx, file_idx, offset, cycle_in_commit)
429431
self:set_cur_item({ next_entry, next_file })
430432

431433
if next_entry ~= entry then
@@ -440,12 +442,12 @@ function FileHistoryPanel:set_file_by_offset(offset)
440442
end
441443
end
442444

443-
function FileHistoryPanel:prev_file()
444-
return self:set_file_by_offset(-vim.v.count1)
445+
function FileHistoryPanel:prev_file(cycle_in_commit)
446+
return self:set_file_by_offset(-vim.v.count1, cycle_in_commit)
445447
end
446448

447-
function FileHistoryPanel:next_file()
448-
return self:set_file_by_offset(vim.v.count1)
449+
function FileHistoryPanel:next_file(cycle_in_commit)
450+
return self:set_file_by_offset(vim.v.count1, cycle_in_commit)
449451
end
450452

451453
---@param item LogEntry|FileEntry

lua/diffview/scene/views/file_history/file_history_view.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ FileHistoryView._set_file = async.void(function(self, file)
124124
end
125125
end)
126126

127-
function FileHistoryView:next_item()
127+
function FileHistoryView:next_item(cycle_in_commit)
128128
self:ensure_layout()
129129

130130
if self:file_safeguard() then return end
131131

132132
if self.panel:num_items() > 1 or self.nulled then
133-
local cur = self.panel:next_file()
133+
local cur = self.panel:next_file(cycle_in_commit)
134134

135135
if cur then
136136
self.panel:highlight_item(cur)
@@ -142,13 +142,13 @@ function FileHistoryView:next_item()
142142
end
143143
end
144144

145-
function FileHistoryView:prev_item()
145+
function FileHistoryView:prev_item(cycle_in_commit)
146146
self:ensure_layout()
147147

148148
if self:file_safeguard() then return end
149149

150150
if self.panel:num_items() > 1 or self.nulled then
151-
local cur = self.panel:prev_file()
151+
local cur = self.panel:prev_file(cycle_in_commit)
152152

153153
if cur then
154154
self.panel:highlight_item(cur)

lua/diffview/scene/views/file_history/listeners.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ return function(view)
9595
select_prev_entry = function()
9696
view:prev_item()
9797
end,
98+
select_next_entry_in_commit = function()
99+
view:next_item(true)
100+
end,
101+
select_prev_entry_in_commit = function()
102+
view:prev_item(true)
103+
end,
98104
select_first_entry = function()
99105
local entry = view.panel.entries[1]
100106
if entry and #entry.files > 0 then

0 commit comments

Comments
 (0)