romgrk / barbar.nvim

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

empty buffer is focused on startup with '%' in shada #526

Closed JosefLitos closed 10 months ago

JosefLitos commented 10 months ago

Description

Occurs when using '%' in shada to open all files from previously closed instance on startup. I end up being focused on an empty buffer [buffer 1] instead of the first file being opened.

To Reproduce

init.vim:

let $PLUGIN_DIRECTORY = '~/.config/nvim/bundle'
set runtimepath^=$PLUGIN_DIRECTORY/nvim-web-devicons
set runtimepath^=$PLUGIN_DIRECTORY/barbar.nvim
set shada+='%'
" Set your options here
let bufferline = {}

Steps to reproduce the behavior:

  1. open a file
  2. close nvim
  3. open nvim without a file
  4. Observe the empty buffer

Informations Neovim version: 0.10

It is possible that this is default behaviour of neovim, if that's the case, is there any way I could mitigate this?

Iron-E commented 10 months ago

Initial investigation: might related to autocmd cruft from the original Vim— we've had issues with that before. (Specifically, BufNew is fired when loading buffers from the shada file, but not BufRead[Pre/Post]).

I have reproduced this issue without barbar.nvim, so it's unrelated to this plugin. I'll try to come up with a snippet that can help mitigate it

Iron-E commented 10 months ago

How is this?

vim.api.nvim_create_autocmd('VimEnter', {callback = function()
  if vim.api.nvim_cmd({cmd = 'args'}, {output = true}) == '' then -- if opening neovim without args
    vim.api.nvim_command 'bdelete' -- delete the empty buffer

    -- HACK: the next buffer won't have a filetype for… some reason. just re-set it.
    local ft = vim.filetype.match {buf = 0}
    vim.api.nvim_buf_set_option(0, 'filetype', ft)
  end
end})
JosefLitos commented 10 months ago

I just added a check for the ft (can be nil when no other buffers were opened). But it does the job very well, thanks a lot.

JosefLitos commented 10 months ago

The only issue remaining is the filetype detection - even with this hack (or similarly with vim.bo.ft = ft) lsp won't get loaded (when loaded by plugin lazy loaded by given filetype - neodev on ft='lua')

JosefLitos commented 10 months ago

Final solution:

vim.api.nvim_create_autocmd('VimEnter', {
    callback = function()
        if #vim.v.argv < 3 and vim.fn.bufnr '$' > 1 then -- no args and other buffers opened
            vim.cmd.bdelete()

            local ft = vim.filetype.match { buf = 0 }
            if ft then
                vim.bo.ft = ft
                vim.api.nvim_exec_autocmds('FileType', { pattern = ft })
            end
        end
    end,
})
Iron-E commented 10 months ago

Thanks for posting the updated solution! Very strange that the filetype is cleared at all.