Skip to content

Commit d921a20

Browse files
committed
doc & fix: merge_sort problem fix & nil sorting
add complex example
1 parent d4f72d2 commit d921a20

File tree

2 files changed

+31
-78
lines changed

2 files changed

+31
-78
lines changed

doc/nvim-tree-lua.txt

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ Subsequent calls to setup will replace the previous configuration.
191191
number = false,
192192
relativenumber = false,
193193
signcolumn = "yes",
194-
-- @deprecated
195194
mappings = {
196195
custom_only = false,
197196
list = {
@@ -466,87 +465,37 @@ For example, to use simple user-defined sort_by function is done like this:
466465
return user_oder
467466
end
468467
469-
Furthermore, You can try more complex example for `rust` sort_by function is done like this:
468+
Furthermore, You can try more complex example for sort_by function is done like this:
470469

471470
>
472471
sort_by = function(tbl)
473-
local function tbl_slice(t, first, last)
474-
local slice = {}
475-
for i = first, last or #t, 1 do
476-
table.insert(slice, t[i])
477-
end
478-
return slice
479-
end
480-
481-
local function merge(t, first, mid, last, comparator)
482-
local n1 = mid - first + 1
483-
local n2 = last - mid
484-
local ls = tbl_slice(t, first, mid)
485-
local rs = tbl_slice(t, mid + 1, last)
486-
local i = 1
487-
local j = 1
488-
local k = first
489-
while i <= n1 and j <= n2 do
490-
if comparator(ls[i], rs[j]) then
491-
t[k] = ls[i]
492-
i = i + 1
493-
else
494-
t[k] = rs[j]
495-
j = j + 1
496-
end
497-
k = k + 1
498-
end
499-
while i <= n1 do
500-
t[k] = ls[i]
501-
i = i + 1
502-
k = k + 1
503-
end
504-
while j <= n2 do
505-
t[k] = rs[j]
506-
j = j + 1
507-
k = k + 1
508-
end
509-
end
510-
511-
local function split_merge(t, first, last, comparator)
512-
if (last - first) < 1 then
513-
return
514-
end
515-
516-
local mid = math.floor((first + last) / 2)
517-
518-
split_merge(t, first, mid, comparator)
519-
split_merge(t, mid + 1, last, comparator)
520-
merge(t, first, mid, last, comparator)
521-
end
522-
523-
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
472+
table.sort(tbl, function(a, b)
524473
if not (a and b) then
525474
return true
526475
end
527-
if a.nodes and not b.nodes then
476+
if a.type == "directory" and b.type ~= "directory" then
528477
return true
529-
elseif not a.nodes and b.nodes then
478+
elseif a.type ~= "directory" and b.type == "directory" then
530479
return false
531480
end
532481
533-
if ignorecase then
534-
return a.name:lower() <= b.name:lower()
535-
else
536-
return a.name <= b.name
482+
if a.type == "link" and b.type ~= "link" then
483+
return true
484+
elseif a.type ~= "link" and b.type == "link" then
485+
return false
537486
end
538-
end
539-
540487
541-
local comparartor = function(a, b) node_comparator_name_ignorecase_or_not(a, b, true) end
542-
split_merge(tbl, 1, #tbl, comparartor)
488+
return a.name:lower() <= b.name:lower()
489+
-- return a.name <= b.name
490+
end) -- NOTE: organize directory, link, file in name order
543491
492+
-- sort by name, is directory?
544493
local i = 1
545494
while i <= #tbl do
546-
if tbl[i] and tbl[i].nodes then
495+
if tbl[i] and tbl[i].type == "directory" then
547496
local j = i + 1
548497
while j <= #tbl do
549-
if tbl[j] and not tbl[j].nodes and tbl[i].name:lower() == tbl[j].name:lower():match "(.+)%..+$" then
498+
if tbl[j] and tbl[j].type ~= "directory" and tbl[i].name:lower() == tbl[j].name:lower():match "(.+)%..+$" then
550499
local change_target = tbl[j]
551500
table.remove(tbl, j)
552501
table.insert(tbl, i, change_target)
@@ -560,12 +509,12 @@ Furthermore, You can try more complex example for `rust` sort_by function is don
560509
561510
local user_order = {}
562511
for j = 1, #tbl do
563-
table.insert(user_order, tbl[j].absolute_path)
512+
if tbl[j] then
513+
table.insert(user_order, tbl[j].absolute_path)
514+
end
564515
end
565516
return user_order
566-
end,
567-
568-
517+
end
569518
570519
*nvim-tree.hijack_unnamed_buffer_when_opening*
571520
Opens in place of the unnamed buffer if it's empty.

lua/nvim-tree/explorer/sorters.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ end
6868
function M.merge_sort(t, comparator)
6969
if type(M.sort_by) == "function" then
7070
local t_user = {}
71+
local origin_index = {}
72+
7173
for _, n in ipairs(t) do
7274
table.insert(t_user, {
7375
absolute_path = n.absolute_path,
@@ -77,28 +79,30 @@ function M.merge_sort(t, comparator)
7779
name = n.name,
7880
type = n.type,
7981
})
82+
table.insert(origin_index, n)
8083
end
8184

8285
local user_order = M.sort_by(t_user)
8386

8487
if type(user_order) == "table" then
8588
-- do merge sort for prevent memory exceed
89+
8690
local user_index = {}
8791
for k, v in pairs(user_order) do
88-
if user_index[v] == nil then
92+
if type(v) == "string" and user_index[v] == nil then
8993
user_index[v] = k
9094
end
9195
end
96+
97+
-- if missing value found, then using origin_index
9298
local mini_comparator = function(a, b)
93-
local a_index = user_index[a.absolute_path]
94-
local b_index = user_index[b.absolute_path]
95-
if a_index == -1 or a_index == nil then
96-
a_index = 9999999999
97-
end
98-
if b_index == -1 or a_index == nil then
99-
b_index = 9999999999
99+
local a_index = user_index[a.absolute_path] or origin_index[a.absolute_path]
100+
local b_index = user_index[b.absolute_path] or origin_index[b.absolute_path]
101+
102+
if type(a_index) == "number" and type(b_index) == "number" then
103+
return a_index <= b_index
100104
end
101-
return a_index <= b_index
105+
return (a_index or 0) <= (b_index or 0)
102106
end
103107

104108
split_merge(t, 1, #t, mini_comparator) -- sort by user order

0 commit comments

Comments
 (0)