Skip to content

Commit 0ea25e2

Browse files
committed
fix: prevent duplicate [No Name] buffer on netrw hijack, fixes #305
1 parent 44310cf commit 0ea25e2

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lua/neo-tree/setup/netrw.lua

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,37 @@ M.hijack = function()
4343
end
4444

4545
-- record where we are now
46-
local should_open_current = hijack_behavior == "open_current" or get_position("filesystem") == "current"
46+
local pos = get_position("filesystem")
47+
local should_open_current = hijack_behavior == "open_current" or pos == "current"
4748
local winid = vim.api.nvim_get_current_win()
4849
local dir_bufnr = vim.api.nvim_get_current_buf()
4950

50-
-- We will want to replace the "directory" buffer with either the "alternate"
51-
-- buffer or a new blank one.
52-
local replace_with_bufnr = vim.fn.bufnr("#")
53-
if replace_with_bufnr > 0 then
54-
if vim.api.nvim_buf_get_option(replace_with_bufnr, "filetype") == "neo-tree" then
55-
replace_with_bufnr = -1
56-
end
57-
end
58-
if not should_open_current then
59-
if replace_with_bufnr == dir_bufnr or replace_with_bufnr < 1 then
60-
replace_with_bufnr = vim.api.nvim_create_buf(true, false)
61-
end
62-
end
63-
if replace_with_bufnr > 0 then
64-
pcall(vim.api.nvim_win_set_buf, winid, replace_with_bufnr)
65-
end
66-
local remove_dir_buf = vim.schedule_wrap(function()
67-
pcall(vim.api.nvim_buf_delete, dir_bufnr, { force = true })
68-
end)
69-
7051
-- Now actually open the tree, with a very quick debounce because this may be
7152
-- called multiple times in quick succession.
7253
utils.debounce("hijack_netrw_" .. winid, function()
54+
-- We will want to replace the "directory" buffer with either the "alternate"
55+
-- buffer or a new blank one.
56+
local replace_with_bufnr = vim.fn.bufnr("#")
57+
if replace_with_bufnr > 0 then
58+
if vim.api.nvim_buf_get_option(replace_with_bufnr, "filetype") == "neo-tree" then
59+
replace_with_bufnr = -1
60+
end
61+
end
62+
if not should_open_current then
63+
if replace_with_bufnr == dir_bufnr or replace_with_bufnr < 1 then
64+
replace_with_bufnr = vim.api.nvim_create_buf(true, false)
65+
log.trace("Created new buffer for netrw hijack", replace_with_bufnr)
66+
end
67+
end
68+
if replace_with_bufnr > 0 then
69+
log.trace("Replacing buffer in netrw hijack", replace_with_bufnr)
70+
pcall(vim.api.nvim_win_set_buf, winid, replace_with_bufnr)
71+
end
72+
local remove_dir_buf = vim.schedule_wrap(function()
73+
log.trace("Deleting buffer in netrw hijack", dir_bufnr)
74+
pcall(vim.api.nvim_buf_delete, dir_bufnr, { force = true })
75+
end)
76+
7377
local state
7478
if should_open_current then
7579
log.debug("hijack_netrw: opening current")

0 commit comments

Comments
 (0)