Skip to content

Commit 3b0d1c0

Browse files
committed
fix: according to code review
1 parent 3bafb7a commit 3b0d1c0

File tree

4 files changed

+86
-84
lines changed

4 files changed

+86
-84
lines changed

lua/nvim-tree/explorer/explore.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local utils = require "nvim-tree.utils"
22
local builders = require "nvim-tree.explorer.node-builders"
33
local explorer_node = require "nvim-tree.explorer.node"
44
local git = require "nvim-tree.git"
5-
local Sorters = require "nvim-tree.explorer.sorters"
65
local filters = require "nvim-tree.explorer.filters"
76
local live_filter = require "nvim-tree.live-filter"
87
local log = require "nvim-tree.log"
@@ -57,7 +56,7 @@ end
5756
---@param node Node
5857
---@param status table
5958
---@return Node[]|nil
60-
function M.explore(node, status)
59+
function M.explore(node, status, explorer)
6160
local cwd = node.link_to or node.absolute_path
6261
local handle = vim.loop.fs_scandir(cwd)
6362
if not handle then
@@ -74,22 +73,21 @@ function M.explore(node, status)
7473
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
7574
local child_status = git.load_project_status(child_cwd)
7675
node.group_next = child_folder_only
77-
local ns = M.explore(child_folder_only, child_status)
76+
local ns = M.explore(child_folder_only, child_status, explorer)
7877
node.nodes = ns or {}
7978

8079
log.profile_end(profile)
8180
return ns
8281
end
8382

84-
M.sorters:sort(node.nodes)
83+
explorer.sorters:sort(node.nodes)
8584
live_filter.apply_filter(node)
8685

8786
log.profile_end(profile)
8887
return node.nodes
8988
end
9089

9190
function M.setup(opts)
92-
M.sorters = Sorters:new(opts)
9391
M.config = opts.renderer
9492
end
9593

lua/nvim-tree/explorer/init.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local git = require "nvim-tree.git"
22
local notify = require "nvim-tree.notify"
33
local watch = require "nvim-tree.explorer.watch"
44
local explorer_node = require "nvim-tree.explorer.node"
5+
local Sorters = require "nvim-tree.explorer.sorters"
56

67
local M = {}
78

@@ -36,6 +37,7 @@ function Explorer.new(path)
3637
absolute_path = path,
3738
nodes = {},
3839
open = true,
40+
sorters = Sorters:new(M.config)
3941
}, Explorer)
4042
explorer.watcher = watch.create_watcher(explorer)
4143
explorer:_load(explorer)
@@ -47,7 +49,7 @@ end
4749
function Explorer:_load(node)
4850
local cwd = node.link_to or node.absolute_path
4951
local git_status = git.load_project_status(cwd)
50-
M.explore(node, git_status)
52+
M.explore(node, git_status, self)
5153
end
5254

5355
---@param node Node
@@ -68,6 +70,7 @@ function Explorer:destroy()
6870
end
6971

7072
function M.setup(opts)
73+
M.config = opts
7174
require("nvim-tree.explorer.node").setup(opts)
7275
require("nvim-tree.explorer.explore").setup(opts)
7376
require("nvim-tree.explorer.filters").setup(opts)

lua/nvim-tree/explorer/reload.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local utils = require "nvim-tree.utils"
22
local builders = require "nvim-tree.explorer.node-builders"
33
local explorer_node = require "nvim-tree.explorer.node"
44
local filters = require "nvim-tree.explorer.filters"
5-
local Sorters = require "nvim-tree.explorer.sorters"
65
local live_filter = require "nvim-tree.live-filter"
76
local git = require "nvim-tree.git"
87
local log = require "nvim-tree.log"
@@ -162,7 +161,10 @@ function M.reload(node, git_status)
162161
return ns
163162
end
164163

165-
M.sorters:sort(node.nodes)
164+
local explorer = require("nvim-tree.core").get_explorer()
165+
if explorer then
166+
explorer.sorters:sort(node.nodes)
167+
end
166168
live_filter.apply_filter(node)
167169
log.profile_end(profile)
168170
return node.nodes
@@ -226,7 +228,6 @@ function M.refresh_parent_nodes_for_path(path)
226228
end
227229

228230
function M.setup(opts)
229-
M.sorters = Sorters:new(opts)
230231
M.config = opts.renderer
231232
end
232233

lua/nvim-tree/explorer/sorters.lua

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
local C = {}
22

3+
---@class Sorter
4+
local Sorter = {}
5+
6+
function Sorter:new(opts)
7+
local o = {} -- create object if user does not provide one
8+
setmetatable(o, self)
9+
self.__index = self
10+
o.config = vim.deepcopy(opts.sort)
11+
12+
if type(o.config.sorter) == "function" then
13+
o.user = o.config.sorter
14+
end
15+
return o
16+
end
17+
18+
--- Predefined comparator, defaulting to name
19+
---@param sorter string as per options
20+
---@return function
21+
function Sorter:get_comparator(sorter)
22+
return function(a, b)
23+
return (C[sorter] or C.name)(a, b, self.config)
24+
end
25+
end
26+
327
---Create a shallow copy of a portion of a list.
428
---@param t table
529
---@param first integer First index, inclusive
@@ -86,6 +110,57 @@ local function split_merge(t, first, last, comparator)
86110
merge(t, first, mid, last, comparator)
87111
end
88112

