Skip to content

Commit d11d701

Browse files
feat(#2364): add option to sort files first (#2366)
* feat(#2364): add option to show files first * Refactor `folders_or_files_first` function * Improve readability * Remove `fallback` from `folders_or_files_first` function --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 807dc05 commit d11d701

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

doc/nvim-tree-lua.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ applying configuration.
308308
sort = {
309309
sorter = "name",
310310
folders_first = true,
311+
files_first = false,
311312
},
312313
root_dirs = {},
313314
prefer_startup_root = false,
@@ -589,6 +590,11 @@ File and folder sorting options.
589590
function.
590591
Type: `boolean`, Default: `true`
591592

593+
*nvim-tree.sort.files_first*
594+
Sort files before folders. Has no effect when |nvim-tree.sort.sorter| is a
595+
function. If set to `true` it overrides |nvim-tree.sort.folders_first|.
596+
Type: `boolean`, Default: `false`
597+
592598
*nvim-tree.hijack_unnamed_buffer_when_opening*
593599
Opens in place of the unnamed buffer if it's empty.
594600
Type: `boolean`, Default: `false`

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
372372
sort = {
373373
sorter = "name",
374374
folders_first = true,
375+
files_first = false,
375376
},
376377
root_dirs = {},
377378
prefer_startup_root = false,

lua/nvim-tree/explorer/sorters.lua

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ local function tbl_slice(t, first, last)
2323
return slice
2424
end
2525

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+
2644
local function merge(t, first, mid, last, comparator)
2745
local n1 = mid - first + 1
2846
local n2 = last - mid
@@ -124,12 +142,9 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
124142
return true
125143
end
126144

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
133148
end
134149

135150
if ignorecase then
@@ -152,12 +167,9 @@ function C.modification_time(a, b)
152167
return true
153168
end
154169

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
161173
end
162174

163175
local last_modified_a = 0
@@ -180,14 +192,11 @@ function C.suffix(a, b)
180192
end
181193

182194
-- 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)
191200
end
192201

193202
-- dotfiles go second
@@ -231,12 +240,9 @@ function C.extension(a, b)
231240
return true
232241
end
233242

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
240246
end
241247

242248
if a.extension and not b.extension then
@@ -259,12 +265,9 @@ function C.filetype(a, b)
259265
local b_ft = vim.filetype.match { filename = b.name }
260266

261267
-- 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
268271
end
269272

270273
-- one is nil, the other wins

0 commit comments

Comments
 (0)