Skip to content

Commit cd8e3cc

Browse files
committed
fix: preserve panel cursor position when switching tabs (sindrets#457)
Save the file panel cursor position on tab_leave and restore it on tab_enter. This complements the fold state preservation from sindrets#582.
1 parent ed13882 commit cd8e3cc

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lua/diffview/scene/views/diff/listeners.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,29 @@ return function(view)
1919
view:set_file(file, false, true)
2020
end
2121

22+
-- Restore panel cursor position.
23+
if view.panel_cursor and view.panel:is_open() then
24+
local winid = view.panel:get_winid()
25+
if winid and api.nvim_win_is_valid(winid) then
26+
pcall(api.nvim_win_set_cursor, winid, view.panel_cursor)
27+
end
28+
end
29+
2230
if view.ready then
2331
view:update_files()
2432
end
2533
end,
2634
tab_leave = function()
2735
local file = view.panel.cur_file
2836

37+
-- Save panel cursor position.
38+
if view.panel:is_open() then
39+
local winid = view.panel:get_winid()
40+
if winid and api.nvim_win_is_valid(winid) then
41+
view.panel_cursor = api.nvim_win_get_cursor(winid)
42+
end
43+
end
44+
2945
if file then
3046
file.layout:detach_files()
3147
end

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
88
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
99
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
1010

11+
local api = vim.api
1112
local await = async.await
1213

1314
---@param view FileHistoryView
@@ -18,10 +19,26 @@ return function(view)
1819
if file then
1920
view:set_file(file)
2021
end
22+
23+
-- Restore panel cursor position.
24+
if view.panel_cursor and view.panel:is_open() then
25+
local winid = view.panel:get_winid()
26+
if winid and api.nvim_win_is_valid(winid) then
27+
pcall(api.nvim_win_set_cursor, winid, view.panel_cursor)
28+
end
29+
end
2130
end,
2231
tab_leave = function()
2332
local file = view.panel.cur_item[2]
2433

34+
-- Save panel cursor position.
35+
if view.panel:is_open() then
36+
local winid = view.panel:get_winid()
37+
if winid and api.nvim_win_is_valid(winid) then
38+
view.panel_cursor = api.nvim_win_get_cursor(winid)
39+
end
40+
end
41+
2542
if file then
2643
file.layout:detach_files()
2744
end

0 commit comments

Comments
 (0)