feat: case sensitive sorter (#1198)

This commit is contained in:
Vincent Law 2022-04-28 08:43:58 +08:00 committed by GitHub
parent eeb842c820
commit fd2332a33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -253,7 +253,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"`

View File

@ -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
@ -85,7 +85,19 @@ function M.node_comparator_name(a, b)
return false
end
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)
@ -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