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 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
3 changes: 2 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ if the tree was previously open.

*nvim-tree.sort_by*
Changes how files within the same directory are sorted.
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension'.
Can be one of 'name', 'case_sensitive', 'modification_time' or 'extension' or
'mixin'.
Type: `string`, Default: `"name"`

*nvim-tree.hijack_unnamed_buffer_when_opening*
Expand Down
23 changes: 23 additions & 0 deletions 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.sort_by == "mixin" then
M.node_inserter_mixin(t)
end
end

local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
Expand Down Expand Up @@ -148,6 +151,26 @@ function M.node_comparator_extension(a, b)
return a.extension:lower() <= b.extension:lower()
end

function M.node_inserter_mixin(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

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