* 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:
parent
807dc05156
commit
d11d701857
@ -308,6 +308,7 @@ applying configuration.
|
||||
sort = {
|
||||
sorter = "name",
|
||||
folders_first = true,
|
||||
files_first = false,
|
||||
},
|
||||
root_dirs = {},
|
||||
prefer_startup_root = false,
|
||||
@ -589,6 +590,11 @@ File and folder sorting options.
|
||||
function.
|
||||
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*
|
||||
Opens in place of the unnamed buffer if it's empty.
|
||||
Type: `boolean`, Default: `false`
|
||||
|
||||
@ -372,6 +372,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
sort = {
|
||||
sorter = "name",
|
||||
folders_first = true,
|
||||
files_first = false,
|
||||
},
|
||||
root_dirs = {},
|
||||
prefer_startup_root = false,
|
||||
|
||||
@ -23,6 +23,24 @@ local function tbl_slice(t, first, last)
|
||||
return slice
|
||||
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 n1 = mid - first + 1
|
||||
local n2 = last - mid
|
||||
@ -124,12 +142,9 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
|
||||
return true
|
||||
end
|
||||
|
||||
if M.config.sort.folders_first then
|
||||
if a.nodes and not b.nodes then
|
||||
return true
|
||||
elseif not a.nodes and b.nodes then
|
||||
return false
|
||||
end
|
||||
local early_return = folders_or_files_first(a, b)
|
||||
if early_return ~= nil then
|
||||
return early_return
|
||||
end
|
||||
|
||||
if ignorecase then
|
||||
@ -152,12 +167,9 @@ function C.modification_time(a, b)
|
||||
return true
|
||||
end
|
||||
|
||||
if M.config.sort.folders_first then
|
||||
if a.nodes and not b.nodes then
|
||||
return true
|
||||
elseif not a.nodes and b.nodes then
|
||||
return false
|
||||
end
|
||||
local early_return = folders_or_files_first(a, b)
|
||||
if early_return ~= nil then
|
||||
return early_return
|
||||
end
|
||||
|
||||
local last_modified_a = 0
|
||||
@ -180,14 +192,11 @@ function C.suffix(a, b)
|
||||
end
|
||||
|
||||
-- directories go first
|
||||
if M.config.sort.folders_first then
|
||||
if a.nodes and not b.nodes then
|
||||
return true
|
||||
elseif not a.nodes and b.nodes then
|
||||
return false
|
||||
elseif a.nodes and b.nodes then
|
||||
return C.name(a, b)
|
||||
end
|
||||
local early_return = folders_or_files_first(a, b)
|
||||
if early_return ~= nil then
|
||||
return early_return
|
||||
elseif a.nodes and b.nodes then
|
||||
return C.name(a, b)
|
||||
end
|
||||
|
||||
-- dotfiles go second
|
||||
@ -231,12 +240,9 @@ function C.extension(a, b)
|
||||
return true
|
||||
end
|
||||
|
||||
if M.config.sort.folders_first then
|
||||
if a.nodes and not b.nodes then
|
||||
return true
|
||||
elseif not a.nodes and b.nodes then
|
||||
return false
|
||||
end
|
||||
local early_return = folders_or_files_first(a, b)
|
||||
if early_return ~= nil then
|
||||
return early_return
|
||||
end
|
||||
|
||||
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 }
|
||||
|
||||
-- directories first
|
||||
if M.config.sort.folders_first then
|
||||
if a.nodes and not b.nodes then
|
||||
return true
|
||||
elseif not a.nodes and b.nodes then
|
||||
return false
|
||||
end
|
||||
local early_return = folders_or_files_first(a, b)
|
||||
if early_return ~= nil then
|
||||
return early_return
|
||||
end
|
||||
|
||||
-- one is nil, the other wins
|
||||
|
||||
Loading…
Reference in New Issue
Block a user