Skip to content
Merged
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
6 changes: 6 additions & 0 deletions lua/neo-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ M.ensure_config = function()
return M.config
end

---A performance-focused version for checking a specific key of a config while trying not to do expensive setup work
---@return neotree.Config.Base
M.peek_config = function()
return new_user_config or M.ensure_config()
end

---@param ignore_filetypes string[]?
---@param ignore_winfixbuf boolean?
M.get_prior_window = function(ignore_filetypes, ignore_winfixbuf)
Expand Down
65 changes: 1 addition & 64 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,77 +272,14 @@ M.buffer_enter_event = function()
end
end

---@type boolean?
local curwin_floating = nil
M.win_enter_event = function()
local win_id = vim.api.nvim_get_current_win()
local previous_window_floating = curwin_floating
curwin_floating = utils.is_floating(win_id)
if curwin_floating then
if utils.is_floating(win_id) then
return
end

-- if the new win is not a floating window, make sure all neo-tree floats are closed
manager.close_all("float")

if M.config.close_if_last_window then
local tabid = vim.api.nvim_get_current_tabpage()
local wins = utils.prior_windows[tabid]
local prior_exists = utils.truthy(wins)
local non_floating_wins = vim.tbl_filter(function(win)
return not utils.is_floating(win)
end, vim.api.nvim_tabpage_list_wins(tabid))
local win_count = #non_floating_wins
log.trace("checking if last window")
log.trace("prior window exists = ", prior_exists)
log.trace("win_count: ", win_count)
if
prior_exists
and win_count == 1
and vim.o.filetype == "neo-tree"
-- if neo-tree managed to stay open long enough for users to open popups, (e.g. other windows automatically closed
-- w/o triggering new WinEnter events), we shouldn't close even if it is technically the last window
and not previous_window_floating
then
local position = vim.b[0].neo_tree_position
local source = vim.b[0].neo_tree_source
-- close_if_last_window just doesn't make sense for a split style
if position ~= "current" then
log.trace("last window, closing")
local state = require("neo-tree.sources.manager").get_state(source)
if state == nil then
return
end
local mod = utils.get_opened_buffers()
log.debug("close_if_last_window, modified files found: ", vim.inspect(mod))
for filename, buf_info in pairs(mod) do
if buf_info.modified then
local buf_name, message
if vim.startswith(filename, "[No Name]#") then
buf_name = string.sub(filename, 11)
message =
"Cannot close because an unnamed buffer is modified. Please save or discard this file."
else
buf_name = filename
message =
"Cannot close because one of the files is modified. Please save or discard changes."
end
log.trace("close_if_last_window, showing unnamed modified buffer: ", filename)
vim.schedule(function()
log.warn(message)
vim.cmd("rightbelow vertical split")
vim.api.nvim_win_set_width(win_id, state.window.width or 40)
vim.cmd("b " .. buf_name)
end)
return
end
end
vim.cmd("q!")
return
end
end
end

if vim.o.filetype == "neo-tree" then
local _, position = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_position")
if position == "current" then
Expand Down
75 changes: 74 additions & 1 deletion plugin/neo-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ local function try_netrw_hijack(path)
return require("neo-tree.setup.netrw").hijack()
end

local augroup = vim.api.nvim_create_augroup("NeoTree_NetrwDeferred", { clear = true })
local augroup = vim.api.nvim_create_augroup("NeoTree", { clear = true })

-- lazy load until bufenter/netrw hijack
vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = augroup,
desc = "Lazy-load until bufenter/opened dir",
callback = function(args)
return vim.g.neotree_watching_bufenter == 1 or try_netrw_hijack(args.file)
end,
})

-- track window order
vim.api.nvim_create_autocmd({ "WinEnter" }, {
group = augroup,
desc = "Track prior windows for opening intuitiveness",
callback = function(ev)
local win = vim.api.nvim_get_current_win()
local utils = require("neo-tree.utils")
Expand Down Expand Up @@ -73,11 +76,81 @@ vim.api.nvim_create_autocmd({ "WinEnter" }, {

-- setup session loading
vim.api.nvim_create_autocmd("SessionLoadPost", {
group = augroup,
desc = "Session loading",
callback = function()
if require("neo-tree").ensure_config().auto_clean_after_session_restore then
require("neo-tree.ui.renderer").clean_invalid_neotree_buffers(true)
end
end,
})

vim.api.nvim_create_autocmd("WinClosed", {
group = augroup,
desc = "close_if_last_window autocmd",
callback = function(args)
local closing_win = tonumber(args.match)
local visible_winids = vim.api.nvim_tabpage_list_wins(0)
local other_panes = {}
local utils = require("neo-tree.utils")
for _, winid in ipairs(visible_winids) do
if not utils.is_floating(winid) and winid ~= closing_win then
other_panes[#other_panes + 1] = winid
end
end

if #other_panes ~= 1 then
return
end

local remaining_pane = other_panes[1]
local remaining_buf = vim.api.nvim_win_get_buf(remaining_pane)

if vim.bo[remaining_buf].filetype ~= "neo-tree" then
return
end

local position = vim.b[remaining_buf].neo_tree_position
local source = vim.b[remaining_buf].neo_tree_source
-- close_if_last_window just doesn't make sense for a split style
if position == "current" then
return
end

local log = require("neo-tree.log")
log.trace("last window, closing")
local state = require("neo-tree.sources.manager").get_state(source)
if not state then
return
end
if not require("neo-tree").ensure_config().close_if_last_window then
return
end
local mod = utils.get_opened_buffers()
log.debug("close_if_last_window, modified files found: ", vim.inspect(mod))
for filename, buf_info in pairs(mod) do
if buf_info.modified then
local buf_name, message
if vim.startswith(filename, "[No Name]#") then
buf_name = string.sub(filename, 11)
message =
"Cannot close because an unnamed buffer is modified. Please save or discard this file."
else
buf_name = filename
message =
"Cannot close because one of the files is modified. Please save or discard changes."
end
log.trace("close_if_last_window, showing unnamed modified buffer: ", filename)
vim.schedule(function()
log.warn(message)
vim.cmd("rightbelow vertical split")
vim.api.nvim_win_set_width(0, state.window.width or 40)
vim.cmd("b " .. buf_name)
end)
return
end
end
end,
})

vim.g.loaded_neo_tree = 1
Loading