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

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

Open
stentibbing opened this issue Apr 17, 2024 · 17 comments
Open

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

stentibbing opened this issue Apr 17, 2024 · 17 comments
Labels
awaiting feedback bug Something isn't working

Comments

@stentibbing
Copy link

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

@stentibbing stentibbing added the bug Something isn't working label Apr 17, 2024
@alex-courtis
Copy link
Member

Lazy is problematic and results in issues like these.

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

@hoax3
Copy link

hoax3 commented May 9, 2024

@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
Copy link
Member

Nice one @hoax3 !

Does that work for you @stentibbing ?

@jo-pouradier
Copy link

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
Copy link
Author

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
Copy link

jo-pouradier commented Jun 7, 2024

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
Copy link
Member

Thank you, replicated.

Behaves as expected:

  • !disable_netrw and !hijack_netrw and netrw not loaded
  • netrw loaded
    • !disable_netrw and !hijack_netrw
    • disable_netrw and !hijack_netrw
    • disable_netrw and hijack_netrw
    • !disable_netrw and hijack_netrw

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

@alex-courtis
Copy link
Member

alex-courtis commented Jun 8, 2024

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
Copy link

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
Copy link
Member

alex-courtis commented Jun 9, 2024

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
Copy link

jo-pouradier commented Jun 9, 2024

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

This comment was marked as duplicate.

@alex-courtis
Copy link
Member

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
Copy link

jo-pouradier commented Jun 17, 2024

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
Copy link
Member

alex-courtis commented Jul 7, 2024

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...

@nrydanov
Copy link

nrydanov commented Aug 4, 2024

Hello! I tried to use new window option, as you mentioned above, but it has no effect and I have similar issue as topic starter — I just get non floating NvimTree_1, which breaks NvimTreeToggle command.

Maybe there's some updates on this issue?

@alex-courtis
Copy link
Member

which breaks NvimTreeToggle command.

That's no good! Does it throw an exception or just do nothing?

Could you please raise a new bug report with a reproducer as it sounds like it might be something a bit different. Hopefully it gets us to the root of this problem.

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

No branches or pull requests

6 participants