Skip to content

Commit dfbfec6

Browse files
committed
fix: sort_by does not return anything
1 parent 26e4066 commit dfbfec6

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

doc/nvim-tree-lua.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,26 +429,19 @@ Can be one of `name`, `case_sensitive`, `modification_time`, `extension` or a
429429
function.
430430
Type: `string` | `function(nodes)`, Default: `"name"`
431431

432-
Function is passed a table of nodes, each node containing:
432+
Function is passed a table of nodes to be sorted, each node containing:
433433
- `absolute_path`: `string`
434434
- `executable`: `boolean`
435435
- `extension`: `string`
436436
- `link_to`: `string`
437437
- `name`: `string`
438438
- `type`: `"directory"` | `"file"` | `"link"`
439439

440-
Function will return a table of `absolute_path`
441-
442440
Example: sort by name length: >
443441
local sort_by = function(nodes)
444442
table.sort(nodes, function(a, b)
445443
return #a.name < #b.name
446444
end)
447-
local order = {}
448-
for _, n in ipairs(nodes) do
449-
table.insert(order, n.absolute_path)
450-
end
451-
return order
452445
end
453446
<
454447
*nvim-tree.hijack_unnamed_buffer_when_opening*

lua/nvim-tree/explorer/sorters.lua

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,28 @@ function M.merge_sort(t, comparator)
8282
table.insert(origin_index, n)
8383
end
8484

85-
local user_order = M.sort_by(t_user)
85+
M.sort_by(t_user)
8686

87-
if type(user_order) == "table" then
88-
-- do merge sort for prevent memory exceed
89-
90-
local user_index = {}
91-
for k, v in pairs(user_order) do
92-
if type(v) == "string" and user_index[v] == nil then
93-
user_index[v] = k
94-
end
87+
-- do merge sort for prevent memory exceed
88+
local user_index = {}
89+
for i, v in ipairs(t_user) do
90+
if type(v.absolute_path) == "string" and user_index[v.absolute_path] == nil then
91+
user_index[v.absolute_path] = i
9592
end
93+
end
9694

97-
-- if missing value found, then using origin_index
98-
local mini_comparator = function(a, b)
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]
95+
-- if missing value found, then using origin_index
96+
local mini_comparator = function(a, b)
97+
local a_index = user_index[a.absolute_path] or origin_index[a.absolute_path]
98+
local b_index = user_index[b.absolute_path] or origin_index[b.absolute_path]
10199

102-
if type(a_index) == "number" and type(b_index) == "number" then
103-
return a_index <= b_index
104-
end
105-
return (a_index or 0) <= (b_index or 0)
100+
if type(a_index) == "number" and type(b_index) == "number" then
101+
return a_index <= b_index
106102
end
107-
108-
split_merge(t, 1, #t, mini_comparator) -- sort by user order
103+
return (a_index or 0) <= (b_index or 0)
109104
end
105+
106+
split_merge(t, 1, #t, mini_comparator) -- sort by user order
110107
else
111108
if not comparator then
112109
comparator = function(left, right)

0 commit comments

Comments
 (0)