Skip to content

find_file doesn't work when group_empty option is enabled #2154

Closed
@liljaylj

Description

@liljaylj

Description

find_file api action does not focus grouped directory node if group_empty config option is enabled.

i use this functionality when i try to edit directory either with :e a/b/c or nvim a/b/c, nvim-tree window with mentioned dir focused.

NOTE: minimal config is adapted from lazy.nvim

Neovim version

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

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Operating system and version

Arch Linux (6.2.11-zen1-1.1-zen x86_64)

nvim-tree version

0db85a7

Minimal config

-- disable netrw
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
      'moll/vim-bbye',
    },
    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.api.nvim_create_autocmd('User', {
  pattern = 'PackerComplete',
  once = true,
  callback = function()
    print 'Ready!'
    setup()
    vim.schedule(open_dir_buf)
  end,
})
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE

_G.open_dir_buf = function(data)
  local api = require 'nvim-tree.api'
  local bufnr = data and data.buf or vim.api.nvim_get_current_buf()
  if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) or api.tree.is_tree_buf(bufnr) then
    return
  end
  -- buffer is a directory
  local file = vim.api.nvim_buf_get_name(bufnr)
  local stat = vim.loop.fs_stat(file)
  if not (stat and stat.type == 'directory') then
    return
  end
  -- remove directory's buffer
  vim.cmd('Bdelete! ' .. bufnr)
  if not vim.startswith(file, vim.loop.cwd() or vim.fn.getcwd()) then
    vim.cmd('lcd ' .. vim.fn.fnameescape(file))
  end
  -- open nvim-tree, focus and expand directory
  api.tree.find_file { buf = file, open = true, focus = true }
  local node = api.tree.get_node_under_cursor()
  if node and node.parent then
    api.node.open.edit()
  end
end

_G.setup = function()
  require('nvim-tree').setup {
    disable_netrw = true,
    hijack_directories = {
      enable = false,
    },
    hijack_unnamed_buffer_when_opening = false,
    renderer = {
      group_empty = true,
    },
  }

  vim.api.nvim_create_autocmd({ 'BufEnter', 'BufNewFile' }, {
    group = vim.api.nvim_create_augroup('nvim_config_augroup', { clear = true }),
    callback = vim.schedule_wrap(open_dir_buf),
  })
end

Steps to reproduce

  1. create directory structure like this:
.
├── a
│   └── b
│       └── c
│           └── d
│               └── e
└── q
    └── w
        └── e
            └── r
                └── t
                    └── y

12 directories, 0 files
  1. nvim -nu /tmp/nvt-min.lua a/b/c (in directory from previous step)
  2. after packer init nvim-tree window should be opened, but directory is not focused

Expected behavior

NvimTree open with directory is focued

image

Actual behavior

NvimTree open but directory is not focued

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions