Skip to content

feat: Mixin Sorter (#1565) Self Solved #1566

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 13 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 47 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Subsequent calls to setup will replace the previous configuration.
open_on_tab = false,
ignore_buf_on_tab_change = {},
sort_by = "name",
after_sort = false,
root_dirs = {},
prefer_startup_root = false,
sync_root_with_cwd = false,
Expand Down Expand Up @@ -414,10 +415,54 @@ Opens the tree automatically when switching tabpage or opening a new tabpage
if the tree was previously open.
Type: `boolean`, Default: `false`


*nvim-tree.sort_by*
Changes how files within the same directory are sorted.
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension'.
Type: `string`, Default: `"name"`
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension',
'function'.
>
sort_by = function(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 a.name:lower() <= b.name:lower()
end


end
Type: `string | function(a, b)`, Default: `"name"`

*nvim-tree.after_sort*
Related to nvim-tree.sort_by, this function runs without mergesort.
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this? Is it because mixin sort cannot be achieved via a comparator?

Can be defined by your own after-sort works.
Type: `function(table)`, Default: `disable`

>
after_sort = function(t)
local i = 1

while i <= #t do
if t[i] and t[i].nodes then
local j = i + 1
while j <= #t do
if t[j] and not t[j].nodes and t[i].name:lower() == t[j].name:lower():match "(.+)%..+$" then
local change_target = t[j]
table.remove(t, j)
table.insert(t, i, change_target)
break
end
j = j + 1
end
end
i = i + 1
end
end

*nvim-tree.hijack_unnamed_buffer_when_opening*
Opens in place of the unnamed buffer if it's empty.
Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
open_on_tab = false,
ignore_buf_on_tab_change = {},
sort_by = "name",
after_sort = false,
root_dirs = {},
prefer_startup_root = false,
sync_root_with_cwd = false,
Expand Down Expand Up @@ -640,6 +641,8 @@ local FIELD_OVERRIDE_TYPECHECK = {
height = { string = true, ["function"] = true, number = true },
remove_keymaps = { boolean = true, table = true },
on_attach = { ["function"] = true, string = true },
sort_by = { ["function"] = true, string = true },
after_sort = { ["function"] = true, boolean = true },
}

local function validate_options(conf)
Expand Down
9 changes: 8 additions & 1 deletion lua/nvim-tree/explorer/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ function M.merge_sort(t, comparator)
end

split_merge(t, 1, #t, comparator)
if M.after_sort then
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to run the comparator and split_merge first?

We should call the user's after_sort instead of M.merge_sort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OMZ,,, I miss understood..
I thought 'sort_by' comparator must be run through merge sort function

Copy link
Member

Choose a reason for hiding this comment

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

OMZ,,, I miss understood.. I thought 'sort_by' comparator must be run through merge sort function

We would need to try it.

Please try explorer.lua and reload.lua calling after_sort directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i will try this too☺️

M.after_sort(t)
end
end

local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
Expand Down Expand Up @@ -150,7 +153,11 @@ end

function M.setup(opts)
M.sort_by = opts.sort_by
if M.sort_by == "modification_time" then
M.after_sort = opts.after_sort

if M.sort_by and type(M.sort_by) == "function" then
M.node_comparator = M.sort_by
elseif M.sort_by == "modification_time" then
M.node_comparator = M.node_comparator_modification_time
elseif M.sort_by == "case_sensitive" then
M.node_comparator = M.node_comparator_name_case_sensisive
Expand Down