nvim-tree/nvim-tree.lua

nvim-tree.lua destroys split pattern, and now show the opened buffer

Opened this issue · 4 comments

Description

When I open nvim and split, and then toggle with current_window=true, and then open a new file from nvim-tree. The split pattern is destroyed, and the opened new file is not on the top.

Neovim version

NVIM v0.10.4
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

Operating system and version

Linux WorkStation 6.11.0-19-generic #19~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 17 11:51:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Windows variant

No response

nvim-tree version

head

Clean room replication

This is my init.lua:

-- download and install and load plugins --
-- install lazy.vim if it does not exist
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

-- install plugins
require("lazy").setup({
    {
        "nvim-tree/nvim-tree.lua",
        version = "*",
        lazy = false,
        dependencies = {
            "nvim-tree/nvim-web-devicons",
        },
    },
})

local nvim_tree_api = require("nvim-tree.api")
require('nvim-tree').setup({
    update_focused_file = {
        enable = true,
    },
    actions = {
        open_file = {
            quit_on_open = true,
            window_picker = {enable = false},
        }
    },
   git = {enable = false},
})


-----------------
-- key bindings --
-----------------
-- set <leader>
vim.g.mapleader = ","

vim.keymap.set('n', '<leader>o', ':only<cr>', opts)
vim.keymap.set('i', '<C-x>', '<esc>', opts)
vim.keymap.set('n', '<leader><cr>', ':noh<cr>', opts)
vim.keymap.set('n', 'ss', ':set spell!<cr>', opts)


-- Hint: see `:h vim.map.set()`
-- Better window navigation
vim.keymap.set('n', '<C-h>', '<C-w>h', opts)
vim.keymap.set('n', '<C-j>', '<C-w>j', opts)
vim.keymap.set('n', '<C-k>', '<C-w>k', opts)
vim.keymap.set('n', '<C-l>', '<C-w>l', opts)

-- Resize with arrows
vim.keymap.set('n', '<C-Up>', ':resize -2<CR>', opts)
vim.keymap.set('n', '<C-Down>', ':resize +2<CR>', opts)
vim.keymap.set('n', '<C-Left>', ':vertical resize -2<CR>', opts)
vim.keymap.set('n', '<C-Right>', ':vertical resize +2<CR>', opts)

-- nvim-tree shortcuts
vim.keymap.set("n", "<leader>n",
    function()
        nvim_tree_api.tree.toggle({
            path = "<args>", current_window = true,
            focus = true, find_file = false, update_root = false })
    end,
    { noremap = true })

Steps to reproduce

  1. $ nvim -u init.lua a b
  2. run :vs, here it should be like [a | b]
  3. n
  4. choose a file(such as a "c"), and press enter

Expected behavior

The split pattern should keep, and the new opened file should be in the split where nvim-tree is toggled. Here it is [c | b]

Actual behavior

The split pattern is gone, that is only a [a] is here. And the new opened file of "c" is not on top of the shown buffers.

Next time please follow the instructions when creating the replicator:

Minimal(!) configuration necessary to reproduce the issue. If not provided it is very unlikely that the nvim-tree team will be able to address your issue. See wiki: Clean Room Replication for instructions and paste the contents of your /tmp/nvt-min.lua here. Please do NOT post a configuration that uses other plugin managers such as lazy, see wiki: Lazy Loading

On simplifying the toggle I am able to reproduce via:

nvim -nu nvt-min.lua a b
:vs
:n
,n
select c
<only window with a is open>
vim.keymap.set("n", "<leader>n",
  function()
    nvim_tree_api.tree.toggle(
      {
        current_window = true,
      }
    )
  end,
  { noremap = true }
)

Please note from #3080 that path = "<args>", is incorrect and may yield unexpected results.

Minimal reproducer, both quit-on-open and window-picker-disabled are required. Update-focused-file irrelevant.

Any thoughts on this one @GCrispino ? We definitely tested the _no_window_picker APIs #3054

echo a > a
echo b > b
echo c > c
nvim -nu nvt-min.lua a b
:vs
:n
<space>a
<CR> on c
buffer a is shown, not c
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

local api = require("nvim-tree.api")

vim.keymap.set("n", "<space>a", function()
  api.tree.toggle({ current_window = true })
end, { noremap = true })

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup({
    actions = {
      open_file = {
        quit_on_open = true,
        window_picker = {
          enable = false
        },
      }
    },
  })
end

I'm finding similar issues, in that nvim-tree is trying to do too much when closing the tree in this situation as well.

For example, say you open the tree in the current window via current_window = true opt. If instead of editing a file, you just try to close the tree (whether that be via tree.close func or something more vanilla like :bd, the window split is destroyed in the process.

I believe this is due to an Event.TreeClose event handler in the plugin.

If the tree is opened via current_window = true, would it not be fair to assume that the user wants to manage their own window, and that nvim-tree should just run in a buffer, and be completely hands-off in regards to window handling when both editing a file or closing the tree?

PS. Just want to thank you for all your work on nvim-tree! 🙏

If the tree is opened via current_window = true, would it not be fair to assume that the user wants to manage their own window, and that nvim-tree should just run in a buffer, and be completely hands-off in regards to window handling when both editing a file or closing the tree?

That's quite reasonable, however difficult to achieve right now due to the current code structure. Following #2255 it will be far simpler and we can do it.