akinsho / akinsho/bufferline.nvim

[Bug]: The cursor moves to the first line when switching tab for the first time

Abierto
#1,023 0 comentarios 0 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Lua
Estrellas
4.4k
Forks
240
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

Problem description:
1. open file `a.txt`
2. open another file in a new tab with `tabe b.txt`
3. use `` (which is mapped to BufferLineCyclePrev) to switch to the first tab
4. the cursor moves to the first line of `a.txt`

Extra information:
1. This problem only happens the first time switching tabs
2. This problem does not happen if using `gt` to switch tabs, so I think it is related to BufferLineCyclePrev
3. I use an autocmd to automatically restore the cursor location every time I open a file, which is copied in the "additional information" part

### What did you expect to happen?

When I switch from `b.txt` to `a.txt`, the cursor should stay at its original position.

### Config
```
return {
"akinsho/bufferline.nvim",
event = "VeryLazy",
keys = {
{ "bp", "BufferLineTogglePin", desc = "Toggle pin" },
{ "bP", "BufferLineGroupClose ungrouped", desc = "Delete non-pinned buffers" },
{ "bo", "BufferLineCloseOthers", desc = "Delete other buffers" },
{ "br", "BufferLineCloseRight", desc = "Delete buffers to the right" },
{ "bl", "BufferLineCloseLeft", desc = "Delete buffers to the left" },
{ "", "BufferLineCyclePrev", desc = "Prev buffer" },
{ "", "BufferLineCycleNext", desc = "Next buffer" },
{ "[b", "BufferLineCyclePrev", desc = "Prev buffer" },
{ "]b", "BufferLineCycleNext", desc = "Next buffer" },
},
opts = {
options = {
-- stylua: ignore
close_command = function(n) require("mini.bufremove").delete(n, false) end,
-- stylua: ignore
right_mouse_command = function(n) require("mini.bufremove").delete(n, false) end,
diagnostics = "nvim_lsp",
always_show_bufferline = false,
diagnostics_indicator = function(_, _, diag)
local icons = require("lazyvim.config").icons.diagnostics
local ret = (diag.error and icons.Error .. diag.error .. " " or "")
.. (diag.warning and icons.Warn .. diag.warning or "")
return vim.trim(ret)
end,
offsets = {
{
filetype = "neo-tree",
text = "Neo-tree",
highlight = "Directory",
text_align = "left",
},
},
},
},
config = function(_, opts)
require("bufferline").setup(opts)
-- Fix bufferline when restoring a session
vim.api.nvim_create_autocmd("BufAdd", {
callback = function()
vim.schedule(function()
pcall(nvim_bufferline)
end)
end,
})
end,
}
```

### Additional Information

the autocmd I use to restore cursor location:

```
-- adapted from https://github.com/neovim/neovim/issues/16339#issuecomment-1348133829
local ignore_buftype = { "quickfix", "nofile", "help" }
local ignore_filetype = { "gitcommit", "gitrebase", "svn", "hgcommit" }

local function run()
if vim.tbl_contains(ignore_buftype, vim.bo.buftype) then
return
end

if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then
-- reset cursor to first line
vim.cmd[[normal! gg]]
return
end

-- If a line has already been specified on the command line, we are done
-- nvim file +num
if vim.fn.line(".") > 1 then
return
end

local last_line = vim.fn.line([['"]])
local buff_last_line = vim.fn.line("$")

-- If the last line is set and the less than the last line in the buffer
if last_line > 0 and last_line <= buff_last_line then
local win_last_line = vim.fn.line("w$")
local win_first_line = vim.fn.line("w0")
-- Check if the last line of the buffer is the same as the win
if win_last_line == buff_last_line then
-- Set line to last line edited
vim.cmd[[normal! g`"]]
-- Try to center
elseif buff_last_line - last_line > ((win_last_line - win_first_line) / 2) - 1 then
vim.cmd[[normal! g`"zz]]
else
vim.cmd[[normal! G'"]]
end
end
end

vim.api.nvim_create_autocmd({'BufNew', 'FileType'}, {
group = vim.api.nvim_create_augroup('nvim-lastplace', {}),
callback = run,
})
```

### commit

_No response_

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.