Skip to content

feat: extension sorter (#1181) #1264

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 1 commit into from
May 14, 2022
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
2 changes: 1 addition & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ if the tree was previously open.

*nvim-tree.sort_by*
Changes how files within the same directory are sorted.
Can be one of 'name', 'case_sensitive' or 'modification_time'.
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension'.
Type: `string`, Default: `"name"`

*nvim-tree.hijack_unnamed_buffer_when_opening*
Expand Down
26 changes: 26 additions & 0 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,38 @@ function M.node_comparator_modification_time(a, b)
return last_modified_b <= last_modified_a
end

function M.node_comparator_extension(a, b)
if not (a and b) then
return true
end

if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
end

if not (a.extension and b.extension) then
return true
end

if a.extension and not b.extension then
return true
elseif not a.extension and b.extension then
return false
end

return a.extension:lower() <= b.extension:lower()
end

function M.setup(opts)
M.sort_by = opts.sort_by
if M.sort_by == "modification_time" then
M.node_comparator = M.node_comparator_modification_time
elseif M.sort_by == "case_sensitive" then
M.node_comparator = M.node_comparator_name_case_sensisive
elseif M.sort_by == "extension" then
M.node_comparator = M.node_comparator_extension
else
M.node_comparator = M.node_comparator_name_ignorecase
end
Expand Down