romgrk / barbar.nvim

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

Feature request: order by buffer name #561

Closed Martin1887 closed 2 months ago

Martin1887 commented 3 months ago

First, thanks for this great plugin!

There is currently an option to order buffers by directory, but his option does not takes into account buffer names. I think modifying this option to be order by name (being the directory part of the name) or creating a new option order by name would be a good feature for the plugin.

This can be useful in any use case, but it becomes even better when editing a text directory (tex, MD, etc.) with chapters with the chapter number as a prefix.

Thanks!

romgrk commented 3 months ago

If you're interested you could submit a PR, adding a new order command is as simple as writing a sort function:

https://github.com/romgrk/barbar.nvim/blob/1d6b1386abe97d1d8cba47eb9afa8a9f2d1bbe66/lua/barbar/api.lua#L364-L370

Martin1887 commented 3 months ago

Thanks, I will try it soon.

Martin1887 commented 2 months ago

Thanks for implementing this feature! But I'm not sure if currently sort by name does exactly the same (or without specifically sorting directories, which is irrelevant from this point of view) than sorting by directory.

This is better seen in a detailed example:

I have some open buffers whose names starts by '1', '2', '4', '7' in the same sub-folder, and another buffer starting by '6_' in the current folder.

Sorting by directory the '6_' file is placed the first one, and the others are sorted after it. Good job!

However, sorting by name they are placed exactly equal, while I would expect the '6' buffer would placed between '4' and '7_' buffers.

I'm not sure if my issue was not clear enough, but I expected that sorting by name takes into account only the filename for it.

Thanks!

PS: I know I said that the current sorting by directory feature would be enough, and it is, thanks! But then I think that the current sorting by name feature is not very useful.

Martin1887 commented 2 months ago

I'm not proficient in Lua nor in Neovim API, but I think that buf_get_name() function gets the full path, so it should be split into the filename to do it as I expected with a function similar to this one (https://stackoverflow.com/questions/5243179/what-is-the-neatest-way-to-split-out-a-path-name-into-its-components-in-lua):

= string.match([[/mnt/tmp/myfile.txt.1]], "(.-)([^\\/]-%.?([^%.\\/]*))$")
"/mnt/tmp/" "myfile.txt.1"  "1"

Thanks!