113+
---Perform a merge sort using sorter option.
114+
---@param t table nodes
115+
function Sorter:sort(t)
116+
if self.user then
117+
local t_user = {}
118+
local origin_index = {}
119+
120+
for _, n in ipairs(t) do
121+
table.insert(t_user, {
122+
absolute_path = n.absolute_path,
123+
executable = n.executable,
124+
extension = n.extension,
125+
filetype = vim.filetype.match { filename = n.name },
126+
link_to = n.link_to,
127+
name = n.name,
128+
type = n.type,
129+
})
130+
table.insert(origin_index, n)
131+
end
132+
133+
local predefined = self.user(t_user)
134+
if predefined then
135+
split_merge(t, 1, #t, self:get_comparator(predefined))
136+
return
137+
end
138+
139+
-- do merge sort for prevent memory exceed
140+
local user_index = {}
141+
for i, v in ipairs(t_user) do
142+
if type(v.absolute_path) == "string" and user_index[v.absolute_path] == nil then
143+
user_index[v.absolute_path] = i
144+
end
145+
end
146+
147+
-- if missing value found, then using origin_index
148+
local mini_comparator = function(a, b)
149+
local a_index = user_index[a.absolute_path] or origin_index[a.absolute_path]
150+
local b_index = user_index[b.absolute_path] or origin_index[b.absolute_path]
151+
152+
if type(a_index) == "number" and type(b_index) == "number" then
153+
return a_index <= b_index
154+
end
155+
return (a_index or 0) <= (b_index or 0)
156+
end
157+
158+
split_merge(t, 1, #t, mini_comparator) -- sort by user order
159+
else
160+
split_merge(t, 1, #t, self:get_comparator(self.config.sorter))
161+
end
162+
end
163+
89164
---@param a Node
90165
---@param b Node
91166
---@param ignorecase boolean|nil
@@ -238,79 +313,4 @@ function C.filetype(a, b, cfg)
238313
return a_ft < b_ft
239314
end
240315

241-
---@class Sorter
242-
local Sorter = {}
243-
244-
function Sorter:new(opts)
245-
local o = {} -- create object if user does not provide one
246-
setmetatable(o, self)
247-
self.__index = self
248-
o.config = opts.sort
249-
250-
if type(o.config.sorter) == "function" then
251-
o.user = o.config.sorter
252-
end
253-
return o
254-
end
255-
256-
--- Predefined comparator, defaulting to name
257-
---@param sorter string as per options
258-
---@return function
259-
function Sorter:get_comparator(sorter)
260-
return function(a, b)
261-
return (C[sorter] or C.name)(a, b, self.config)
262-
end
263-
end
264-
265-
---Perform a merge sort using sorter option.
266-
---@param t table nodes
267-
function Sorter:sort(t)
268-
if self.user then
269-
local t_user = {}
270-
local origin_index = {}
271-
272-
for _, n in ipairs(t) do
273-
table.insert(t_user, {
274-
absolute_path = n.absolute_path,
275-
executable = n.executable,
276-
extension = n.extension,
277-
filetype = vim.filetype.match { filename = n.name },
278-
link_to = n.link_to,
279-
name = n.name,
280-
type = n.type,
281-
})
282-
table.insert(origin_index, n)
283-
end
284-
285-
local predefined = self.user(t_user)
286-
if predefined then
287-
split_merge(t, 1, #t, self:get_comparator(predefined))
288-
return
289-
end
290-
291-
-- do merge sort for prevent memory exceed
292-
local user_index = {}
293-
for i, v in ipairs(t_user) do
294-
if type(v.absolute_path) == "string" and user_index[v.absolute_path] == nil then
295-
user_index[v.absolute_path] = i
296-
end
297-
end
298-
299-
-- if missing value found, then using origin_index
300-
local mini_comparator = function(a, b)
301-
local a_index = user_index[a.absolute_path] or origin_index[a.absolute_path]
302-
local b_index = user_index[b.absolute_path] or origin_index[b.absolute_path]
303-
304-
if type(a_index) == "number" and type(b_index) == "number" then
305-
return a_index <= b_index
306-
end
307-
return (a_index or 0) <= (b_index or 0)
308-
end
309-
310-
split_merge(t, 1, #t, mini_comparator) -- sort by user order
311-
else
312-
split_merge(t, 1, #t, self:get_comparator(self.config.sorter))
313-
end
314-
end
315-
316316
return Sorter

0 commit comments

Comments
 (0)