Skip to content

feat(#2313): sort_by -> sort.sorter, add sort.folders_first default true #2314

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 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -313,6 +313,7 @@ applying configuration.
hijack_netrw = true,
hijack_unnamed_buffer_when_opening = false,
sort_by = "name",
sort_folders_first = true,
root_dirs = {},
prefer_startup_root = false,
sync_root_with_cwd = false,
Expand Down Expand Up @@ -591,6 +592,11 @@ Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`
end)
end
<
*nvim-tree.sort_folders_first*
Sort folders before files. Has no effect when |nvim-tree.sort_by| is a
function.
Type: `boolean`, Default: `true`

*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 @@ -367,6 +367,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
hijack_netrw = true,
hijack_unnamed_buffer_when_opening = false,
sort_by = "name",
sort_folders_first = true,
root_dirs = {},
prefer_startup_root = false,
sync_root_with_cwd = false,
Expand Down
57 changes: 35 additions & 22 deletions lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
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

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
end

if ignorecase then
Expand All @@ -148,10 +151,13 @@ function C.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

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
end

local last_modified_a = 0
Expand All @@ -174,12 +180,14 @@ function C.suffix(a, b)
end

-- directories go first
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)
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moniquelive should we sort folders by suffix? Does that make any sense?

Copy link
Collaborator

@moniquelive moniquelive Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not very usual, but sometimes you may have folders with suffixes to disambiguate their names such as .tmp, .old, .bkp, etc. But I believe people will want to turn this directory-grouping-by-suffix on or off. Should this be another setting? This is getting fractal pretty quickly... 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have too many options... I'm happy for this to be opinionated.

Other sort methods apply the same rules to folders and files, this should too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it, it looked odd, abandoned.

end
end

-- dotfiles go second
Expand Down Expand Up @@ -223,10 +231,12 @@ function C.extension(a, b)
return true
end

if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
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
end

if a.extension and not b.extension then
Expand All @@ -249,10 +259,12 @@ function C.filetype(a, b)
local b_ft = vim.filetype.match { filename = b.name }

-- directories first
if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
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
end

-- one is nil, the other wins
Expand All @@ -273,6 +285,7 @@ end
function M.setup(opts)
M.config = {}
M.config.sort_by = opts.sort_by
M.config.sort_folders_first = opts.sort_folders_first

if type(opts.sort_by) == "function" then
C.user = opts.sort_by
Expand Down