romgrk / barbar.nvim

The neovim tabline plugin.
2.2k stars 81 forks source link

`sidebar_filetypes` with `NvimTree` doesn't work on startup #421

Closed Iron-E closed 1 year ago

Iron-E commented 1 year ago

See:

cc @bavo96; what was your NvimTree config?

bavo96 commented 1 year ago

Hey @Iron-E, this is my current nvim-tree.lua config:

local status_ok, nvimtree = pcall(require, 'nvim-tree')

if not status_ok then
    print('nvim-tree is not working. Skipping...')
    return
end

-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

local options = {
  filters = {
    dotfiles = false,
  },
  disable_netrw = true,
  hijack_netrw = true,
  hijack_cursor = true,
  hijack_unnamed_buffer_when_opening = false,
  sync_root_with_cwd = true,
  update_focused_file = {
    enable = true,
    update_root = true,
  },
}

-- setup with some options
nvimtree.setup(options)

-- auto open nvim-tree when open neovim
local function open_nvim_tree(data)

  -- buffer is a real file on the disk
  local real_file = vim.fn.filereadable(data.file) == 1

  -- buffer is a [No Name]
  local no_name = data.file == "" and vim.bo[data.buf].buftype == ""

  -- only files please
  if not real_file and not no_name then
    return
  end

  -- open the tree but don't focus it
  require("nvim-tree.api").tree.toggle({ focus = false })
end
vim.api.nvim_create_autocmd({ "VimEnter" }, { 
  callback = open_nvim_tree
})

-- auto close nvim-tree
vim.api.nvim_create_autocmd("BufEnter", {
  group = vim.api.nvim_create_augroup("NvimTreeClose", {clear = true}),
  pattern = "NvimTree_*",
  callback = function()
    local layout = vim.api.nvim_call_function("winlayout", {})
    if layout[1] == "leaf" and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(layout[2]), "filetype") == "NvimTree" and layout[3] == nil then vim.cmd("confirm quit") end
  end
})
Iron-E commented 1 year ago

I'm gonna take a look at this next.

Iron-E commented 1 year ago

@bavo96 I tried your config but couldn't reproduce. Two things we can try:

  1. vim.g.barbar_auto_setup = false (see the README). It's a recent feature that may help in this case.
  2. Ensure that require'barbar'.setup is called before require'nvim-tree'.setup, and before the tree opens for the first time.

If you try all that and the problem is still happening, see if you can't get it to reproduce with nvim --clean:

nvim --clean 
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/barbar.nvim' 
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/nvim-tree.lua'
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/nvim-web-devicons'
     --cmd 'lua require"bufferline".setup{ OPTIONS_HERE }'
     -u minimal.lua

You will have to adjust the rtps, and fill your minimal.lua with any other commands.

bavo96 commented 1 year ago

Hey @Iron-E, been busy these days and now I have time to go back with this. I have tried the two suggestions above, it seems to work now but there's a small issue. When I first open neovim with a default buffer, the sidebar haven't appeared yet and only when I open a file, the sidebar appears, as shown in the below clip:

https://user-images.githubusercontent.com/27908949/230942662-ada21f4c-6811-402c-b79d-80d25be3d5c6.mp4

I have also tried the nvim-clean and the result is the same:

https://user-images.githubusercontent.com/27908949/230942687-5ec99452-bada-40c9-9273-5d113ef67e3b.mp4

And this is my setup for nvim-clean:

bash

nvim --clean  \
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/barbar.nvim' \
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/nvim-tree.lua' \
     --cmd 'set rtp+=~/.local/share/nvim/site/pack/packer/start/nvim-web-devicons' \
     --cmd 'lua require"bufferline".setup{ 
         -- Enable/disable animations
        animation = true,

        -- Icons in barbar
        icons = {
            -- Configure the base icons on the bufferline.
            buffer_index = true, -- Index according to visual index on the screen not the background number of buffer
        },
        -- Set the filetypes which barbar will offset itself for
        sidebar_filetypes = {
            NvimTree = true,
        },
 }' \
     --cmd 'lua require"nvim-tree".setup{
        filters = {
            dotfiles = false,
        },
        disable_netrw = true,
        hijack_netrw = true,
        hijack_cursor = true,
        hijack_unnamed_buffer_when_opening = false,
        sync_root_with_cwd = true,
        update_focused_file = {
            enable = true,
            update_root = true,
        },
 }' \
     -u minimal.lua

