@@ -23,6 +23,24 @@ local function tbl_slice(t, first, last)
23
23
return slice
24
24
end
25
25
26
+ --- Evaluate `sort.folders_first` and `sort.files_first`
27
+ --- @param a table node
28
+ --- @param b table node
29
+ --- @return boolean | nil
30
+ local function folders_or_files_first (a , b )
31
+ if not (M .config .sort .folders_first or M .config .sort .files_first ) then
32
+ return
33
+ end
34
+
35
+ if not a .nodes and b .nodes then
36
+ -- file <> folder
37
+ return M .config .sort .files_first
38
+ elseif a .nodes and not b .nodes then
39
+ -- folder <> file
40
+ return not M .config .sort .files_first
41
+ end
42
+ end
43
+
26
44
local function merge (t , first , mid , last , comparator )
27
45
local n1 = mid - first + 1
28
46
local n2 = last - mid
@@ -124,12 +142,9 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
124
142
return true
125
143
end
126
144
127
- if M .config .sort .folders_first then
128
- if a .nodes and not b .nodes then
129
- return true
130
- elseif not a .nodes and b .nodes then
131
- return false
132
- end
145
+ local early_return = folders_or_files_first (a , b )
146
+ if early_return ~= nil then
147
+ return early_return
133
148
end
134
149
135
150
if ignorecase then
@@ -152,12 +167,9 @@ function C.modification_time(a, b)
152
167
return true
153
168
end
154
169
155
- if M .config .sort .folders_first then
156
- if a .nodes and not b .nodes then
157
- return true
158
- elseif not a .nodes and b .nodes then
159
- return false
160
- end
170
+ local early_return = folders_or_files_first (a , b )
171
+ if early_return ~= nil then
172
+ return early_return
161
173
end
162
174
163
175
local last_modified_a = 0
@@ -180,14 +192,11 @@ function C.suffix(a, b)
180
192
end
181
193
182
194
-- directories go first
183
- if M .config .sort .folders_first then
184
- if a .nodes and not b .nodes then
185
- return true
186
- elseif not a .nodes and b .nodes then
187
- return false
188
- elseif a .nodes and b .nodes then
189
- return C .name (a , b )
190
- end
195
+ local early_return = folders_or_files_first (a , b )
196
+ if early_return ~= nil then
197
+ return early_return
198
+ elseif a .nodes and b .nodes then
199
+ return C .name (a , b )
191
200
end
192
201
193
202
-- dotfiles go second
@@ -231,12 +240,9 @@ function C.extension(a, b)
231
240
return true
232
241
end
233
242
234
- if M .config .sort .folders_first then
235
- if a .nodes and not b .nodes then
236
- return true
237
- elseif not a .nodes and b .nodes then
238
- return false
239
- end
243
+ local early_return = folders_or_files_first (a , b )
244
+ if early_return ~= nil then
245
+ return early_return
240
246
end
241
247
242
248
if a .extension and not b .extension then
@@ -259,12 +265,9 @@ function C.filetype(a, b)
259
265
local b_ft = vim .filetype .match { filename = b .name }
260
266
261
267
-- directories first
262
- if M .config .sort .folders_first then
263
- if a .nodes and not b .nodes then
264
- return true
265
- elseif not a .nodes and b .nodes then
266
- return false
267
- end
268
+ local early_return = folders_or_files_first (a , b )
269
+ if early_return ~= nil then
270
+ return early_return
268
271
end
269
272
270
273
-- one is nil, the other wins
0 commit comments