Skip to content

Commit ef305a8

Browse files
authored
feat(#2313): sort_by -> sort.sorter, add sort.folders_first default true (#2314)
* feat(#2313): add sort_folders_first, default true * feat(#2313): add sort.sorter, sort.folders_firs
1 parent a708bd2 commit ef305a8

File tree

4 files changed

+89
-58
lines changed

4 files changed

+89
-58
lines changed

doc/nvim-tree-lua.txt

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ Setup the plugin in your `init.lua` >
9595
9696
-- OR setup with some options
9797
require("nvim-tree").setup({
98-
sort_by = "case_sensitive",
98+
sort = {
99+
sorter = "case_sensitive",
100+
},
99101
view = {
100102
width = 30,
101103
},
@@ -312,7 +314,10 @@ applying configuration.
312314
hijack_cursor = false,
313315
hijack_netrw = true,
314316
hijack_unnamed_buffer_when_opening = false,
315-
sort_by = "name",
317+
sort = {
318+
sorter = "name",
319+
folders_first = true,
320+
},
316321
root_dirs = {},
317322
prefer_startup_root = false,
318323
sync_root_with_cwd = false,
@@ -566,31 +571,39 @@ Hijack netrw windows (overridden if |disable_netrw| is `true`)
566571
Reloads the explorer every time a buffer is written to.
567572
Type: `boolean`, Default: `true`
568573

569-
*nvim-tree.sort_by*
570-
Changes how files within the same directory are sorted.
571-
Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`,
572-
`"suffix"`, `"filetype"` or a function.
573-
`"extension"` uses all suffixes e.g. `foo.tar.gz` -> `.tar.gz`
574-
`"suffix"` uses the last e.g. `.gz`
575-
Type: `string` | `function(nodes)`, Default: `"name"`
576-
577-
Function may perform a sort or return a string with one of the above
578-
methods. It is passed a table of nodes to be sorted, each node containing:
579-
- `absolute_path`: `string`
580-
- `executable`: `boolean`
581-
- `extension`: `string`
582-
- `filetype`: `string`
583-
- `link_to`: `string`
584-
- `name`: `string`
585-
- `type`: `"directory"` | `"file"` | `"link"`
586-
587-
Example: sort by name length: >
588-
local sort_by = function(nodes)
589-
table.sort(nodes, function(a, b)
590-
return #a.name < #b.name
591-
end)
592-
end
574+
*nvim-tree.sort*
575+
File and folder sorting options.
576+
577+
*nvim-tree.sort.sorter* (previously `sort_by`)
578+
Changes how files within the same directory are sorted.
579+
Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`,
580+
`"suffix"`, `"filetype"` or a function.
581+
`"extension"` uses all suffixes e.g. `foo.tar.gz` -> `.tar.gz`
582+
`"suffix"` uses the last e.g. `.gz`
583+
Type: `string` | `function(nodes)`, Default: `"name"`
584+
585+
Function may perform a sort or return a string with one of the above
586+
methods. It is passed a table of nodes to be sorted, each node containing:
587+
- `absolute_path`: `string`
588+
- `executable`: `boolean`
589+
- `extension`: `string`
590+
- `filetype`: `string`
591+
- `link_to`: `string`
592+
- `name`: `string`
593+
- `type`: `"directory"` | `"file"` | `"link"`
594+
595+
Example: sort by name length: >
596+
local sorter = function(nodes)
597+
table.sort(nodes, function(a, b)
598+
return #a.name < #b.name
599+
end)
600+
end
593601
<
602+
*nvim-tree.sort.sort_folders_first*
603+
Sort folders before files. Has no effect when |nvim-tree.sort.sorter| is a
604+
function.
605+
Type: `boolean`, Default: `true`
606+
594607
*nvim-tree.hijack_unnamed_buffer_when_opening*
595608
Opens in place of the unnamed buffer if it's empty.
596609
Type: `boolean`, Default: `false`

lua/nvim-tree.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
366366
hijack_cursor = false,
367367
hijack_netrw = true,
368368
hijack_unnamed_buffer_when_opening = false,
369-
sort_by = "name",
369+
sort = {
370+
sorter = "name",
371+
folders_first = true,
372+
},
370373
root_dirs = {},
371374
prefer_startup_root = false,
372375
sync_root_with_cwd = false,
@@ -613,7 +616,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
613616
min = { string = true, ["function"] = true, number = true },
614617
remove_keymaps = { boolean = true, table = true },
615618
on_attach = { ["function"] = true, string = true },
616-
sort_by = { ["function"] = true, string = true },
619+
sorter = { ["function"] = true, string = true },
617620
root_folder_label = { ["function"] = true, string = true, boolean = true },
618621
picker = { ["function"] = true, string = true },
619622
}

lua/nvim-tree/explorer/sorters.lua

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ local M = {}
33
local C = {}
44

55
--- Predefined comparator, defaulting to name
6-
--- @param sort_by string as per options
6+
--- @param sorter string as per options
77
--- @return function
8-
local function get_comparator(sort_by)
9-
return C[sort_by] or C.name
8+
local function get_comparator(sorter)
9+
return C[sorter] or C.name
1010
end
1111

1212
---Create a shallow copy of a portion of a list.
@@ -68,7 +68,7 @@ local function split_merge(t, first, last, comparator)
6868
merge(t, first, mid, last, comparator)
6969
end
7070

71-
---Perform a merge sort using sort_by option.
71+
---Perform a merge sort using sorter option.
7272
---@param t table nodes
7373
function M.sort(t)
7474
if C.user then
@@ -115,18 +115,21 @@ function M.sort(t)
115115

116116
split_merge(t, 1, #t, mini_comparator) -- sort by user order
117117
else
118-
split_merge(t, 1, #t, get_comparator(M.config.sort_by))
118+
split_merge(t, 1, #t, get_comparator(M.config.sort.sorter))
119119
end
120120
end
121121

122122
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
123123
if not (a and b) then
124124
return true
125125
end
126-
if a.nodes and not b.nodes then
127-
return true
128-
elseif not a.nodes and b.nodes then
129-
return false
126+
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
130133
end
131134

132135
if ignorecase then
@@ -148,10 +151,13 @@ function C.modification_time(a, b)
148151
if not (a and b) then
149152
return true
150153
end
151-
if a.nodes and not b.nodes then
152-
return true
153-
elseif not a.nodes and b.nodes then
154-
return false
154+
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
155161
end
156162

157163
local last_modified_a = 0
@@ -174,12 +180,14 @@ function C.suffix(a, b)
174180
end
175181

176182
-- directories go first
177-
if a.nodes and not b.nodes then
178-
return true
179-
elseif not a.nodes and b.nodes then
180-
return false
181-
elseif a.nodes and b.nodes then
182-
return C.name(a, b)
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
183191
end
184192

185193
-- dotfiles go second
@@ -223,10 +231,12 @@ function C.extension(a, b)
223231
return true
224232
end
225233

226-
if a.nodes and not b.nodes then
227-
return true
228-
elseif not a.nodes and b.nodes then
229-
return false
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
230240
end
231241

232242
if a.extension and not b.extension then
@@ -249,10 +259,12 @@ function C.filetype(a, b)
249259
local b_ft = vim.filetype.match { filename = b.name }
250260

251261
-- directories first
252-
if a.nodes and not b.nodes then
253-
return true
254-
elseif not a.nodes and b.nodes then
255-
return false
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
256268
end
257269

258270
-- one is nil, the other wins
@@ -272,10 +284,10 @@ end
272284

273285
function M.setup(opts)
274286
M.config = {}
275-
M.config.sort_by = opts.sort_by
287+
M.config.sort = opts.sort
276288

277-
if type(opts.sort_by) == "function" then
278-
C.user = opts.sort_by
289+
if type(M.config.sort.sorter) == "function" then
290+
C.user = M.config.sort.sorter
279291
end
280292
end
281293

lua/nvim-tree/legacy.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ local function refactored(opts)
4141
end
4242
opts.view.adaptive_size = nil
4343
end
44+
45+
-- 2023/07/15
46+
utils.move_missing_val(opts, "", "sort_by", opts, "sort", "sorter", true)
4447
end
4548

4649
local function deprecated(opts)

0 commit comments

Comments
 (0)