Skip to content
Draft
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 lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local config = {
},
add_blank_line_at_top = false, -- Add a blank line at the top of the tree.
auto_clean_after_session_restore = false, -- Automatically clean up broken neo-tree buffers saved in sessions
auto_restore_session_experimental = false, -- Try best to restore neo-tree from invalid buffers restored by auto-session plugins
close_if_last_window = false, -- Close Neo-tree if it is the last window left in the tab
default_source = "filesystem", -- you can choose a specific source `last` here which indicates the last used source
enable_diagnostics = true,
Expand Down
19 changes: 16 additions & 3 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -687,12 +687,25 @@ M.merge_config = function(user_config, is_auto_config)
manager.redraw(source_name)
end

if M.config.auto_clean_after_session_restore then
require("neo-tree.ui.renderer").clean_invalid_neotree_buffers(false)
if M.config.auto_clean_after_session_restore or M.config.auto_restore_session_experimental then
events.subscribe({
event = events.VIM_AFTER_SESSION_LOAD,
handler = function()
require("neo-tree.ui.renderer").clean_invalid_neotree_buffers(true)
local invalid_tree = require("neo-tree.ui.renderer").clean_invalid_neotree_buffers(true)
if M.config.auto_restore_session_experimental and invalid_tree then
for _, v in ipairs(M.config.sources) do
if v == invalid_tree then
vim.schedule(function ()
require("neo-tree.command").execute({
source = invalid_tree,
dir = vim.fn.getcwd(),
action = "show",
})
end)
return
end
end
end
end,
})
end
Expand Down
10 changes: 8 additions & 2 deletions lua/neo-tree/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ M.clean_invalid_neotree_buffers = function(force)
return
end

---@type string?
local invalid_tree_name = nil
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local bufname = vim.fn.bufname(buf)
local is_neotree_buffer = string.match(bufname, "neo%-tree [^ ]+ %[%d+]")
local neotree_buffer_name = string.match(bufname, "neo%-tree ([^ ]+) %[%d+]")
local is_valid_neotree, _ = pcall(vim.api.nvim_buf_get_var, buf, "neo_tree_source")
if is_neotree_buffer and not is_valid_neotree then
if neotree_buffer_name and not is_valid_neotree then
vim.api.nvim_buf_delete(buf, { force = true })
if invalid_tree_name ~= "filesystem" then -- prioritize filesystem source as that is most wanted
invalid_tree_name = neotree_buffer_name
end
end
end
cleaned_up = true
return invalid_tree_name
end

local resize_monitor_timer = nil
Expand Down