Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: order by buffer name #561

Closed
Martin1887 opened this issue Mar 29, 2024 · 4 comments · Fixed by #570
Closed

Feature request: order by buffer name #561

Martin1887 opened this issue Mar 29, 2024 · 4 comments · Fixed by #570
Assignees
Labels
request New feature or request
Milestone

Comments

@Martin1887
Copy link

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!

@Iron-E Iron-E added the request New feature or request label Mar 29, 2024
@romgrk
Copy link
Owner

romgrk commented Apr 1, 2024

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

--- Order the buffers by their parent directory.
--- @return nil
function api.order_by_directory()
table_sort(state.buffers, with_pin_order(function(a, b)
local name_of_a = buf_get_name(a)
local name_of_b = buf_get_name(b)
local a_less_than_b = name_of_b < name_of_a

@Martin1887
Copy link
Author

Thanks, I will try it soon.

@Martin1887
Copy link
Author

Martin1887 commented Apr 7, 2024

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
Copy link
Author

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!

@romgrk romgrk reopened this Apr 9, 2024
@romgrk romgrk closed this as completed in 605d924 Apr 11, 2024
@Iron-E Iron-E added this to the 1.8 milestone Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants