Skip to content

Commit cc01af0

Browse files
committed
fix: Don't delete buffers that are in use in other views (fixes #291)
1 parent 7de7334 commit cc01af0

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lua/diffview/events.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949

5050
---Subscribe to a given event.
5151
---@param event_id any Event identifier.
52-
---@param callback fun(event: Event, ...)
52+
---@param callback fun(event: Event, ...): boolean?
5353
function EventEmitter:on(event_id, callback)
5454
if not self.event_map[event_id] then
5555
self.event_map[event_id] = {}
@@ -66,7 +66,7 @@ end
6666

6767
---Subscribe a one-shot listener to a given event.
6868
---@param event_id any Event identifier.
69-
---@param callback fun(event: Event, ...)
69+
---@param callback fun(event: Event, ...): boolean?
7070
function EventEmitter:once(event_id, callback)
7171
if not self.event_map[event_id] then
7272
self.event_map[event_id] = {}
@@ -87,7 +87,7 @@ function EventEmitter:once(event_id, callback)
8787
end
8888

8989
---Add a new any-listener, subscribed to all events.
90-
---@param callback fun(event: Event, ...)
90+
---@param callback fun(event: Event, ...): boolean?
9191
function EventEmitter:on_any(callback)
9292
table.insert(self.any_listeners, 1, {
9393
type = "any",
@@ -99,7 +99,7 @@ function EventEmitter:on_any(callback)
9999
end
100100

101101
---Add a new one-shot any-listener, subscribed to all events.
102-
---@param callback fun(event: Event, ...)
102+
---@param callback fun(event: Event, ...): boolean?
103103
function EventEmitter:once_any(callback)
104104
local emitted = false
105105

lua/diffview/lib.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ function M.get_prev_non_view_tabpage()
194194
end
195195
end
196196

197+
---@param bufnr integer
198+
---@return boolean
199+
function M.is_buf_in_use(bufnr)
200+
for _, view in ipairs(M.views) do
201+
if view:instanceof(StandardView.__get()) then
202+
---@cast view StandardView
203+
for _, file in ipairs(view.cur_entry.layout:files()) do
204+
if file:is_valid() and file.bufnr == bufnr then
205+
return true
206+
end
207+
end
208+
end
209+
end
210+
211+
return false
212+
end
213+
197214
function M.update_colors()
198215
for _, view in ipairs(M.views) do
199216
if view:instanceof(StandardView.__get()) then

lua/diffview/vcs/file.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local GitRev = lazy.access("diffview.vcs.adapters.git.rev", "GitRev") ---@type G
55
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
66
local async = lazy.require("plenary.async") ---@module "plenary.async"
77
local config = lazy.require("diffview.config") ---@module "diffview.config"
8+
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
89
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
910

1011
local pl = lazy.access(utils, "path") ---@type PathLib|LazyModule
@@ -107,7 +108,7 @@ function File:destroy(force)
107108
self.active = false
108109
self:detach_buffer()
109110

110-
if force or self.rev.type ~= RevType.LOCAL then
111+
if force or self.rev.type ~= RevType.LOCAL and not lib.is_buf_in_use(self.bufnr) then
111112
File.safe_delete_buf(self.bufnr)
112113
end
113114
end
@@ -366,7 +367,11 @@ end
366367
function File:dispose_buffer()
367368
if self.bufnr and api.nvim_buf_is_loaded(self.bufnr) then
368369
self:detach_buffer()
369-
File.safe_delete_buf(self.bufnr)
370+
371+
if not lib.is_buf_in_use(self.bufnr) then
372+
File.safe_delete_buf(self.bufnr)
373+
end
374+
370375
self.bufnr = nil
371376
end
372377
end

0 commit comments

Comments
 (0)