Skip to content

Commit 59d556e

Browse files
committed
Merge upstream PR sindrets#477 from sindrets/diffview.nvim
* pr-477: feat: auto close buffer opened by diff_view
2 parents 4516612 + 66107de commit 59d556e

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ require("diffview").setup({
189189
use_icons = true, -- Requires nvim-web-devicons
190190
show_help_hints = true, -- Show hints for how to open the help panel
191191
watch_index = true, -- Update views and index buffers when the git index changes.
192+
delete_buffer_on_diff_exit = false, -- Delete file buffers opened by diffview that were not opened
192193
icons = { -- Only applies when use_icons is true.
193194
folder_closed = "",
194195
folder_open = "",

doc/diffview_defaults.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ DEFAULT CONFIG *diffview.defaults*
1010
use_icons = true, -- Requires nvim-web-devicons
1111
show_help_hints = true, -- Show hints for how to open the help panel
1212
watch_index = true, -- Update views and index buffers when the git index changes.
13+
delete_buffer_on_diff_exit = false, -- Delete file buffers opened by diffview that were not opened
1314
icons = { -- Only applies when use_icons is true.
1415
folder_closed = "",
1516
folder_open = "",

lua/diffview/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ M.defaults = {
4444
use_icons = true,
4545
show_help_hints = true,
4646
watch_index = true,
47+
delete_buffer_on_diff_exit = false,
4748
icons = {
4849
folder_closed = "",
4950
folder_open = "",

lua/diffview/scene/view.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function View:init_layout() oop.abstract_stub() end
3838
---@abstract
3939
function View:post_open() oop.abstract_stub() end
4040

41+
---@abstract
42+
function View:pre_open() oop.abstract_stub() end
43+
4144
---@diagnostic enable unused-local
4245

4346
---View constructor
@@ -62,6 +65,7 @@ function View:init(opt)
6265
end
6366

6467
function View:open()
68+
self:pre_open()
6569
vim.cmd("tab split")
6670
self.tabpage = api.nvim_get_current_tabpage()
6771
self:init_layout()

lua/diffview/scene/views/diff/diff_view.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ function DiffView:init(opt)
7777
self.attached_bufs = {}
7878
self.emitter:on("file_open_post", utils.bind(self.file_open_post, self))
7979
self.valid = true
80+
self.buffers_pre_open = {}
81+
end
82+
83+
function DiffView:pre_open()
84+
self.buffers_pre_open = vim.api.nvim_list_bufs()
8085
end
8186

8287
function DiffView:post_open()
@@ -170,6 +175,16 @@ end
170175

171176
---@override
172177
function DiffView:close()
178+
if config.get_config().delete_buffer_on_diff_exit then
179+
-- delete buffers that were opened by the view
180+
local buffers_pre_close = vim.api.nvim_list_bufs()
181+
for _, buf in ipairs(buffers_pre_close) do
182+
if not vim.tbl_contains(self.buffers_pre_open, buf) then
183+
vim.api.nvim_buf_delete(buf, { force = false})
184+
end
185+
end
186+
end
187+
173188
if not self.closing:check() then
174189
self.closing:send()
175190

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ function FileHistoryView:init(opt)
3939
self.valid = true
4040
end
4141

42+
function FileHistoryView:pre_open()
43+
end
44+
4245
function FileHistoryView:post_open()
4346
self.commit_log_panel = CommitLogPanel(self.adapter, {
4447
name = ("diffview://%s/log/%d/%s"):format(self.adapter.ctx.dir, self.tabpage, "commit_log"),

0 commit comments

Comments
 (0)