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 <alex@courtis.org>
This commit is contained in:
Azad 2023-08-26 06:15:56 +02:00 committed by GitHub
parent 807dc05156
commit d11d701857
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 32 deletions

View File

@ -308,6 +308,7 @@ applying configuration.
sort = { sort = {
sorter = "name", sorter = "name",
folders_first = true, folders_first = true,
files_first = false,
}, },
root_dirs = {}, root_dirs = {},
prefer_startup_root = false, prefer_startup_root = false,
@ -589,6 +590,11 @@ File and folder sorting options.
function. function.
Type: `boolean`, Default: `true` Type: `boolean`, Default: `true`
*nvim-tree.sort.files_first*
Sort files before folders. Has no effect when |nvim-tree.sort.sorter| is a
function. If set to `true` it overrides |nvim-tree.sort.folders_first|.
Type: `boolean`, Default: `false`
*nvim-tree.hijack_unnamed_buffer_when_opening* *nvim-tree.hijack_unnamed_buffer_when_opening*
Opens in place of the unnamed buffer if it's empty. Opens in place of the unnamed buffer if it's empty.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`

View File

@ -372,6 +372,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
sort = { sort = {
sorter = "name", sorter = "name",
folders_first = true, folders_first = true,
files_first = false,
}, },
root_dirs = {}, root_dirs = {},
prefer_startup_root = false, prefer_startup_root = false,

View File

@ -23,6 +23,24 @@ local function tbl_slice(t, first, last)
return slice return slice
end end
---Evaluate `sort.folders_first` and `sort.files_first`
---@param a table node
---@param b table node
---@return boolean|nil
local function folders_or_files_first(a, b)
if not (M.config.sort.folders_first or M.config.sort.files_first) then
return
end
if not a.nodes and b.nodes then
-- file <> folder
return M.config.sort.files_first
elseif a.nodes and not b.nodes then
-- folder <> file
return not M.config.sort.files_first
end
end
local function merge(t, first, mid, last, comparator) local function merge(t, first, mid, last, comparator)
local n1 = mid - first + 1 local n1 = mid - first + 1
local n2 = last - mid local n2 = last - mid
@ -124,12 +142,9 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
return true return true
end end
if M.config.sort.folders_first then local early_return = folders_or_files_first(a, b)
if a.nodes and not b.nodes then if early_return ~= nil then
return true return early_return
elseif not a.nodes and b.nodes then
return false
end
end end
if ignorecase then if ignorecase then
@ -152,12 +167,9 @@ function C.modification_time(a, b)
return true return true
end end
if M.config.sort.folders_first then local early_return = folders_or_files_first(a, b)
if a.nodes and not b.nodes then if early_return ~= nil then
return true return early_return
elseif not a.nodes and b.nodes then
return false
end
end end
local last_modified_a = 0 local last_modified_a = 0
@ -180,14 +192,11 @@ function C.suffix(a, b)
end end
-- directories go first -- directories go first
if M.config.sort.folders_first then local early_return = folders_or_files_first(a, b)
if a.nodes and not b.nodes then if early_return ~= nil then
return true return early_return
elseif not a.nodes and b.nodes then elseif a.nodes and b.nodes then
return false return C.name(a, b)
elseif a.nodes and b.nodes then
return C.name(a, b)
end
end end
-- dotfiles go second -- dotfiles go second
@ -231,12 +240,9 @@ function C.extension(a, b)
return true return true
end end
if M.config.sort.folders_first then local early_return = folders_or_files_first(a, b)
if a.nodes and not b.nodes then if early_return ~= nil then
return true return early_return
elseif not a.nodes and b.nodes then
return false
end
end end
if a.extension and not b.extension then if a.extension and not b.extension then
@ -259,12 +265,9 @@ function C.filetype(a, b)
local b_ft = vim.filetype.match { filename = b.name } local b_ft = vim.filetype.match { filename = b.name }
-- directories first -- directories first
if M.config.sort.folders_first then local early_return = folders_or_files_first(a, b)
if a.nodes and not b.nodes then if early_return ~= nil then
return true return early_return
elseif not a.nodes and b.nodes then
return false
end
end end
-- one is nil, the other wins -- one is nil, the other wins