akinsho / akinsho/bufferline.nvim

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

オープン
#1,023 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
Lua
スター
4.4k
フォーク
240
PR マージ指標
30日以内にマージされた PR はありません

説明

### 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_

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。