Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidebar_filetypes with NvimTree doesn't work on startup #421

Closed
Iron-E opened this issue Apr 2, 2023 · 10 comments
Closed

sidebar_filetypes with NvimTree doesn't work on startup #421

Iron-E opened this issue Apr 2, 2023 · 10 comments
Assignees
Labels
bug Something isn't working upstream Caused by code barbar.nvim depends on
Milestone

Comments

@Iron-E
Copy link
Collaborator

Iron-E commented Apr 2, 2023

See:

cc @bavo96; what was your NvimTree config?

@Iron-E Iron-E added the bug Something isn't working label Apr 2, 2023
@Iron-E Iron-E self-assigned this Apr 2, 2023
@bavo96
Copy link

bavo96 commented Apr 3, 2023

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
Copy link
Collaborator Author

Iron-E commented Apr 5, 2023

I'm gonna take a look at this next.

@Iron-E
Copy link
Collaborator Author

Iron-E commented Apr 5, 2023

@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.

@Iron-E Iron-E added need-feedback Further information from original poster was requested. downstream Caused by code depending on barbar.nvim labels Apr 5, 2023
@Iron-E Iron-E added this to the 1.6 milestone Apr 9, 2023
@bavo96
Copy link

bavo96 commented Apr 10, 2023

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:

barbar-2023-04-10_23.01.52.mp4

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

barbar2-2023-04-10_23.03.23.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
Copy link
Collaborator Author

Iron-E commented Apr 10, 2023

I can reproduce with that, thank you!

@Iron-E
Copy link
Collaborator Author

Iron-E commented Apr 10, 2023

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.

@Iron-E Iron-E added upstream Caused by code barbar.nvim depends on and removed downstream Caused by code depending on barbar.nvim need-feedback Further information from original poster was requested. labels Apr 10, 2023
@bavo96
Copy link

bavo96 commented Apr 11, 2023

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
Copy link
Collaborator Author

Iron-E commented Apr 11, 2023

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
Copy link

bavo96 commented Apr 12, 2023

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

@Iron-E Iron-E pinned this issue Apr 13, 2023
@Iron-E
Copy link
Collaborator Author

Iron-E commented Apr 17, 2023

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

@Iron-E Iron-E modified the milestones: backlog, unplanned Apr 17, 2023
@Iron-E Iron-E unpinned this issue Apr 17, 2023
@Iron-E Iron-E closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream Caused by code barbar.nvim depends on
Projects
None yet
Development

No branches or pull requests

2 participants