Skip to content

Commit 690c7e9

Browse files
bdwielBrandon Dwiel
and
Brandon Dwiel
authored
feat: add sort_by option to sort files by modification time (#1040)
Co-authored-by: Brandon Dwiel <bdwiel@apple.com>
1 parent 50a927f commit 690c7e9

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

doc/nvim-tree-lua.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function.
8383
auto_close = false,
8484
auto_reload_on_write = true,
8585
open_on_tab = false,
86+
sort_by = "name",
8687
hijack_cursor = false,
8788
update_cwd = false,
8889
hijack_unnamed_buffer_when_opening = false,
@@ -201,6 +202,12 @@ Here is a list of the options available in the setup call:
201202
type: `boolean`
202203
default: `false`
203204

205+
*nvim-tree.sort_by*
206+
- |sort_by|: changes how files within the same directory are sorted. can be
207+
one of 'name' | 'modification_time'
208+
type: `string`
209+
default: `"name"`
210+
204211
*nvim-tree.hijack_unnamed_buffer_when_opening*
205212
- |hijack_unnamed_buffer_when_opening|: opens in place of the unnamed
206213
buffer if it's empty.

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ local DEFAULT_OPTS = {
339339
ignore_buffer_on_setup = false,
340340
open_on_setup = false,
341341
open_on_tab = false,
342+
sort_by = "name",
342343
update_cwd = false,
343344
hijack_directories = {
344345
enable = true,

lua/nvim-tree/explorer/node-builders.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ local M = {
55
is_windows = vim.fn.has('win32') == 1
66
}
77

8+
local function get_last_modified(absolute_path)
9+
local stat = uv.fs_stat(absolute_path)
10+
11+
local last_modified = 0
12+
if stat ~= nil then
13+
last_modified = stat.mtime.sec
14+
end
15+
return last_modified
16+
end
17+
818
function M.get_dir_git_status(parent_ignored, status, absolute_path)
919
if parent_ignored then
1020
return '!!'
@@ -30,6 +40,7 @@ function M.folder(absolute_path, name, status, parent_ignored)
3040
name = name,
3141
nodes = {},
3242
open = false,
43+
last_modified = get_last_modified(absolute_path),
3344
}
3445
end
3546

@@ -49,6 +60,7 @@ function M.file(absolute_path, name, status, parent_ignored)
4960
extension = ext,
5061
git_status = M.get_git_status(parent_ignored, status, absolute_path),
5162
name = name,
63+
last_modified = get_last_modified(absolute_path),
5264
}
5365
end
5466

@@ -60,23 +72,17 @@ end
6072
function M.link(absolute_path, name, status, parent_ignored)
6173
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
6274
local link_to = uv.fs_realpath(absolute_path)
63-
local stat = uv.fs_stat(absolute_path)
6475
local open, nodes
6576
if (link_to ~= nil) and uv.fs_stat(link_to).type == 'directory' then
6677
open = false
6778
nodes = {}
6879
end
6980

70-
local last_modified = 0
71-
if stat ~= nil then
72-
last_modified = stat.mtime.sec
73-
end
74-
7581
return {
7682
absolute_path = absolute_path,
7783
git_status = M.get_git_status(parent_ignored, status, absolute_path),
7884
group_next = nil, -- If node is grouped, this points to the next child dir/link node
79-
last_modified = last_modified,
85+
last_modified = get_last_modified(absolute_path),
8086
link_to = link_to,
8187
name = name,
8288
nodes = nodes,

lua/nvim-tree/explorer/utils.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ local utils = require'nvim-tree.utils'
55
local M = {
66
ignore_list = {},
77
exclude_list = {},
8+
node_comparator = nil,
89
}
910

10-
function M.node_comparator(a, b)
11+
function M.node_comparator_name(a, b)
1112
if not (a and b) then
1213
return true
1314
end
@@ -20,6 +21,19 @@ function M.node_comparator(a, b)
2021
return a.name:lower() <= b.name:lower()
2122
end
2223

24+
function M.node_comparator_modification_time(a, b)
25+
if not (a and b) then
26+
return true
27+
end
28+
if a.nodes and not b.nodes then
29+
return true
30+
elseif not a.nodes and b.nodes then
31+
return false
32+
end
33+
34+
return b.last_modified <= a.last_modified
35+
end
36+
2337
---Check if the given path should be ignored.
2438
---@param path string Absolute path
2539
---@return boolean
@@ -73,6 +87,7 @@ function M.setup(opts)
7387
filter_ignored = true,
7488
filter_dotfiles = opts.filters.dotfiles,
7589
filter_git_ignored = opts.git.ignore,
90+
sort_by = opts.sort_by,
7691
}
7792

7893
M.exclude_list = opts.filters.exclude
@@ -83,6 +98,12 @@ function M.setup(opts)
8398
M.ignore_list[filter_name] = true
8499
end
85100
end
101+
102+
if M.config.sort_by == "modification_time" then
103+
M.node_comparator = M.node_comparator_modification_time
104+
else
105+
M.node_comparator = M.node_comparator_name
106+
end
86107
end
87108

88109
return M

0 commit comments

Comments
 (0)