Skip to content

feat: add sort_by "suffix" #2307

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

Merged
merged 6 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ Reloads the explorer every time a buffer is written to.

*nvim-tree.sort_by*
Changes how files within the same directory are sorted.
Can be one of `name`, `case_sensitive`, `modification_time`, `extension`,
`filetype` or a function.
Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`,
`"suffix"`, `"filetype"` or a function.
`"extension"` uses all suffixes e.g. `foo.tar.gz` -> `.tar.gz`
`"suffix"` uses the last e.g. `.gz`
Type: `string` | `function(nodes)`, Default: `"name"`

Function may perform a sort or return a string with one of the above
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed suffix from the node; it's more complex to calculate and users can do it themselves if they feel the need.

Expand Down
50 changes: 50 additions & 0 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,56 @@ function C.modification_time(a, b)
return last_modified_b <= last_modified_a
end

function C.suffix(a, b)
if not (a and b) then
return true
end

-- directories go first
if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
elseif a.nodes and b.nodes then
return C.name(a, b)
end

-- dotfiles go second
if a.name:sub(1, 1) == "." and b.name:sub(1, 1) ~= "." then
return true
elseif a.name:sub(1, 1) ~= "." and b.name:sub(1, 1) == "." then
return false
elseif a.name:sub(1, 1) == "." and b.name:sub(1, 1) == "." then
return C.name(a, b)
end

-- unsuffixed go third
local a_suffix_ndx = a.name:find "%.%w+$"
local b_suffix_ndx = b.name:find "%.%w+$"

if not a_suffix_ndx and b_suffix_ndx then
return true
elseif a_suffix_ndx and not b_suffix_ndx then
return false
elseif not (a_suffix_ndx and b_suffix_ndx) then
return C.name(a, b)
end

-- finally, compare by suffixes
local a_suffix = a.name:sub(a_suffix_ndx)
local b_suffix = b.name:sub(b_suffix_ndx)

if a_suffix and not b_suffix then
return true
elseif not a_suffix and b_suffix then
return false
elseif a_suffix:lower() == b_suffix:lower() then
return C.name(a, b)
end

return a_suffix:lower() < b_suffix:lower()
end

function C.extension(a, b)
if not (a and b) then
return true
Expand Down