Skip to content

feat(#2364): add option to show files first #2366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ applying configuration.
sort = {
sorter = "name",
folders_first = true,
files_first = false,
},
root_dirs = {},
prefer_startup_root = false,
Expand Down Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
67 changes: 35 additions & 32 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down