Skip to content

feat: auto close buffer opened by diff_view #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ require("diffview").setup({
use_icons = true, -- Requires nvim-web-devicons
show_help_hints = true, -- Show hints for how to open the help panel
watch_index = true, -- Update views and index buffers when the git index changes.
delete_buffer_on_diff_exit = false, -- Delete file buffers opened by diffview that were not opened
icons = { -- Only applies when use_icons is true.
folder_closed = "",
folder_open = "",
Expand Down
1 change: 1 addition & 0 deletions doc/diffview_defaults.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DEFAULT CONFIG *diffview.defaults*
use_icons = true, -- Requires nvim-web-devicons
show_help_hints = true, -- Show hints for how to open the help panel
watch_index = true, -- Update views and index buffers when the git index changes.
delete_buffer_on_diff_exit = false, -- Delete file buffers opened by diffview that were not opened
icons = { -- Only applies when use_icons is true.
folder_closed = "",
folder_open = "",
Expand Down
1 change: 1 addition & 0 deletions lua/diffview/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ M.defaults = {
use_icons = true,
show_help_hints = true,
watch_index = true,
delete_buffer_on_diff_exit = false,
icons = {
folder_closed = "",
folder_open = "",
Expand Down
4 changes: 4 additions & 0 deletions lua/diffview/scene/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function View:init_layout() oop.abstract_stub() end
---@abstract
function View:post_open() oop.abstract_stub() end

---@abstract
function View:pre_open() oop.abstract_stub() end

---@diagnostic enable unused-local

---View constructor
Expand All @@ -62,6 +65,7 @@ function View:init(opt)
end

function View:open()
self:pre_open()
vim.cmd("tab split")
self.tabpage = api.nvim_get_current_tabpage()
self:init_layout()
Expand Down
15 changes: 15 additions & 0 deletions lua/diffview/scene/views/diff/diff_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ function DiffView:init(opt)
self.attached_bufs = {}
self.emitter:on("file_open_post", utils.bind(self.file_open_post, self))
self.valid = true
self.buffers_pre_open = {}
end

function DiffView:pre_open()
self.buffers_pre_open = vim.api.nvim_list_bufs()
end

function DiffView:post_open()
Expand Down Expand Up @@ -170,6 +175,16 @@ end

---@override
function DiffView:close()
if config.get_config().delete_buffer_on_diff_exit then
-- delete buffers that were opened by the view
local buffers_pre_close = vim.api.nvim_list_bufs()
for _, buf in ipairs(buffers_pre_close) do
if not vim.tbl_contains(self.buffers_pre_open, buf) then
vim.api.nvim_buf_delete(buf, { force = false})
end
end
end

if not self.closing:check() then
self.closing:send()

Expand Down
3 changes: 3 additions & 0 deletions lua/diffview/scene/views/file_history/file_history_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function FileHistoryView:init(opt)
self.valid = true
end

function FileHistoryView:pre_open()
end

function FileHistoryView:post_open()
self.commit_log_panel = CommitLogPanel(self.adapter, {
name = ("diffview://%s/log/%d/%s"):format(self.adapter.ctx.dir, self.tabpage, "commit_log"),
Expand Down