bufferline.nvim icon indicating copy to clipboard operation
bufferline.nvim copied to clipboard

fix: respect 'insert_after_current' and 'insert_at_end' after a custom sort is set

Open AThePeanut4 opened this issue 1 year ago • 2 comments

This PR fixes two issues related to behaviour with a custom buffer sort order that have been bothering me for a while.

As described in #706, if sort_by is "insert_after_current" or "insert_at_end", and you move a buffer manually using :BufferLineMoveNext or :BufferLineMovePrev, new buffers are no longer inserted after the current buffer or at the end, but rather based on their ID. This fixes this by moving the logic out of the sort function, and into what was the get_updated_buffers function. There, while sorting the buffers according to the order in custom_sort, it will also ensure that new buffers (ie those that do not appear in custom_sort) are put where they should be. It then updates custom_sort to now contain those new buffers.

~~The second issue is that restoring the custom sort order from a saved session didn't work correctly most of the time, because it was storing buffer ids which are not preserved by :mksession. The fix is to store the full paths of the buffers in g:BufferlinePositions instead. vim.json is required to stringify the list as :mksession only stores string/number values.~~

This is the first time I've done anything substantial with a Neovim plugin, so I apologise if I've done something very wrong. I've only really tested this in my own setup, and I haven't done anything to the tests, because I don't really know how all that works. Feel free to make any changes you think are needed.

AThePeanut4 avatar Jun 23 '23 15:06 AThePeanut4

@AThePeanut4 I strongly suggest separating both changes out into separate PRs so that each change can be assessed and merged separately. Currently this PR introduces to many new changes at once. The change to fix the behaviour with session I think is most welcome. The change that moves sorting logic into the buffer fetching function will need a lot more conversations since it puts logic concerning specific sort strategies literally at the source where all buffers are fetched which I believe is a pretty big problem

Alright I've split off the second part, this PR is now just for the first change.

AThePeanut4 avatar Jun 27 '23 14:06 AThePeanut4

From what I can gather the main objective of this PR is to try to maintain the insert_after ordering whilst also allowing users to move buffers to specific positions since currently having a custom_sort overrides a previous sort_by for simplicity frankly.

You are basically correct, I essentially want to be able to maintain a custom sort order (ie :BufferLineMoveNext or :BufferLineMovePrev) while still having new buffers be inserted after the current one or at the end. This is essentially just issue #706. Additionally, when "insert_after_current|end" is set, the order is not persisted in a session file, which this fixes.

To be absolutely clear fetching buffers should essentially just return the list from neovim with some filtering based on loaded state etc. I appreciate the custom sort was implemented there historically but frankly that's a bit of tech debt. That function should primarily just fetch buffers not sort them.

That makes sense. I think what I'll do is move the custom sort logic into sorted.lua.

AThePeanut4 avatar Jun 27 '23 16:06 AThePeanut4