* feat: Mixin Sorter (#1565) Self Solved adding `mixin` sort options for `rust` like package systems ``` package.rs package/ __inside__ lib.rs lib/ _inside_ a.rs b.rs module.rs ``` * feat: sort_by, after_sort options for more convinient using ``` *nvim-tree.sort_by* Changes how files within the same directory are sorted. Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension', 'function'. > sort_by = function(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 return a.name:lower() <= b.name:lower() end end Type: `string | function(a, b)`, Default: `"name"` *nvim-tree.after_sort* Related to nvim-tree.sort_by, this function runs without mergesort. Can be defined by your own after-sort works. Type: `function(table)`, Default: `disable` > after_sort = function(t) local i = 1 while i <= #t do if t[i] and t[i].nodes then local j = i + 1 while j <= #t do if t[j] and not t[j].nodes and t[i].name:lower() == t[j].name:lower():match "(.+)%..+$" then local change_target = t[j] table.remove(t, j) table.insert(t, i, change_target) break end j = j + 1 end end i = i + 1 end end ``` * remove: after_sort ( misunderstood feature ) sort_by parameter can be function. ``` lua sort_by = function(t) local sorters = require "nvim-tree.explorer.sorters" local comparator = sorters.retrieve_comparator("name") sorters.split_merge(t, 1, #t, comparator) -- run default merge_sort local i = 1 while i <= #t do if t[i] and t[i].nodes then local j = i + 1 while j <= #t do if t[j] and not t[j].nodes and t[i].name:lower() == t[j].name:lower():match "(.+)%..+$" then local change_target = t[j] table.remove(t, j) table.insert(t, i, change_target) break end j = j + 1 end end i = i + 1 end end, ``` * try-fix: change existing merge_sort function, call user's sort_by hope.. like it...? * doc: explain function parameter and return, add more complex example * fix: reorder with user-comparator exceed memory limit apply merge_sort check nil & type for senitize * fix: user_index based sorting ( create index ) for performance, create index once, using index to re-ordering * fix: fence problems * doc & fix: merge_sort problem fix & nil sorting add complex example * fix: sort_by detect and use string and nil * doc: revert sort_by to simple * fix: sort_by does not return anything Co-authored-by: Alexander Courtis <alex@courtis.org>
206 lines
4.6 KiB
Lua
206 lines
4.6 KiB
Lua
local M = {
|
|
sort_by = nil,
|
|
node_comparator = nil,
|
|
}
|
|
|
|
---Create a shallow copy of a portion of a list.
|
|
---@param t table
|
|
---@param first integer First index, inclusive
|
|
---@param last integer Last index, inclusive
|
|
---@return table
|
|
local function tbl_slice(t, first, last)
|
|
local slice = {}
|
|
for i = first, last or #t, 1 do
|
|
table.insert(slice, t[i])
|
|
end
|
|
|
|
return slice
|
|
end
|
|
|
|
local function merge(t, first, mid, last, comparator)
|
|
local n1 = mid - first + 1
|
|
local n2 = last - mid
|
|
local ls = tbl_slice(t, first, mid)
|
|
local rs = tbl_slice(t, mid + 1, last)
|
|
local i = 1
|
|
local j = 1
|
|
local k = first
|
|
|
|
while i <= n1 and j <= n2 do
|
|
if comparator(ls[i], rs[j]) then
|
|
t[k] = ls[i]
|
|
i = i + 1
|
|
else
|
|
t[k] = rs[j]
|
|
j = j + 1
|
|
end
|
|
k = k + 1
|
|
end
|
|
|
|
while i <= n1 do
|
|
t[k] = ls[i]
|
|
i = i + 1
|
|
k = k + 1
|
|
end
|
|
|
|
while j <= n2 do
|
|
t[k] = rs[j]
|
|
j = j + 1
|
|
k = k + 1
|
|
end
|
|
end
|
|
|
|
local function split_merge(t, first, last, comparator)
|
|
if (last - first) < 1 then
|
|
return
|
|
end
|
|
|
|
local mid = math.floor((first + last) / 2)
|
|
|
|
split_merge(t, first, mid, comparator)
|
|
split_merge(t, mid + 1, last, comparator)
|
|
merge(t, first, mid, last, comparator)
|
|
end
|
|
|
|
---Perform a merge sort on a given list.
|
|
---@param t any[]
|
|
---@param comparator function|nil
|
|
function M.merge_sort(t, comparator)
|
|
if type(M.sort_by) == "function" then
|
|
local t_user = {}
|
|
local origin_index = {}
|
|
|
|
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,
|
|
})
|
|
table.insert(origin_index, n)
|
|
end
|
|
|
|
M.sort_by(t_user)
|
|
|
|
-- do merge sort for prevent memory exceed
|
|
local user_index = {}
|
|
for i, v in ipairs(t_user) do
|
|
if type(v.absolute_path) == "string" and user_index[v.absolute_path] == nil then
|
|
user_index[v.absolute_path] = i
|
|
end
|
|
end
|
|
|
|
-- if missing value found, then using origin_index
|
|
local mini_comparator = function(a, b)
|
|
local a_index = user_index[a.absolute_path] or origin_index[a.absolute_path]
|
|
local b_index = user_index[b.absolute_path] or origin_index[b.absolute_path]
|
|
|
|
if type(a_index) == "number" and type(b_index) == "number" then
|
|
return a_index <= b_index
|
|
end
|
|
return (a_index or 0) <= (b_index or 0)
|
|
end
|
|
|
|
split_merge(t, 1, #t, mini_comparator) -- sort by 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)
|
|
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 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)
|
|
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
|
|
|
|
local last_modified_a = 0
|
|
local last_modified_b = 0
|
|
|
|
if a.fs_stat ~= nil then
|
|
last_modified_a = a.fs_stat.mtime.sec
|
|
end
|
|
|
|
if b.fs_stat ~= nil then
|
|
last_modified_b = b.fs_stat.mtime.sec
|
|
end
|
|
|
|
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 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
|
|
elseif M.sort_by == "extension" then
|
|
M.node_comparator = M.node_comparator_extension
|
|
else
|
|
M.node_comparator = M.node_comparator_name_ignorecase
|
|
end
|
|
end
|
|
|
|
return M
|