* feat(#2079): prefactor * feat(#2079): sort_by may return a predefined string
This commit is contained in:
committed by
GitHub
parent
56cdb5827d
commit
6ad5c26f4d
@@ -491,7 +491,8 @@ Can be one of `name`, `case_sensitive`, `modification_time`, `extension` or a
|
|||||||
function.
|
function.
|
||||||
Type: `string` | `function(nodes)`, Default: `"name"`
|
Type: `string` | `function(nodes)`, Default: `"name"`
|
||||||
|
|
||||||
Function is passed a table of nodes to be sorted, each node containing:
|
Function may perform a sort or return a string with one of the above
|
||||||
|
methods. It is passed a table of nodes to be sorted, each node containing:
|
||||||
- `absolute_path`: `string`
|
- `absolute_path`: `string`
|
||||||
- `executable`: `boolean`
|
- `executable`: `boolean`
|
||||||
- `extension`: `string`
|
- `extension`: `string`
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ function M.explore(node, status)
|
|||||||
return ns
|
return ns
|
||||||
end
|
end
|
||||||
|
|
||||||
sorters.merge_sort(node.nodes, sorters.node_comparator)
|
sorters.sort(node.nodes)
|
||||||
live_filter.apply_filter(node)
|
live_filter.apply_filter(node)
|
||||||
|
|
||||||
log.profile_end(profile)
|
log.profile_end(profile)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ function M.reload(node, git_status, unloaded_bufnr)
|
|||||||
return ns
|
return ns
|
||||||
end
|
end
|
||||||
|
|
||||||
sorters.merge_sort(node.nodes, sorters.node_comparator)
|
sorters.sort(node.nodes)
|
||||||
live_filter.apply_filter(node)
|
live_filter.apply_filter(node)
|
||||||
log.profile_end(profile)
|
log.profile_end(profile)
|
||||||
return node.nodes
|
return node.nodes
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
local M = {
|
local M = {}
|
||||||
sort_by = nil,
|
|
||||||
node_comparator = nil,
|
local C = {}
|
||||||
}
|
|
||||||
|
--- Predefined comparator, defaulting to name
|
||||||
|
--- @param sort_by string as per options
|
||||||
|
--- @return function
|
||||||
|
local function get_comparator(sort_by)
|
||||||
|
return C[sort_by] or C.name
|
||||||
|
end
|
||||||
|
|
||||||
---Create a shallow copy of a portion of a list.
|
---Create a shallow copy of a portion of a list.
|
||||||
---@param t table
|
---@param t table
|
||||||
@@ -62,11 +68,10 @@ local function split_merge(t, first, last, comparator)
|
|||||||
merge(t, first, mid, last, comparator)
|
merge(t, first, mid, last, comparator)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Perform a merge sort on a given list.
|
---Perform a merge sort using sort_by option.
|
||||||
---@param t any[]
|
---@param t table nodes
|
||||||
---@param comparator function|nil
|
function M.sort(t)
|
||||||
function M.merge_sort(t, comparator)
|
if C.user then
|
||||||
if type(M.sort_by) == "function" then
|
|
||||||
local t_user = {}
|
local t_user = {}
|
||||||
local origin_index = {}
|
local origin_index = {}
|
||||||
|
|
||||||
@@ -82,7 +87,11 @@ function M.merge_sort(t, comparator)
|
|||||||
table.insert(origin_index, n)
|
table.insert(origin_index, n)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.sort_by(t_user)
|
local predefined = C.user(t_user)
|
||||||
|
if predefined then
|
||||||
|
split_merge(t, 1, #t, get_comparator(predefined))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- do merge sort for prevent memory exceed
|
-- do merge sort for prevent memory exceed
|
||||||
local user_index = {}
|
local user_index = {}
|
||||||
@@ -105,12 +114,7 @@ function M.merge_sort(t, comparator)
|
|||||||
|
|
||||||
split_merge(t, 1, #t, mini_comparator) -- sort by user order
|
split_merge(t, 1, #t, mini_comparator) -- sort by user order
|
||||||
else
|
else
|
||||||
if not comparator then
|
split_merge(t, 1, #t, get_comparator(M.config.sort_by))
|
||||||
comparator = function(left, right)
|
|
||||||
return left < right
|
|
||||||
end
|
|
||||||
end
|
|
||||||
split_merge(t, 1, #t, comparator)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -131,15 +135,15 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.node_comparator_name_case_sensitive(a, b)
|
function C.case_sensitive(a, b)
|
||||||
return node_comparator_name_ignorecase_or_not(a, b, false)
|
return node_comparator_name_ignorecase_or_not(a, b, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.node_comparator_name_ignorecase(a, b)
|
function C.name(a, b)
|
||||||
return node_comparator_name_ignorecase_or_not(a, b, true)
|
return node_comparator_name_ignorecase_or_not(a, b, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.node_comparator_modification_time(a, b)
|
function C.modification_time(a, b)
|
||||||
if not (a and b) then
|
if not (a and b) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -163,7 +167,7 @@ function M.node_comparator_modification_time(a, b)
|
|||||||
return last_modified_b <= last_modified_a
|
return last_modified_b <= last_modified_a
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.node_comparator_extension(a, b)
|
function C.extension(a, b)
|
||||||
if not (a and b) then
|
if not (a and b) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -188,17 +192,11 @@ function M.node_comparator_extension(a, b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.sort_by = opts.sort_by
|
M.config = {}
|
||||||
if M.sort_by and type(M.sort_by) == "function" then
|
M.config.sort_by = opts.sort_by
|
||||||
M.node_comparator = M.sort_by
|
|
||||||
elseif M.sort_by == "modification_time" then
|
if type(opts.sort_by) == "function" then
|
||||||
M.node_comparator = M.node_comparator_modification_time
|
C.user = opts.sort_by
|
||||||
elseif M.sort_by == "case_sensitive" then
|
|
||||||
M.node_comparator = M.node_comparator_name_case_sensitive
|
|
||||||
elseif M.sort_by == "extension" then
|
|
||||||
M.node_comparator = M.node_comparator_extension
|
|
||||||
else
|
|
||||||
M.node_comparator = M.node_comparator_name_ignorecase
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user