Skip to content

feat: case sensitive sorter #1198

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
Apr 28, 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 @@ -252,7 +252,7 @@ Here is a list of the options available in the setup call:

*nvim-tree.sort_by*
- |sort_by|: changes how files within the same directory are sorted. can be
one of 'name' | 'modification_time'
one of 'name' | 'case_sensitive' | 'modification_time'.
type: `string`
default: `"name"`

Expand Down
20 changes: 17 additions & 3 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function M.merge_sort(t, comparator)
split_merge(t, 1, #t, comparator)
end

function M.node_comparator_name(a, b)
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
if not (a and b) then
return true
end
Expand All @@ -85,7 +85,19 @@ function M.node_comparator_name(a, b)
return false
end

return a.name:lower() <= b.name:lower()
if ignorecase then
return a.name:lower() <= b.name:lower()
else
return a.name <= b.name
end
end

function M.node_comparator_name_case_sensisive(a, b)
return node_comparator_name_ignorecase_or_not(a, b, false)
end

function M.node_comparator_name_ignorecase(a, b)
return node_comparator_name_ignorecase_or_not(a, b, true)
end

function M.node_comparator_modification_time(a, b)
Expand Down Expand Up @@ -116,8 +128,10 @@ 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
else
M.node_comparator = M.node_comparator_name
M.node_comparator = M.node_comparator_name_ignorecase
end
end

Expand Down