Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Neotree automatically resizes splits on close #1605

Closed
3 tasks done
Muizzyranking opened this issue Nov 23, 2024 · 1 comment
Closed
3 tasks done

BUG: Neotree automatically resizes splits on close #1605

Muizzyranking opened this issue Nov 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Muizzyranking
Copy link

Did you check docs and existing issues?

  • I have read all the docs.
  • I have searched the existing issues.
  • I have searched the existing discussions.

Neovim Version (nvim -v)

NVIM v0.10.2

Operating System / Version

Fedora 40

Describe the Bug

I do not know if this is a bug per say or maybe I missed it from the docs, but I do not want neotree to resize splits on close.
When I close neotree, it resizes splits to make them equal, I do not want that as I could be working with multiple splits in specific sizes and I would have to resize after closing the splits and this forces me to use neotree float or oil most of the time as I do not want my splits to be automatically reiszed on neotree close.

Screenshots, Traceback

No response

Steps to Reproduce

  1. Open neovim
  2. Make splits (vertical)
  3. Resize splits
  4. Open neotree
  5. Close neotree

Expected Behavior

I expect my splits to stay as they are and not be automatically resized when neotree closes. I expect the state of the window should be restored, I have tried it manually with events and I cant get it to work that is why I am here.

Your Configuration

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")
vim.g.mapleader = " "
local set = vim.keymap.set
set({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
set({ "n", "x" }, "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
set({ "n" }, "<C-h>", "<C-w>h", { desc = "Go to left window", remap = true })
set({ "n" }, "<C-j>", "<C-w>j", { desc = "Go to lower window", remap = true })
set({ "n" }, "<C-k>", "<C-w>k", { desc = "Go to upper window", remap = true })
set({ "n" }, "<C-l>", "<C-w>l", { desc = "Go to right window", remap = true })

set("n", "<leader>ww", "<C-W>p", { desc = "Other Window", remap = true })
set("n", "<leader>wd", "<C-W>c", { desc = "Delete Window", remap = true })
set("n", "<leader>w-", "<C-W>s", { desc = "Split Window Below", remap = true })
set("n", "<leader>w|", "<C-W>v", { desc = "Split Window Right", remap = true })
set("n", "<leader>wn", "<C-W>n", { desc = "Split Window Right", remap = true })
set("n", "<leader>wo", "<C-W>o", { desc = "Close other windows", remap = true })
-- Resize window using <ctrl> arrow keys
set("n", "<C-Up>", "<cmd>resize +2<cr>", { desc = "Increase Window Height" })
set("n", "<C-Down>", "<cmd>resize -2<cr>", { desc = "Decrease Window Height" })
set("n", "<C-Left>", "<cmd>vertical resize -2<cr>", { desc = "Decrease Window Width" })
set("n", "<C-Right>", "<cmd>vertical resize +2<cr>", { desc = "Increase Window Width" })

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
	"folke/tokyonight.nvim",
	-- add any other plugins here
}

local neotree_config = {
	"nvim-neo-tree/neo-tree.nvim",
	dependencies = {
		"nvim-lua/plenary.nvim",
		"MunifTanjim/nui.nvim",
	},
	keys = {
		{
			"<leader>E",
			"<cmd>Neotree toggle<cr>",
			desc = "File Explorer",
		},
		{
			"<leader>e",
			function()
				require("neo-tree.command").execute({
					toggle = true,
					reveal_force_cwd = true,
				})
			end,
			desc = "File Explorer",
		},
	},
	opts = {
		open_files_do_not_replace_types = { "terminal", "Trouble", "trouble", "qf", "Outline" },
		sources = { "filesystem", "buffers", "git_status" },
		close_if_last_window = true,
		popup_border_style = "single",
		enable_git_status = true,
		enable_modified_markers = true,
		enable_diagnostics = true,
		sort_case_insensitive = true,
		default_component_configs = {
			indent = {
				with_markers = true,
				with_expanders = true,
			},
			modified = {
				symbol = "",
				highlight = "NeoTreeModified",
			},
		},
		window = {
			position = "right",
			width = 40,
			mappings = {
				["l"] = "open",
				["h"] = "close_node",
				["<space>"] = "none",
				["Y"] = {
					function(state)
						local node = state.tree:get_node()
						local path = node:get_id()
						vim.fn.setreg("+", path, "c")
					end,
					desc = "Copy Path to Clipboard",
				},
				["O"] = {
					function(state)
						require("lazy.util").open(state.tree:get_node().path, { system = true })
					end,
					desc = "Open with System Application",
				},
				["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = false } },
				["<C-b>"] = { "scroll_preview", config = { direction = 10 } },
				["<C-f>"] = { "scroll_preview", config = { direction = -10 } },
			},
		},
		filesystem = {
			use_libuv_file_watcher = true,
			bind_to_cwd = false,
			follow_current_file = { enabled = true },
			filtered_items = {
				hide_dotfiles = true,
				hide_gitignored = true,
				hide_by_name = {
					"node_modules",
				},
				never_show = {
					".DS_Store",
					"thumbs.db",
				},
				always_show_by_pattern = {
					".zsh*",
				},
			},
		},
	},
	config = function(_, opts)
		require("neo-tree").setup(opts)
		vim.api.nvim_create_autocmd("TermClose", {
			pattern = "*lazygit",
			callback = function()
				if package.loaded["neo-tree.sources.git_status"] then
					require("neo-tree.sources.git_status").refresh()
				end
			end,
		})
	end,
}
table.insert(plugins, neotree_config)
require("lazy").setup(plugins, {
	root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
@Muizzyranking Muizzyranking added the bug Something isn't working label Nov 23, 2024
@Muizzyranking
Copy link
Author

for anyone who stumbled across this, this is not a neotree issue, it is a neovim settings that automatically resizes splits to equal sizes when closed. Check :h equalalways.

set noequalalways fixes this issue but it disables <c-w>= to equalize all splits. What I did was to save the window layout before neotree opens and then restore the layout after neotree closes

for anyone interested, here is what I did

return {
  "nvim-neo-tree/neo-tree.nvim",
  opts =  {
       -- your config here
      event_handlers = {
        -- save layout before opening neotree
        {
          event = "neo_tree_window_before_open",
          handler = function()
            -- vim.cmd("set noequalalways")
            local layout = {}
            for _, win in ipairs(vim.api.nvim_list_wins()) do
              layout[win] = {
                height = vim.api.nvim_win_get_height(win),
                width = vim.api.nvim_win_get_width(win),
              }
            end
            vim._neotree_layout = layout
          end,
        },
        -- restore layout after closing neotree
        {
          event = "neo_tree_window_after_close",
          handler = function()
            if vim._neotree_layout then
              for win, dims in pairs(vim._neotree_layout) do
                if vim.api.nvim_win_is_valid(win) then
                  pcall(vim.api.nvim_win_set_height, win, dims.height)
                  pcall(vim.api.nvim_win_set_width, win, dims.width)
                end
              end
            end
          end,
        },
      },
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant