Skip to content

Commit 82c868f

Browse files
committed
clean up sorters
1 parent b5f2b3a commit 82c868f

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function Explorer:create(path)
6969
o.open = true
7070
o.opts = config
7171

72-
o.sorters = Sorters:new(config)
72+
o.sorters = Sorters:create(config)
7373
o.renderer = Renderer:new(config, o)
7474
o.filters = Filters:new(config, o)
7575
o.live_filter = LiveFilter:new(config, o)

lua/nvim-tree/explorer/sorters.lua

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
local C = {}
2-
3-
---@class Sorter
4-
local Sorter = {}
1+
local Class = require("nvim-tree.class")
2+
local DirectoryNode = require("nvim-tree.node.directory")
53

6-
function Sorter:new(opts)
7-
local o = {}
8-
setmetatable(o, self)
9-
self.__index = self
10-
o.config = vim.deepcopy(opts.sort)
4+
local C = {}
115

12-
if type(o.config.sorter) == "function" then
13-
o.user = o.config.sorter
6+
---@class (exact) SorterCfg
7+
---@field sorter string|fun(nodes: Node[])
8+
---@field folders_first boolean
9+
---@field files_first boolean
10+
11+
---@class (exact) Sorter: Class
12+
---@field cfg SorterCfg
13+
---@field user fun(nodes: Node[])?
14+
---@field pre string?
15+
local Sorter = Class:new()
16+
17+
---@param opts table user options
18+
---@return Sorter
19+
function Sorter:create(opts)
20+
---@type Sorter
21+
local o = {
22+
cfg = vim.deepcopy(opts.sort),
23+
}
24+
o = self:new(o)
25+
26+
if type(o.cfg.sorter) == "function" then
27+
o.user = o.cfg.sorter --[[@as fun(nodes: Node[])]]
28+
elseif type(o.cfg.sorter) == "string" then
29+
o.pre = o.cfg.sorter --[[@as string]]
1430
end
1531
return o
1632
end
@@ -20,7 +36,7 @@ end
2036
---@return fun(a: Node, b: Node): boolean
2137
function Sorter:get_comparator(sorter)
2238
return function(a, b)
23-
return (C[sorter] or C.name)(a, b, self.config)
39+
return (C[sorter] or C.name)(a, b, self.cfg)
2440
end
2541
end
2642

@@ -41,17 +57,17 @@ end
4157
---Evaluate `sort.folders_first` and `sort.files_first`
4258
---@param a Node
4359
---@param b Node
44-
---@param cfg table
60+
---@param cfg SorterCfg
4561
---@return boolean|nil
4662
local function folders_or_files_first(a, b, cfg)
4763
if not (cfg.folders_first or cfg.files_first) then
4864
return
4965
end
5066

51-
if not a.nodes and b.nodes then
67+
if not a:is(DirectoryNode) and b:is(DirectoryNode) then
5268
-- file <> folder
5369
return cfg.files_first
54-
elseif a.nodes and not b.nodes then
70+
elseif a:is(DirectoryNode) and not b:is(DirectoryNode) then
5571
-- folder <> file
5672
return not cfg.files_first
5773
end
@@ -157,15 +173,15 @@ function Sorter:sort(t)
157173
end
158174

159175
split_merge(t, 1, #t, mini_comparator) -- sort by user order
160-
else
161-
split_merge(t, 1, #t, self:get_comparator(self.config.sorter))
176+
elseif self.pre then
177+
split_merge(t, 1, #t, self:get_comparator(self.pre))
162178
end
163179
end
164180

165181
---@param a Node
166182
---@param b Node
167183
---@param ignorecase boolean|nil
168-
---@param cfg table
184+
---@param cfg SorterCfg
169185
---@return boolean
170186
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase, cfg)
171187
if not (a and b) then

0 commit comments

Comments
 (0)