nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua
Other
7.17k stars 609 forks source link

nvim-tree window moves to the right after opening a new file #1103

Closed senicko closed 2 years ago

senicko commented 2 years ago

Description

After entering neovim inside a directory and choosing a file, file explorer moves to the right (the file opens to the left of the file explorer). After reopening the nvim-tree window with :NvimTreeClose, :NvimTreeOpen everything works as expected.

I updated my plugins yesterday (I didn't update it for a few months), and I didn't have that issue before.

I am still experiencing the issue with only nvim-tree installed, so it is not caused by another plugin.

Is it because of recent config changes?

Neovim version

NVIM v0.6.1
Build type: Release
LuaJIT 2.1.0-beta3

Operating system and version

macOS 12.2.1

nvim-tree version

7b0ebf8

Steps to reproduce

Open neovim with nvim-tree installed inside a directory and open a file using nvim-tree.

Expected behavior

After opening neovim in directory and choosing a file it should open to the right of an explorer.

Actual behavior

The file opens on the left (nvim-tree moves to the right), but after reopening nvim-tree it works as expected.

https://user-images.githubusercontent.com/25861704/159904811-4ccb2c8a-a028-4ae2-8b40-5d77176c39e2.mov

Minimal config

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",
      "kyazdani42/nvim-tree.lua",
      "kyazdani42/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 {
      auto_close = true,
      view = {
        side = "left",
        width = 30,
      },
      actions = {
        open_file = {
          resize_window = true
        }
      }
  }
end
Gonzalo9823 commented 2 years ago

Hi!

I'm having the same problem but the other way around, I have nvim tree configured for the right side and on first open it opens on the left and after closing it and opening it again it opens on the right side.

I'm using LunarVim btw.

Video

https://user-images.githubusercontent.com/5316653/159950255-2b2f4c01-9029-42ed-9016-508f58ebb9db.mov

Config

local M = {}
local Log = require "lvim.core.log"

function M.config()
  lvim.builtin.nvimtree = {
    active = true,
    on_config_done = nil,
    setup = {
      disable_netrw = true,
      hijack_netrw = true,
      open_on_setup = false,
      ignore_buffer_on_setup = false,
      ignore_ft_on_setup = {
        "startify",
        "dashboard",
        "alpha",
      },
      auto_reload_on_write = true,
      hijack_unnamed_buffer_when_opening = false,
      hijack_directories = {
        enable = true,
        auto_open = true,
      },
      update_to_buf_dir = {
        enable = true,
        auto_open = true,
      },
      auto_close = false,
      open_on_tab = false,
      hijack_cursor = false,
      update_cwd = false,
      diagnostics = {
        enable = true,
        icons = {
          hint = "",
          info = "",
          warning = "",
          error = "",
        },
      },
      update_focused_file = {
        enable = true,
        update_cwd = true,
        ignore_list = {},
      },
      system_open = {
        cmd = nil,
        args = {},
      },
      git = {
        enable = true,
        ignore = false,
        timeout = 200,
      },
      view = {
        width = 30,
        height = 30,
        hide_root_folder = false,
        side = "right",
        auto_resize = false,
        mappings = {
          custom_only = false,
          list = {},
        },
        number = false,
        relativenumber = false,
        signcolumn = "yes",
      },
      filters = {
        dotfiles = false,
        custom = { "node_modules", ".cache" },
      },
      trash = {
        cmd = "trash",
        require_confirm = true,
      },
      actions = {
        change_dir = {
          global = false,
        },
        open_file = {
          resize_window = true,
          quit_on_open = false,
        },
        window_picker = {
          enable = false,
          chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
          exclude = {},
        },
      },
    },
    show_icons = {
      git = 1,
      folders = 1,
      files = 1,
      folder_arrows = 1,
    },
    git_hl = 1,
    root_folder_modifier = ":t",
    icons = {
      default = "",
      symlink = "",
      git = {
        unstaged = "",
        staged = "S",
        unmerged = "",
        renamed = "➜",
        deleted = "",
        untracked = "U",
        ignored = "◌",
      },
      folder = {
        default = "",
        open = "",
        empty = "",
        empty_open = "",
        symlink = "",
      },
    },
  }
  lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
end

function M.setup()
  local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
  if not status_ok then
    Log:error "Failed to load nvim-tree.config"
    return
  end

  for opt, val in pairs(lvim.builtin.nvimtree) do
    vim.g["nvim_tree_" .. opt] = val
  end

  -- Implicitly update nvim-tree when project module is active
  if lvim.builtin.project.active then
    lvim.builtin.nvimtree.respect_buf_cwd = 1
    lvim.builtin.nvimtree.setup.update_cwd = true
    lvim.builtin.nvimtree.setup.update_focused_file = { enable = true, update_cwd = true }
  end

  local function telescope_find_files(_)
    require("lvim.core.nvimtree").start_telescope "find_files"
  end
  local function telescope_live_grep(_)
    require("lvim.core.nvimtree").start_telescope "live_grep"
  end

  -- Add useful keymaps
  if #lvim.builtin.nvimtree.setup.view.mappings.list == 0 then
    lvim.builtin.nvimtree.setup.view.mappings.list = {
      { key = { "l", "<CR>", "o" }, action = "edit", mode = "n" },
      { key = "h", action = "close_node" },
      { key = "v", action = "vsplit" },
      { key = "C", action = "cd" },
      { key = "gtf", action = "telescope_find_files", action_cb = telescope_find_files },
      { key = "gtg", action = "telescope_live_grep", action_cb = telescope_live_grep },
    }
  end

  if lvim.builtin.nvimtree.on_config_done then
    lvim.builtin.nvimtree.on_config_done(nvim_tree_config)
  end

  require("nvim-tree").setup(lvim.builtin.nvimtree.setup)
end

function M.start_telescope(telescope_mode)
  local node = require("nvim-tree.lib").get_node_at_cursor()
  local abspath = node.link_to or node.absolute_path
  local is_folder = node.open ~= nil
  local basedir = is_folder and abspath or vim.fn.fnamemodify(abspath, ":h")
  require("telescope.builtin")[telescope_mode] {
    cwd = basedir,
  }
end

return M
baleksa commented 2 years ago

Hi, same problem here.

local tree_cb = require("nvim-tree.config").nvim_tree_callback
require("nvim-tree").setup({ -- Setup nvim file explorer written in Lua
    view = {
        mappings = {
            custom_only = false,
            list = {
                { key = { "l", "<CR>", "o" }, cb = tree_cb("edit") },
                { key = "h", cb = tree_cb("close_node") },
                { key = "v", cb = tree_cb("vsplit") },
            },
        },
    },
    auto_close = true,
    -- diagnostics = {
    --   enable = true,
    --   icons = { error = "", warn = "", hint = "", info = "", },
    -- },
    diagnostics = {
        enable = true,
        icons = {
            hint = "",
            info = "",
            warning = "",
            error = "",
        },
    },
    update_focused_file = {
        enable = true,
        update_cwd = true,
        ignore_list = {},
    },
    update_cwd = true,
    update_to_buf_dir = {
        enable = true,
        auto_open = true,
    },
})
nyan2d commented 2 years ago

Hi! Same here. Even with the default config.

kyazdani42 commented 2 years ago

What is the output of :lua print(require"nvim-tree.view".View.side) ?

kyazdani42 commented 2 years ago

also see https://github.com/kyazdani42/nvim-tree.lua/issues/1072

senicko commented 2 years ago

What is the output of :lua print(require"nvim-tree.view".View.side) ?

left

also see https://github.com/kyazdani42/nvim-tree.lua/issues/1072

Oh, I didn't see this issue. Setting set splitright solves the problem. But as you said in https://github.com/kyazdani42/nvim-tree.lua/issues/1072 it is a global option. resize_window = true does not work on it own.

kyazdani42 commented 2 years ago

indeed it does not. resize does not replace, i thought it did. Doesn't matter, i think this should be documented, i'll add a note in the readme. Closing since it's not an nvim-tree issue :)

orangethewell commented 2 years ago

I'm having the same problem too, resize_window = true doesn't work except if I close and re-open it

xieping5555 commented 2 years ago

did you solve this problem?

kyazdani42 commented 2 years ago

please see :help splitright