nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Floating nvim-tree does not respect config on startup

stentibbing opened this issue · comments

Description

When opening a folder with "nvim ." and while having nvim-tree configured to float in a window, nvim-tree opens full screen, not respecting open_win_config. Furthermore, trying to close the initial fullscreen nvim-tree with NvimTreeToggle produces an error:

Error executing Lua callback: ...cal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/view.lua:225: Expected Lua number stack traceback: [C]: in function 'nvim_win_is_valid' ...cal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/view.lua:225: in function 'close' ...cal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/view.lua:235: in function 'close_this_tab_only' ...cal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/view.lua:248: in function 'close' ...lazy/nvim-tree.lua/lua/nvim-tree/actions/tree/toggle.lua:45: in function 'toggle' ...share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/commands.lua:36: in function <...share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/commands.lua:35>

Neovim version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1692716794

Operating system and version

Windows 11

Windows variant

WSL2 Ubuntu

nvim-tree version

#ddd1d6e, v1.3.0

Clean room replication

Install nvim-tree with Lazy, using the following configuration:

return {
	"nvim-tree/nvim-tree.lua",
	version = "*",
	lazy = false,
	dependencies = {
		"nvim-tree/nvim-web-devicons",
	},
	config = function()
		require("nvim-tree").setup({
			disable_netrw = true,
			hijack_netrw = false,
			view = {
				relativenumber = true,
				float = {
					enable = true,
					open_win_config = function()
						local screen_w = vim.opt.columns:get()
						local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
						local window_w = screen_w * 0.5
						local window_h = screen_h * 0.8
						local window_w_int = math.floor(window_w)
						local window_h_int = math.floor(window_h)
						local center_x = (screen_w - window_w) / 2
						local center_y = ((vim.opt.lines:get() - window_h) / 2) - vim.opt.cmdheight:get()
						return {
							title = " NvimTree ",
							title_pos = "center",
							border = "rounded",
							relative = "editor",
							row = center_y,
							col = center_x,
							width = window_w_int,
							height = window_h_int,
						}
					end,
				},
			},
		})
	end,
}

Steps to reproduce

  1. nvim .
  2. :NvimTreeToggle

Expected behavior

NvimTree should open in a floating window and close on NvimTreeToggle

Actual behavior

NvimTree opens full screen and :NvimTreeToggle produces error

Screenshot 2024-04-17 103007

Lazy is problematic and results in issues like these.

Please attempt a clean room replication so that we may rule out lazy.

@stentibbing , I was able to get the floating preview with lazy using this config. Hope it helps

return {
  "nvim-tree/nvim-tree.lua",
  version = "*",
  lazy = false,
  dependencies = {
    "nvim-tree/nvim-web-devicons",
  },
  config = function()
    local HEIGHT_RATIO = 0.8  -- You can change this
    local WIDTH_RATIO = 0.5   -- You can change this too
    require("nvim-tree").setup({
    view = {
      float = {
        enable = true,
          open_win_config = function() 
            local screen_w = vim.opt.columns:get()
            local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
            local window_w = screen_w * WIDTH_RATIO
            local window_h = screen_h * HEIGHT_RATIO
            local window_w_int = math.floor(window_w)
            local window_h_int = math.floor(window_h)
            local center_x = (screen_w - window_w) / 2
            local center_y = ((vim.opt.lines:get() - window_h) / 2 - vim.opt.cmdheight:get())
            return {
              border = 'rounded',
              relative = 'editor',
              row = center_y,
              col = center_x,
              width = window_w_int,
              height = window_h_int,
            }
            end,
          },
          width = function()
            return math.floor(vim.opt.columns:get() * WIDTH_RATIO)
          end,
        },
    })
    local keymap = vim.keymap

    keymap.set("n", "<leader>ee", "<cmd>NvimTreeToggle<CR>", { desc = "Toggle file explorer"})
    keymap.set("n", "<leader>ef", "<cmd>NvimTreeFindFileToggle<CR>", { desc = "Toggle file explorer on current file" })
    keymap.set("n", "<leader>ec", "<cmd>NvimTreeCollapse<CR>", { desc = "Collapse folders in explorer" })
    
  end,
}

Nice one @hoax3 !

Does that work for you @stentibbing ?