nvim-tree / nvim-tree.lua

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

Cannot override empty buffer with new opened file buffer #615

Closed BlueDruddigon closed 2 years ago

BlueDruddigon commented 3 years ago

At the latest master branch, an option in https://github.com/kyazdani42/nvim-tree.lua#setup

let g:nvim_tree_respect_buf_cwd = 1 "0 by default, will change cwd of nvim-tree to that of new buffer's when opening nvim-tree. does not work for me.

I'm using LunarVim from LunarVim at the latest rolling branch. I'm not sure if this issue belongs to nvim-tree or LunarVim. Please help me fix this.

kyazdani42 commented 3 years ago

what do you mean by does not work ? seems lunarvim enable the feature by default when the tree is active

BlueDruddigon commented 3 years ago

what do you mean by does not work ? seems lunarvim enable the feature by default when the tree is active

I'm sorry. Let me explain the issue. in lunarvim, I've set these options image because I don't want the dashboard. But when lunarvim opened, an empty buffer open and then, when I open a file, the buffer didn't be replaced by a new file. In the dc630d1 commit the newly opened file will replace the empty buffer above. I don't know if it was a bug.

kyazdani42 commented 3 years ago

Hello ! i still don't understand. how this relates to nvim_tree_respect_buf_cwd ? EDIT: what do you mean by the buffer didn't be replaced by a new file ?

BlueDruddigon commented 2 years ago

Hello ! i still don't understand. how this relates to nvim_tree_respect_buf_cwd ? EDIT: what do you mean by the buffer didn't be replaced by a new file ?

On open nvim by calling lvim command, there is an empty buffer that appeared! I'm using option like auto_open = 1 for nvim-tree plugin. So the thing I expected to get is when I open a file in the nvim-tree buffer, that file bufffer will replace in the empty buffer at the beginning. At the latest commit of your repo, when I open a file from nvim-tree, the buffer is still there! Just like this: image

Here is my lua config for nvim-tree:

lvim.builtin.nvimtree = {
    active = true,
    on_config_done = nil,
    setup = {
      open_on_setup = false,
      auto_close = true,
      open_on_tab = false,
      update_focused_file = {
        enable = true,
      },
      diagnostics = {
        enable = true,
        icons = {
          hint = "",
          info = "",
          warning = "",
          error = "",
        },
      },
      view = {
        width = 30,
        side = "left",
        auto_resize = false,
        mappings = {
          custom_only = false,
        },
      },
    },
    show_icons = {
      git = 1,
      folders = 1,
      files = 1,
      folder_arrows = 1,
      tree_width = 30,
    },
    ignore = { ".git", "node_modules", ".cache" },
    quit_on_open = 0,
    hide_dotfiles = 1,
    git_hl = 1,
    root_folder_modifier = ":t",
    allow_resize = 1,
    auto_ignore_ft = { "startify", "dashboard" },
    icons = {
      default = "",
      symlink = "",
      git = {
        unstaged = "",
        staged = "S",
        unmerged = "",
        renamed = "➜",
        deleted = "",
        untracked = "U",
        ignored = "◌",
      },
      folder = {
        default = "",
        open = "",
        empty = "",
        empty_open = "",
        symlink = "",
      },
    },
  }

setup function with project.nvim of ahmedkhalf plugin

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
  local g = vim.g

  for opt, val in pairs(lvim.builtin.nvimtree) do
    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.disable_netrw = false
    lvim.builtin.nvimtree.setup.hijack_netrw = false
    vim.g.netrw_banner = false
  end

  local tree_cb = nvim_tree_config.nvim_tree_callback

  if not lvim.builtin.nvimtree.setup.view.mappings.list then
    lvim.builtin.nvimtree.setup.view.mappings.list = {
      { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
      { key = "h", cb = tree_cb "close_node" },
      { key = "v", cb = tree_cb "vsplit" },
    }
  end

  lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }

  local tree_view = require "nvim-tree.view"

  -- Add nvim_tree open callback
  local open = tree_view.open
  tree_view.open = function()
    M.on_open()
    open()
  end

  vim.cmd "au WinClosed * lua require('lvim.core.nvimtree').on_close()"

  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

Hope for your reply soon! EDIT: I've found this issue same as my problem! That link contains a gif file for evidence.

EDIT 2: I've found the problem cause when I set the open_on_setup=true, the nvim-tree will appear when I call lvim command. And by defaults, the active buffer was the nvimtree, not the empty buffer! The diff between master and dc630d1 was the active buffer when calling editor. So how can I solve this problem by setting some of the configurations of the master branch of nvimtree?

kyazdani42 commented 2 years ago

hi @BlueDruddigon, sorry for the late answer, is this issue still relevant ?

ghost commented 2 years ago

yes @kyazdani42 i opened the same issue yesterday the current behavior is that nvim-tree takes the focus [cursor] when i open a file, i want nvim tree to detect that i opend a file and give the focus [cursor] to the file that i want to edit instead of keeping the focus, here is an example that is been used by nerd tree i want this from nvim-tree [sorry for my bad english im not a native american]

[ " Start NERDTree. If a file is specified, move the cursor to its window. autocmd StdinReadPre let s:std_in=1 autocmd VimEnter NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif ]

I Want Nvim-Tree To change Focus when i open a file From this

pic2

To This automatically

1pic2

Here Is my configs https://github.com/micro-aries/configs/blob/main/nvim/configs/vim-tree.lua

kyazdani42 commented 2 years ago

@micro-aries i don't think so. @BlueDruddigon pinging again.

alex-courtis commented 2 years ago

This should be resolved. 76d4ed5 added hijack_unnamed_buffer_when_opening and there has been a lot of improvements around that area since then.

Please reopen if you experience any further issues.