minimal.lua

vim.g.barbar_auto_setup = false

-- auto open nvim-tree when open neovim
local function open_nvim_tree(data)
    -- buffer is a real file on the disk
    local real_file = vim.fn.filereadable(data.file) == 1

    -- buffer is a [No Name]
    local no_name = data.file == "" and vim.bo[data.buf].buftype == ""

    -- only files please
    if not real_file and not no_name then
        return
    end
    -- open the tree but dont focus it
    require("nvim-tree.api").tree.toggle({ focus = false })
end
vim.api.nvim_create_autocmd(
{ "VimEnter" },
{ callback = open_nvim_tree })
Iron-E commented 1 year ago

I can reproduce with that, thank you!

Iron-E commented 1 year ago

Nvim-tree doesn't emit BufWinEnter on start. I'll have to report it upstream.

For now, you can add:

vim.api.nvim_exec_autocmds('BufWinEnter', {buffer = vim.fn.bufnr('#')})

after toggling the tree to emit it yourself.

bavo96 commented 1 year ago

Many thanks for your effort :D I don't know if I'm doing right but after adding the line to nvim-tree.lua, I got this error:

image

This is my current setting for nvim-tree config:

local status_ok, nvimtree = pcall(require, 'nvim-tree')

if not status_ok then
    print('nvim-tree is not working. Skipping...')
    return
end

-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

local options = {
    filters = {
        dotfiles = false,
    },
    disable_netrw = true,
    hijack_netrw = true,
    hijack_cursor = true,
    hijack_unnamed_buffer_when_opening = false,
    sync_root_with_cwd = true,
    update_focused_file = {
        enable = true,
        update_root = true,
    },
}

-- setup with some options
nvimtree.setup(options)

-- auto open nvim-tree when open neovim
local function open_nvim_tree(data)
    -- buffer is a real file on the disk
    local real_file = vim.fn.filereadable(data.file) == 1

    -- buffer is a [No Name]
    local no_name = data.file == "" and vim.bo[data.buf].buftype == ""

    -- only files please
    if not real_file and not no_name then
        return
    end

    -- open the tree but don't focus it
    require("nvim-tree.api").tree.toggle({ focus = false })
end
vim.api.nvim_create_autocmd({ "VimEnter" }, {
    callback = open_nvim_tree
})

-- Emit BufWinEnter
vim.api.nvim_exec_autocmds('BufWinEnter', {buffer = vim.fn.bufnr('#')})

-- auto close nvim-tree
vim.api.nvim_create_autocmd("BufEnter", {
    group = vim.api.nvim_create_augroup("NvimTreeClose", { clear = true }),
    pattern = "NvimTree_*",
    callback = function()
        local layout = vim.api.nvim_call_function("winlayout", {})
        if layout[1] == "leaf" and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(layout[2]), "filetype") == "NvimTree" and layout[3] == nil then
            vim.cmd("confirm quit") end
    end
})
Iron-E commented 1 year ago

Sorry for any confusion. This will work:

require'nvim-tree'.setup {}

-- auto open nvim-tree when open neovim
local function open_nvim_tree(data)
    -- buffer is a real file on the disk
    local real_file = vim.fn.filereadable(data.file) == 1

    -- buffer is a [No Name]
    local no_name = data.file == '' and vim.bo[data.buf].buftype == ''

    -- only files please
    if not real_file and not no_name then
        return
    end

    -- open the tree but dont focus it
    require('nvim-tree.api').tree.toggle({ focus = false })
    vim.api.nvim_exec_autocmds('BufWinEnter', {buffer = require('nvim-tree.view').get_bufnr()})
end

vim.api.nvim_create_autocmd({'VimEnter'}, { callback = open_nvim_tree })
bavo96 commented 1 year ago

Many thanks @Iron-E it works now, you rock <3 Wish you all the best :D

Iron-E commented 1 year ago

Apparently this is a bug upstream in Neovim. It will be necessary to use that workaround until it is fixed.