feat: add sort_by option to sort files by modification time (#1040)

Co-authored-by: Brandon Dwiel <bdwiel@apple.com>
This commit is contained in:
Brandon D
2022-03-03 17:41:58 -05:00
committed by GitHub
parent 50a927f176
commit 690c7e96ed
4 changed files with 43 additions and 8 deletions

View File

@@ -5,9 +5,10 @@ local utils = require'nvim-tree.utils'
local M = {
ignore_list = {},
exclude_list = {},
node_comparator = nil,
}
function M.node_comparator(a, b)
function M.node_comparator_name(a, b)
if not (a and b) then
return true
end
@@ -20,6 +21,19 @@ function M.node_comparator(a, b)
return a.name:lower() <= b.name:lower()
end
function M.node_comparator_modification_time(a, b)
if not (a and b) then
return true
end
if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
end
return b.last_modified <= a.last_modified
end
---Check if the given path should be ignored.
---@param path string Absolute path
---@return boolean
@@ -73,6 +87,7 @@ function M.setup(opts)
filter_ignored = true,
filter_dotfiles = opts.filters.dotfiles,
filter_git_ignored = opts.git.ignore,
sort_by = opts.sort_by,
}
M.exclude_list = opts.filters.exclude
@@ -83,6 +98,12 @@ function M.setup(opts)
M.ignore_list[filter_name] = true
end
end
if M.config.sort_by == "modification_time" then
M.node_comparator = M.node_comparator_modification_time
else
M.node_comparator = M.node_comparator_name
end
end
return M