local is_neotree_focused = function() -- Get our current buffer number local bufnr = vim.api.nvim_get_current_buf and vim.api.nvim_get_current_buf() or vim.fn.bufnr() -- Get all the available sources in neo-tree for _, source in ipairs(require("neo-tree").config.sources) do -- Get each sources state local state = require("neo-tree.sources.manager").get_state(source) -- Check if the source has a state, if the state has a buffer and if the buffer is our current buffer if state and state.bufnr and state.bufnr == bufnr then return true end end return false end local function is_neotree_open() local manager = require("neo-tree.sources.manager") local renderer = require("neo-tree.ui.renderer") local success, state = pcall(manager.get_state, "filesystem") if not success then return false end local window_exists = renderer.window_exists(state) return window_exists end -- If the tree is currently open and focused, then close it. If it's not open, or it's open -- but not currently focused, it is made focused. -- Effectively prevents the need to close and re-open it to gain focus of the tree. local function toggle() local cmd = require("neo-tree.command") if is_neotree_open() and is_neotree_focused() then cmd.execute({ action = "close", }) else cmd.execute({ action = "focus", source = "filesystem", position = "left", }) end end return { "nvim-neo-tree/neo-tree.nvim", branch = "v3.x", dependencies = { "nvim-lua/plenary.nvim", "nvim-tree/nvim-web-devicons", "MunifTanjim/nui.nvim", }, config = function() --vim.keymap.set("n", "", ":Neotree filesystem toggle left") vim.keymap.set("n", "", toggle, {}) end, }