romgrk / barbar.nvim

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

Buffer ordering not preserved neither in sessions nor with tabs #555

Closed alexconst closed 3 months ago

alexconst commented 3 months ago

Description

Buffer ordering not preserved on two different and independent cases. When restoring a session the user's custom buffer ordering is lost. When opening a new tab and moving back to the original the user's custom buffer ordering is lost. Is there any way to prevent this automatic re-ordering? Thanks

To Reproduce

lazyvim custom.lua

return {

  -- improve tabline with scope and barbar
  {
    "romgrk/barbar.nvim",
    event = "VimEnter",
    dependencies = {
      "lewis6991/gitsigns.nvim",
      "nvim-tree/nvim-web-devicons",
    },
    init = function()
      vim.g.barbar_auto_setup = false
    end,
    opts = {
      animation = true,
      highlight_inactive_file_icons = false,
      tabpages = true,
      icons = {
        buffer_number = true,
        separator = { left = '', right = '▕' },
        separator_at_end = true,
        modified = { button = "" },
        pinned = { button = "", filename = true, separator = { left = '', right = '▕'} },
      },
    },
  },

  -- configure persistence.nvim as per barbar docs
  {
    "persistence.nvim",
    event = "BufReadPre",
    options = {--[[<other options>,]] 'globals'},
    pre_save = function() vim.api.nvim_exec_autocmds('User', {pattern = 'SessionSavePre'}) end,
  },

}

Steps to reproduce the behavior with tabs:

  1. nvim aaa bbb ccc ddd eee
  2. reorder some buffers
  3. open new tab :tabe foo
  4. go to first tab
  5. see that your custom buffer ordering has been lost

Steps to reproduce the behavior with sessions:

  1. nvim aaa bbb ccc ddd eee
  2. reorder some buffers
  3. :mksession ~/test-session.nvim
  4. exit neovim and start it with nvim -S ~/test-session.nvim
  5. see that your custom buffer ordering has been lost

Informations Running latest stable neovim and latest version of plugins updated today. neovim v0.9.5 barbar 3c48b5edf61dda21ad41c514e53448fee366a824 lazyvim 0107a1079be7fb92f7d5b7e2c40818f47cf425d9

Iron-E commented 3 months ago

Possibly related: https://github.com/romgrk/barbar.nvim/issues/31

alexconst commented 3 months ago

I checked https://github.com/romgrk/barbar.nvim/issues/31 and I can say that I don't have the ordering issues OP mentions. nvim foo bar baz keeps the buffers in the same order as provided. All good.

In that thread romgrk said he was unable to reproduce the issue. @Iron-E were you unable to reproduced both of the cases I mentioned too?

Thanks

Iron-E commented 3 months ago

I can say that I don't have the ordering issues OP mentions.

Just checking :)


Looking closer at this, I see there are a few things going on here.

Tabs

I wasn't able to reproduce this one. I converted your snippet to run without lazy.nvim installed:

local data_dir = vim.fn.stdpath 'data'
local plugin_dir = data_dir .. '/lazy/' -- or your plugin install path

vim.opt.rtp:prepend {
    plugin_dir .. 'nvim-web-devicons',
    plugin_dir .. 'gitsigns.nvim',
    plugin_dir .. 'barbar.nvim',
}

vim.g.barbar_auto_setup = false

require('barbar').setup {
    animation = true,
    highlight_inactive_file_icons = false,
    tabpages = true,
    icons = {
        buffer_number = true,
        separator = { left = '', right = '▕' },
        separator_at_end = true,
        modified = { button = "" },
        pinned = { button = "", filename = true, separator = { left = '', right = '▕'} },
    },
}

And reduced it to this:

local data_dir = vim.fn.stdpath 'data'
local plugin_dir = data_dir .. '/lazy/'

vim.opt.rtp:prepend { plugin_dir .. 'barbar.nvim' }

vim.g.barbar_auto_setup = false

require('barbar').setup {
    icons = {
        filetype = { enabled = false },
    },
}

But I wasn't able to reproduce with either… if you do nvim --clean -u minimal.lua (where minimal.lua is either of the previous snippets) does it still happen?

Sessions

First, persistence.nvim is not configured quite correctly. Since you're using lazy.nvim, you'll want to do it like this:

{
    'folke/persistence.nvim',
    event = "BufReadPre",
    opts = { -- ← options go in `opts`
        options = {--[[<other options>,]] 'globals'},
        pre_save = function() vim.api.nvim_exec_autocmds('User', {pattern = 'SessionSavePre'}) end,
    },
}

The --[[<other options>,]] should also be fleshed out to include whatever should be saved in a session. Right now, only global variables get saved (the minimum requirement for integration with barbar.nvim). For a full list, see :h 'sessionoptions'.

 

Second, calling :mksession manually does not use persistence.nvim— rather, persistence.nvim saves sessions automatically when quitting Neovim. See :h persistence.nvim-persistence-usage for more information.

Alternatively, if you wish to use :mksession manually, this snippet might help.

alexconst commented 3 months ago

Sessions

Alternatively, if you wish to use :mksession manually, this snippet might help.

That worked. I can now save sessions while preserving buffer order when running the user command:Mksession. I take it that overriding neovim's :mksession is not actually possible? Running it saved the list of buffers but didn't preserve order.

I also updated my persistence.nvim settings with the clarifications you gave. Working fine now.

Tabs

Sorry my bad. Because I'm using multiple machines I accidentely pasted the wrong config file which was missing a plugin https://github.com/tiagovla/scope.nvim Incompatibility between both plugins cause this problem. Where exactly is the problem I don't know.

scope.nvim does expose several hooks for tab enter and tabe leave, so maybe fixing this is feasible. Does barbar.nvim exposes any way to get or set the buffer order? I take it it's using its own variables to track it because :ls and vim.api.nvim_list_bufs() always shows the same buffer order.

For reference sake, here is my minimal.lua which reproduces the issue:

local data_dir = vim.fn.stdpath 'data'
local plugin_dir = data_dir .. '/lazy/'

vim.opt.rtp:prepend { plugin_dir .. 'barbar.nvim' }
vim.g.barbar_auto_setup = false
require('barbar').setup {
    icons = { 
        filetype = { enabled = false },
    },  
}

-- https://github.com/tiagovla/scope.nvim
vim.opt.rtp:prepend { plugin_dir .. 'scope.nvim' }
require("scope").setup({})

To reproduce:

  1. nvim --clean -u minimal.lua aaa bbb ccc ddd eee
  2. reorder some buffers
  3. open new tab :tabe foo
  4. go back to first tab
  5. see that your custom buffer ordering has been lost

Thanks for all the help.

Iron-E commented 3 months ago

I'm glad :Mksession et al. worked for you

I take it that overriding neovim's :mksession is not actually possible? Running it saved the list of buffers but didn't preserve order.

Not presently, but there is an effort to allow this in neovim/neovim#22814.

https://github.com/tiagovla/scope.nvim Incompatibility between both plugins cause this problem. Where exactly is the problem I don't know.

We have recommended this plugin in the past (#76), so it is in our best interest to maintain compatibility. I'll open a separate issue, since the problem with session restoration was resolved.