Skip to content

feat: Mixin Sorter (#1565) Self Solved #1566

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 13 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 14 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,22 @@ Opens the tree automatically when switching tabpage or opening a new tabpage
if the tree was previously open.
Type: `boolean`, Default: `false`


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

local user_order = {};
for i = 1, #tbl do
table.insert(user_order, tbl.absolute_path)
end

return user_oder
end

*nvim-tree.hijack_unnamed_buffer_when_opening*
Opens in place of the unnamed buffer if it's empty.
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
height = { string = true, ["function"] = true, number = true },
remove_keymaps = { boolean = true, table = true },
on_attach = { ["function"] = true, string = true },
sort_by = { ["function"] = true, string = true },
}

local function validate_options(conf)
Expand Down
40 changes: 34 additions & 6 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,39 @@ end
---@param t any[]
---@param comparator function|nil
function M.merge_sort(t, comparator)
if not comparator then
comparator = function(left, right)
return left < right
if type(M.sort_by) == "function" then
local t_user = {}
for _, n in ipairs(t) do
table.insert(t_user, {
absolute_path = n.absolute_path,
executable = n.executable,
extension = n.extension,
link_to = n.link_to,
name = n.name,
type = n.type,
})
end
end

split_merge(t, 1, #t, comparator)
local user_order = M.sort_by(t_user)

for i = 1, #user_order, 1 do
for j = 1, #t, 1 do
if t[j].absolute_path == user_order[i] then
local tmp = t[i]
t[i] = t[j]
t[j] = tmp
break
end
end
end -- reorder the list according to the user order
else
if not comparator then
comparator = function(left, right)
return left < right
end
end
split_merge(t, 1, #t, comparator)
end
end

local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
Expand Down Expand Up @@ -150,7 +176,9 @@ end

function M.setup(opts)
M.sort_by = opts.sort_by
if M.sort_by == "modification_time" then
if M.sort_by and type(M.sort_by) ~= "function" then
M.node_comparator = M.sort_by
elseif 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
Expand Down