romgrk / barbar.nvim

The neovim tabline plugin.
2.23k stars 83 forks source link

[Question] Integration with filetree plugins (Neo-Tree) #355

Closed FahimAnayet closed 1 year ago

FahimAnayet commented 1 year ago

After closing neotree file tree offset stays there.

NVIM v0.9.0-dev-613+gef18c9f9b-dirty
Build type: Release
LuaJIT 2.1.0-beta3
Barbar: v1.4.1
NeoTree: v2.49
OS: macOS 12.6.2

barbar config. Everything is default except following

vim.api.nvim_create_autocmd('BufWinEnter', {
  pattern = '*',
  callback = function()
    if vim.bo.filetype == 'neo-tree' then
      require'bufferline.api'.set_offset(31, 'FileTree')
    end
  end
})

vim.api.nvim_create_autocmd('BufWinLeave', {
  pattern = '*',
  callback = function()
    if vim.fn.expand('<afile>'):match('neo-tree') then
      require'bufferline.api'.set_offset(0)
    end
  end
})

neo-tree config

    -- Trash the target
    local function trash(state)
        local tree = state.tree
        local node = tree:get_node()
        if node.type == "message" then
            return
        end
        local _, name = utils.split_path(node.path)
        local msg = string.format("Are you sure to trash '%s'?", name)
        inputs.confirm(msg, function(confirmed)
            if not confirmed then
                return
            end
            vim.api.nvim_command("silent !trash -F " .. node.path)
            cmds.refresh(state)
        end)
    end

    -- Trash the selections (visual mode)
    local function trash_visual(state, selected_nodes)
        local paths_to_trash = {}
        for _, node in ipairs(selected_nodes) do
            if node.type ~= 'message' then
                table.insert(paths_to_trash, node.path)
            end
        end
        local msg = "Are you sure to trash " .. #paths_to_trash .. " items?"
        inputs.confirm(msg, function(confirmed)
            if not confirmed then
                return
            end
            for _, path in ipairs(paths_to_trash) do
                vim.api.nvim_command("silent !trash -F " .. path)
            end
            cmds.refresh(state)
        end)
    end

    require("neo-tree").setup({
        vim.cmd([[ let g:neo_tree_remove_legacy_commands = 1 ]]),
        close_if_last_window = true,
        -- use_libuv_file_watcher = true,
        -- follow_current_file = true,
        window = {
            position = "left",
            width = 35,
        }
    })

video for better understanding

https://user-images.githubusercontent.com/48918937/213254266-cb26584f-7663-430f-b3dd-84c7c8d75b35.mov

FahimAnayet commented 1 year ago

If I use the following code in barbar instead, it's works fine.

local function is_neotree_open()
    for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
        if vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(win), 'ft') == 'neo-tree' then
            return require 'bufferline.api'.set_offset(35, 'FileTree')
        end
    end
    return require 'bufferline.api'.set_offset(0)
end

vim.api.nvim_create_autocmd({ 'BufWinEnter', 'BufWipeout' }, {
    pattern = '*',
    callback = function()
        is_neotree_open()
    end
})

Thanks.

szechp commented 1 year ago

is there a way to make this dynamic? to somehow get the current width of neo tree?

Iron-E commented 1 year ago

@FahimAnayet can you try #360 to see if that also works with neo-tree?

@szechp since there is no WinResized event (yet), the only way to accomplish that would be to write a window resize command that then updates the number for require 'bufferline.api'.set_offset(35, 'FileTree').

FahimAnayet commented 1 year ago

@Iron-E unfortunately #360 doesn't work with the given instructions with neo-tree either. The main problem is neo-tree doesn't leave rather it just wipes out the buffer. So, if we use the following

-- Doesn't work
vim.api.nvim_create_autocmd('BufWinEnter', {
  callback = function(tbl)
    if vim.bo[tbl.buf].filetype == 'NvimTree' then
      require'bufferline.api'.set_offset(31, 'FileTree')
    end
  end
})

vim.api.nvim_create_autocmd('BufWinLeave', {
  callback = function(tbl)
    if vim.bo[tbl.buf].filetype == 'NvimTree' then
      require'bufferline.api'.set_offset(0)
    end
  end
})
-- Does work
vim.api.nvim_create_autocmd('BufWinEnter', {
  callback = function(tbl)
    if vim.bo[tbl.buf].filetype == 'NvimTree' then
      require'bufferline.api'.set_offset(31, 'FileTree')
    end
  end
})

vim.api.nvim_create_autocmd('BufWipeout', {
  callback = function(tbl)
    if vim.bo[tbl.buf].filetype == 'NvimTree' then
      require'bufferline.api'.set_offset(0)
    end
  end
})

then #360 certainly does work. So, For neo-tree we must use BufWipeout instead of BufWinLeave.

Iron-E commented 1 year ago

I updated the PR to include the BufWipeout event, thanks for the feedback.

d1y commented 1 year ago

is there a way to make this dynamic? to somehow get the current width of neo tree?

Hey buddy, can your current configuration dynamically obtain the width of the neo-tree?

Iron-E commented 1 year ago

is there a way to make this dynamic? to somehow get the current width of neo tree?

Hey buddy, can your current configuration dynamically obtain the width of the neo-tree?

https://github.com/romgrk/barbar.nvim/pull/374