Skip to content

Commit

Permalink
feat: Save and restore winopts of diff buffers.
Browse files Browse the repository at this point in the history
(closes #97)
  • Loading branch information
sindrets committed Nov 25, 2021
1 parent 8dd1ad5 commit 00c2f23
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lua/diffview/ui/panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local utils = require("diffview.utils")
local renderer = require("diffview.renderer")
local logger = require("diffview.logger")
local PerfTimer = require("diffview.perf").PerfTimer
local FileEntry = require("diffview.views.file_entry").FileEntry
local api = vim.api
local M = {}
local uid_counter = 0
Expand Down Expand Up @@ -166,6 +167,12 @@ end

function Panel:close()
if self:is_open() then
local num_wins = api.nvim_tabpage_list_wins(api.nvim_win_get_tabpage(self.winid))
if #num_wins == 1 then
-- Ensure that the tabpage doesn't close if the panel is the last window.
vim.cmd("sp")
FileEntry.load_null_buffer(0)
end
api.nvim_win_hide(self.winid)
end
end
Expand Down
10 changes: 10 additions & 0 deletions lua/diffview/views/diff/listeners.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local git = require("diffview.git.utils")
local lib = require("diffview.lib")
local RevType = require("diffview.git.rev").RevType
local Event = require("diffview.events").Event
local FileEntry = require("diffview.views.file_entry").FileEntry
local api = vim.api

local function prepare_goto_file(view)
Expand Down Expand Up @@ -42,6 +43,15 @@ return function(view)
if file then
file:detach_buffers()
end
local cur_winid = api.nvim_get_current_win()
if cur_winid == view.left_winid or cur_winid == view.right_winid then
-- Change the current buffer such that 'restore_winopts()' will work
-- correctly.
FileEntry.load_null_buffer(cur_winid)
end
for _, f in view.panel.files:ipairs() do
f:restore_winopts()
end
end,
buf_write_post = function()
if git.has_local(view.left, view.right) then
Expand Down
39 changes: 39 additions & 0 deletions lua/diffview/views/file_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ local FileEntry = oop.create_class("FileEntry")
---@type integer|nil
FileEntry._null_buffer = nil

---@static
---@type table<integer, table>
FileEntry.winopt_store = {}

---@static
FileEntry.winopts = {
diff = true,
scrollbind = true,
cursorbind = true,
scrollopt = { "ver", "hor", "jump" },
foldmethod = "diff",
foldcolumn = "1",
foldlevel = 0,
Expand Down Expand Up @@ -82,6 +87,7 @@ end
function FileEntry:destroy()
self.active = false
self:detach_buffers()
self:restore_winopts()
for _, bn in ipairs(self.created_bufs) do
FileEntry.safe_delete_buf(bn)
end
Expand Down Expand Up @@ -136,6 +142,9 @@ function FileEntry:load_buffers(git_root, left_winid, right_winid, callback)
else
api.nvim_win_set_buf(sp.winid, sp.bufid)
end
if sp.rev.type == RevType.LOCAL and not FileEntry.winopt_store[sp.bufid] then
FileEntry._save_winopts(sp.bufid, sp.winid)
end
end
FileEntry._update_windows(left_winid, right_winid)
perf:lap("view updated")
Expand Down Expand Up @@ -295,6 +304,14 @@ function FileEntry:compare(other)
return (self.path == other.path and self.status == other.status)
end

function FileEntry:restore_winopts()
for _, bufid in ipairs({ self.left_bufid, self.right_bufid }) do
if bufid then
FileEntry._restore_winopts(bufid)
end
end
end

---@static Get the bufid of the null buffer. Create it if it's not loaded.
---@return integer
function FileEntry._get_null_buffer()
Expand Down Expand Up @@ -412,6 +429,28 @@ function FileEntry.safe_delete_buf(bufid)
pcall(api.nvim_buf_delete, bufid, { force = true })
end

---@static
function FileEntry._save_winopts(bufid, winid)
FileEntry.winopt_store[bufid] = {}
api.nvim_win_call(winid, function()
for option, _ in pairs(FileEntry.winopts) do
FileEntry.winopt_store[bufid][option] = vim.o[option]
end
end)
end

---@static
function FileEntry._restore_winopts(bufid)
if FileEntry.winopt_store[bufid] then
utils.no_win_event_call(function()
vim.cmd("sp")
api.nvim_win_set_buf(0, bufid)
utils.set_local(0, FileEntry.winopt_store[bufid])
api.nvim_win_close(0, true)
end)
end
end

---@static
function FileEntry._update_windows(left_winid, right_winid)
utils.set_local({ left_winid, right_winid }, FileEntry.winopts)
Expand Down
12 changes: 12 additions & 0 deletions lua/diffview/views/file_history/listeners.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local git = require("diffview.git.utils")
local lib = require("diffview.lib")
local RevType = require("diffview.git.rev").RevType
local DiffView = require("diffview.views.diff.diff_view").DiffView
local FileEntry = require("diffview.views.file_entry").FileEntry
local api = vim.api

local function prepare_goto_file(view)
Expand Down Expand Up @@ -38,6 +39,17 @@ return function(view)
if file then
file:detach_buffers()
end
local cur_winid = api.nvim_get_current_win()
if cur_winid == view.left_winid or cur_winid == view.right_winid then
-- Change the current buffer such that 'restore_winopts()' will work
-- correctly.
FileEntry.load_null_buffer(cur_winid)
end
for _, entry in ipairs(view.panel.entries) do
for _, f in ipairs(entry.files) do
f:restore_winopts()
end
end
end,
win_closed = function(winid)
if winid and winid == view.panel.option_panel.winid then
Expand Down

0 comments on commit 00c2f23

Please sign in to comment.