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:
parent
50a927f176
commit
690c7e96ed
@ -83,6 +83,7 @@ function.
|
|||||||
auto_close = false,
|
auto_close = false,
|
||||||
auto_reload_on_write = true,
|
auto_reload_on_write = true,
|
||||||
open_on_tab = false,
|
open_on_tab = false,
|
||||||
|
sort_by = "name",
|
||||||
hijack_cursor = false,
|
hijack_cursor = false,
|
||||||
update_cwd = false,
|
update_cwd = false,
|
||||||
hijack_unnamed_buffer_when_opening = false,
|
hijack_unnamed_buffer_when_opening = false,
|
||||||
@ -201,6 +202,12 @@ Here is a list of the options available in the setup call:
|
|||||||
type: `boolean`
|
type: `boolean`
|
||||||
default: `false`
|
default: `false`
|
||||||
|
|
||||||
|
*nvim-tree.sort_by*
|
||||||
|
- |sort_by|: changes how files within the same directory are sorted. can be
|
||||||
|
one of 'name' | 'modification_time'
|
||||||
|
type: `string`
|
||||||
|
default: `"name"`
|
||||||
|
|
||||||
*nvim-tree.hijack_unnamed_buffer_when_opening*
|
*nvim-tree.hijack_unnamed_buffer_when_opening*
|
||||||
- |hijack_unnamed_buffer_when_opening|: opens in place of the unnamed
|
- |hijack_unnamed_buffer_when_opening|: opens in place of the unnamed
|
||||||
buffer if it's empty.
|
buffer if it's empty.
|
||||||
|
|||||||
@ -339,6 +339,7 @@ local DEFAULT_OPTS = {
|
|||||||
ignore_buffer_on_setup = false,
|
ignore_buffer_on_setup = false,
|
||||||
open_on_setup = false,
|
open_on_setup = false,
|
||||||
open_on_tab = false,
|
open_on_tab = false,
|
||||||
|
sort_by = "name",
|
||||||
update_cwd = false,
|
update_cwd = false,
|
||||||
hijack_directories = {
|
hijack_directories = {
|
||||||
enable = true,
|
enable = true,
|
||||||
|
|||||||
@ -5,6 +5,16 @@ local M = {
|
|||||||
is_windows = vim.fn.has('win32') == 1
|
is_windows = vim.fn.has('win32') == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local function get_last_modified(absolute_path)
|
||||||
|
local stat = uv.fs_stat(absolute_path)
|
||||||
|
|
||||||
|
local last_modified = 0
|
||||||
|
if stat ~= nil then
|
||||||
|
last_modified = stat.mtime.sec
|
||||||
|
end
|
||||||
|
return last_modified
|
||||||
|
end
|
||||||
|
|
||||||
function M.get_dir_git_status(parent_ignored, status, absolute_path)
|
function M.get_dir_git_status(parent_ignored, status, absolute_path)
|
||||||
if parent_ignored then
|
if parent_ignored then
|
||||||
return '!!'
|
return '!!'
|
||||||
@ -30,6 +40,7 @@ function M.folder(absolute_path, name, status, parent_ignored)
|
|||||||
name = name,
|
name = name,
|
||||||
nodes = {},
|
nodes = {},
|
||||||
open = false,
|
open = false,
|
||||||
|
last_modified = get_last_modified(absolute_path),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -49,6 +60,7 @@ function M.file(absolute_path, name, status, parent_ignored)
|
|||||||
extension = ext,
|
extension = ext,
|
||||||
git_status = M.get_git_status(parent_ignored, status, absolute_path),
|
git_status = M.get_git_status(parent_ignored, status, absolute_path),
|
||||||
name = name,
|
name = name,
|
||||||
|
last_modified = get_last_modified(absolute_path),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -60,23 +72,17 @@ end
|
|||||||
function M.link(absolute_path, name, status, parent_ignored)
|
function M.link(absolute_path, name, status, parent_ignored)
|
||||||
--- 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.
|
--- 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.
|
||||||
local link_to = uv.fs_realpath(absolute_path)
|
local link_to = uv.fs_realpath(absolute_path)
|
||||||
local stat = uv.fs_stat(absolute_path)
|
|
||||||
local open, nodes
|
local open, nodes
|
||||||
if (link_to ~= nil) and uv.fs_stat(link_to).type == 'directory' then
|
if (link_to ~= nil) and uv.fs_stat(link_to).type == 'directory' then
|
||||||
open = false
|
open = false
|
||||||
nodes = {}
|
nodes = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local last_modified = 0
|
|
||||||
if stat ~= nil then
|
|
||||||
last_modified = stat.mtime.sec
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
absolute_path = absolute_path,
|
absolute_path = absolute_path,
|
||||||
git_status = M.get_git_status(parent_ignored, status, absolute_path),
|
git_status = M.get_git_status(parent_ignored, status, absolute_path),
|
||||||
group_next = nil, -- If node is grouped, this points to the next child dir/link node
|
group_next = nil, -- If node is grouped, this points to the next child dir/link node
|
||||||
last_modified = last_modified,
|
last_modified = get_last_modified(absolute_path),
|
||||||
link_to = link_to,
|
link_to = link_to,
|
||||||
name = name,
|
name = name,
|
||||||
nodes = nodes,
|
nodes = nodes,
|
||||||
|
|||||||
@ -5,9 +5,10 @@ local utils = require'nvim-tree.utils'
|
|||||||
local M = {
|
local M = {
|
||||||
ignore_list = {},
|
ignore_list = {},
|
||||||
exclude_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
|
if not (a and b) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -20,6 +21,19 @@ function M.node_comparator(a, b)
|
|||||||
return a.name:lower() <= b.name:lower()
|
return a.name:lower() <= b.name:lower()
|
||||||
end
|
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.
|
---Check if the given path should be ignored.
|
||||||
---@param path string Absolute path
|
---@param path string Absolute path
|
||||||
---@return boolean
|
---@return boolean
|
||||||
@ -73,6 +87,7 @@ function M.setup(opts)
|
|||||||
filter_ignored = true,
|
filter_ignored = true,
|
||||||
filter_dotfiles = opts.filters.dotfiles,
|
filter_dotfiles = opts.filters.dotfiles,
|
||||||
filter_git_ignored = opts.git.ignore,
|
filter_git_ignored = opts.git.ignore,
|
||||||
|
sort_by = opts.sort_by,
|
||||||
}
|
}
|
||||||
|
|
||||||
M.exclude_list = opts.filters.exclude
|
M.exclude_list = opts.filters.exclude
|
||||||
@ -83,6 +98,12 @@ function M.setup(opts)
|
|||||||
M.ignore_list[filter_name] = true
|
M.ignore_list[filter_name] = true
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user