-
-
Notifications
You must be signed in to change notification settings - Fork 624
feat(#2364): add option to show files first #2366
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
Conversation
FYI: you're a repo member: you can push a branch to this repo instead of using a fork. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice, simplifies things a little as well.
Tested:
- all permutations of
folders_first
andfiles_first
- sort function
lua/nvim-tree/explorer/sorters.lua
Outdated
end | ||
|
||
if x ~= nil then | ||
if M.config.sort.files_first then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we flip this around? Test this config at the start of the function?
That would remove the need for the mystery x
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I stored in x
what should be returned if sort.folders_first
is enabled and reverse it with sort.files_first
, but now that I think about it maybe something like this would be more readable and intuitive.
local a_and_not_b, not_a_and_b
if M.config.sort.files_first then
a_and_not_b = false
not_a_and_b = true
elseif M.config.sort.folders_first then
a_and_not_b = true
not_a_and_b = false
else
return
end
if a.nodes and not b.nodes then
return a_and_not_b
elseif not a.nodes and b.nodes then
return not_a_and_b
elseif a.nodes and b.nodes and third_option_callback ~= nil then
return third_option_callback()
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much more readable. Could we go simpler?
if a.nodes and not b.nodes then
return M.config.sort.folders_first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After several tests I think that would not work because the function should return true
or false
only if one (or both) of sort.folders_first
and sort.files_first
are enabled, otherwise it should return nil
and continue without altering entries' order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah of course. How about an early exit?
if not M.config.sort.files_first and not M.config.sort.folders_first then
return
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this is the simplest and most concise way I got it to work:
if not (M.config.sort.folders_first or M.config.sort.files_first) then
return
end
local x = M.config.sort.files_first or not M.config.sort.folders_first
if not a.nodes and b.nodes then
return x
elseif a.nodes and not b.nodes then
return not x
elseif fallback and a.nodes and b.nodes then
return fallback()
end
Now we just have to give a sensible name to x
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should do it:
---Evaluate `sort.folders_first` and `sort.files_first`, files takes precedence.
---@param a table node
---@param b table node
---@param fallback function|nil function should return `boolean`. Evaluated when both are folders.
---@return boolean|nil
local function folders_or_files_first(a, b, fallback)
if not M.config.sort.folders_first and not M.config.sort.files_first then
return
end
if not a.nodes and b.nodes then
-- file <> folder
return M.config.sort.files_first
elseif a.nodes and not b.nodes then
-- folder <> file
return not M.config.sort.files_first
elseif fallback and a.nodes and b.nodes then
-- folder <> folder
return fallback()
end
end
Yes, we could be tricky with ternaries but readability is more important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't consider to completely ignore sort.folders_first
but it works so that's fine.
I didn't think about that, next time I'll push a new branch here. |
With the last commit I removed the |
That's great... simple is best. I'll test this one on the weekend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested:
- all permutations of
folders_first
andfiles_first
- sort function
Many thanks for your contribution!
This is a simple and straight forward implementation of the feature requested in #2364. For the configuration I think that since
sort.folders_first
is enabled by default,sort.files_first = true
should override it. I made a minor refactoring inexplorer/sorter.lua
, moving the folders-first logic in a separate function so that it's easier to manage and enhance.