nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua
Other
6.78k stars 600 forks source link

Floating nvim-tree does not respect config on startup #2749

Open stentibbing opened 2 months ago

stentibbing commented 2 months ago

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

alex-courtis commented 2 months ago

Lazy is problematic and results in issues like these.

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

hoax3 commented 2 months ago

@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,
}
alex-courtis commented 1 month ago

Nice one @hoax3 !

Does that work for you @stentibbing ?

jo-pouradier commented 1 month ago

same issue here. In float mode, on start up nvim-tree should open an empty buffer and render the floating window on top. Which is not the case, it open a wide nvim-tree.

stentibbing commented 1 month ago

Hi @alex-courtis !

Sorry for the very late response. I did "clean-room replication" and the same problem still exists. Here's the clean room configuration:

`vim.g.loaded_netrw = 1 vim.g.loaded_netrwPlugin = 1

vim.cmd([[set runtimepath=$VIMRUNTIME]]) vim.cmd([[set packpath=/tmp/nvt-min/site]]) local package_root = "/tmp/nvt-min/site/pack" local install_path = package_root .. "/packer/start/packer.nvim" local function load_plugins() require("packer").startup({ { "wbthomason/packer.nvim", "nvim-tree/nvim-tree.lua", "nvim-tree/nvim-web-devicons", -- ADD PLUGINS THAT ARE NECESSARY FOR REPRODUCING THE ISSUE }, config = { package_root = package_root, compile_path = install_path .. "/plugin/packer_compiled.lua", display = { non_interactive = true }, }, }) end if vim.fn.isdirectory(install_path) == 0 then print("Installing nvim-tree and dependencies.") vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }) end load_plugins() require("packer").sync() vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]) vim.opt.termguicolors = true vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE NECESSARY FOR REPRODUCING THE ISSUE _G.setup = 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

-- UNCOMMENT this block for diagnostics issues, substituting pattern and cmd as appropriate. -- Requires diagnostics.enable = true in setup. --[[ vim.api.nvim_create_autocmd("FileType", { pattern = "lua", callback = function() vim.lsp.start { cmd = { "lua-language-server" } } end, }) ]] ` So if you open nvim with "nvim -nu init.lua" and then use the command :NvimTreeOpen, everything works as expected. The tree is floating. BUT, if you open current folder with a command "nvim . -nu init.lua" and then do :NvimTreeOpen, the tree is full screen.

jo-pouradier commented 1 month ago

I tried a few options and hijack_netrw = false does the trick when you use nvim . but nvim /path/anywhere/ does not set the new working directory. And then using nvim open the default nvim page and nvim-tree open a wide tree.

alex-courtis commented 4 weeks ago

Thank you, replicated.

Behaves as expected:

It seems that *_netrw are not behaving when netrw is not loaded.

alex-courtis commented 4 weeks ago

That's working as intended: :help nvim-tree.hijack_directories.enable directs nvim-tree to open inside the current buffer if it's a directory.

Same behaviour when not using floating.

Please try this:

  require("nvim-tree").setup({
    -- disable_netrw = true,
    -- hijack_netrw = true,
    hijack_directories = {
      enable = false,
    },
    ---
jo-pouradier commented 4 weeks ago

I do want the hijack_directories, the option:

hijack_directories = {
      enable = true,
      auto_open = false,
    },

does not open the tree on startup but then NvimTreeTroggle still opens this wide tree.

It looks like nvim-tree overrides the unnamed buffer to this wide tree instead of the unnamed buffer + the floating window.

alex-courtis commented 4 weeks ago

It looks like nvim-tree overrides the unnamed buffer to this wide tree instead of the unnamed buffer + the floating window.

It's actually not an unnamed buffer, it's a buffer with the directory .. Enter :ls! to see it. mkdir foo; nvim -nu nvt-min foo might make it a bit clearer.

The only purpose of hijack_directories and hijack_unnamed_buffer_when_opening is directing the tree to take the place of directory and unnamed buffers.

What's your use case / desired functionality here? If you're looking for startup behaviour you need to add that yourself, see wiki: Open At Startup for rationale and recipes.

jo-pouradier commented 4 weeks ago

Thanks for the explanation, didnt see the wiki.

I made a recording to clarify, in this setup up I have these options:

      hijack_unnamed_buffer_when_opening = true,
      -- hijack_netrw = false,
      disable_netrw = true, -- disable :Explore
      hijack_directories = {
        enable = true,
        auto_open = false,
      },

When opening the tree in a No Name or directory buffer, the options of float are not used:

[video]

1-800-jono commented 3 weeks ago

I'm currently facing the same issue.

alex-courtis commented 3 weeks ago

When opening the tree in a No Name or directory buffer, the options of float are not used.

To clarify: does setting hijack_directories.enable false result in the desired behaviour? If so, we'll document it.

We don't change default behaviour like this as it will break existing users who exect and rely on it.

jo-pouradier commented 2 weeks ago

Yes when using nvim . because I have the cwd and the root dir, but not when using nvim /whatever/path I have . as root dir and not the path given.

alex-courtis commented 23 hours ago

Yes when using nvim . because I have the cwd and the root dir, but not when using nvim /whatever/path I have . as root dir and not the path given.

Sorry @jo-pouradier for the late reply, however I'm just not able to replicate this, with absolute or relative paths or ..

You could go with the nuclear option as I do: wipe directory buffers on startup: https://github.com/alex-courtis/arch/blob/4fba2fc937a60934764bf3a1bf8a46a92c77f87a/config/nvim/lua/amc/init/dirs.lua#L5

Edit: see "new window" in the wiki, which wipes before opening: https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup#open-for-directories-and-change-neovims-directory

Unless you have some use for directory buffers they should be removed as they just get in the way.

Updating the wiki...