Skip to content

Commit 3676e0b

Browse files
feat(sorters): allow user sort_by
* 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 <[email protected]>
1 parent fb8735e commit 3676e0b

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

doc/nvim-tree-lua.txt

+19-4
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 = {
@@ -426,9 +425,25 @@ if the tree was previously open.
426425

427426
*nvim-tree.sort_by*
428427
Changes how files within the same directory are sorted.
429-
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension'.
430-
Type: `string`, Default: `"name"`
431-
428+
Can be one of `name`, `case_sensitive`, `modification_time`, `extension` or a
429+
function.
430+
Type: `string` | `function(nodes)`, Default: `"name"`
431+
432+
Function is passed a table of nodes to be sorted, each node containing:
433+
- `absolute_path`: `string`
434+
- `executable`: `boolean`
435+
- `extension`: `string`
436+
- `link_to`: `string`
437+
- `name`: `string`
438+
- `type`: `"directory"` | `"file"` | `"link"`
439+
440+
Example: sort by name length: >
441+
local sort_by = function(nodes)
442+
table.sort(nodes, function(a, b)
443+
return #a.name < #b.name
444+
end)
445+
end
446+
<
432447
*nvim-tree.hijack_unnamed_buffer_when_opening*
433448
Opens in place of the unnamed buffer if it's empty.
434449
Type: `boolean`, Default: `false`

lua/nvim-tree.lua

+1
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
640640
height = { string = true, ["function"] = true, number = true },
641641
remove_keymaps = { boolean = true, table = true },
642642
on_attach = { ["function"] = true, string = true },
643+
sort_by = { ["function"] = true, string = true },
643644
}
644645

645646
local function validate_options(conf)

lua/nvim-tree/explorer/sorters.lua

+47-6
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,52 @@ end
6666
---@param t any[]
6767
---@param comparator function|nil
6868
function M.merge_sort(t, comparator)
69-
if not comparator then
70-
comparator = function(left, right)
71-
return left < right
69+
if type(M.sort_by) == "function" then
70+
local t_user = {}
71+
local origin_index = {}
72+
73+
for _, n in ipairs(t) do
74+
table.insert(t_user, {
75+
absolute_path = n.absolute_path,
76+
executable = n.executable,
77+
extension = n.extension,
78+
link_to = n.link_to,
79+
name = n.name,
80+
type = n.type,
81+
})
82+
table.insert(origin_index, n)
7283
end
73-
end
7484

75-
split_merge(t, 1, #t, comparator)
85+
M.sort_by(t_user)
86+
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
92+
end
93+
end
94+
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]
99+
100+
if type(a_index) == "number" and type(b_index) == "number" then
101+
return a_index <= b_index
102+
end
103+
return (a_index or 0) <= (b_index or 0)
104+
end
105+
106+
split_merge(t, 1, #t, mini_comparator) -- sort by user order
107+
else
108+
if not comparator then
109+
comparator = function(left, right)
110+
return left < right
111+
end
112+
end
113+
split_merge(t, 1, #t, comparator)
114+
end
76115
end
77116

78117
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
@@ -150,7 +189,9 @@ end
150189

151190
function M.setup(opts)
152191
M.sort_by = opts.sort_by
153-
if M.sort_by == "modification_time" then
192+
if M.sort_by and type(M.sort_by) == "function" then
193+
M.node_comparator = M.sort_by
194+
elseif M.sort_by == "modification_time" then
154195
M.node_comparator = M.node_comparator_modification_time
155196
elseif M.sort_by == "case_sensitive" then
156197
M.node_comparator = M.node_comparator_name_case_sensisive

0 commit comments

Comments
 (0)