Skip to content

Commit 6f8c499

Browse files
authored
fix(filesystem): ensure replacemnt for deleted buffer is a real file (#1176)
fixes #1174
1 parent 7e2a3ca commit 6f8c499

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lua/neo-tree/sources/filesystem/lib/fs_actions.lua

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,36 @@ local Path = require("plenary").path
1616

1717
local M = {}
1818

19+
local function find_replacement_buffer(for_buf)
20+
local bufs = vim.api.nvim_list_bufs()
21+
22+
-- make sure the alternate buffer is at the top of the list
23+
local alt = vim.fn.bufnr("#")
24+
if alt ~= -1 and alt ~= for_buf then
25+
table.insert(bufs, 1, alt)
26+
end
27+
28+
-- find the first valid real file buffer
29+
for _, buf in ipairs(bufs) do
30+
if buf ~= for_buf then
31+
local is_valid = vim.api.nvim_buf_is_valid(buf)
32+
if is_valid then
33+
local buftype = vim.api.nvim_buf_get_option(buf, "buftype")
34+
if buftype == "" then
35+
return buf
36+
end
37+
end
38+
end
39+
end
40+
return -1
41+
end
42+
1943
local function clear_buffer(path)
2044
local buf = utils.find_buffer_by_name(path)
2145
if buf < 1 then
2246
return
2347
end
24-
local alt = vim.fn.bufnr("#")
48+
local alt = find_replacement_buffer(buf)
2549
-- Check all windows to see if they are using the buffer
2650
for _, win in ipairs(vim.api.nvim_list_wins()) do
2751
if vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_buf(win) == buf then

0 commit comments

Comments
 (0)