Skip to content

Commit aefa66c

Browse files
authored
feat: extension sorter (#1181) (#1264)
1 parent f8312cd commit aefa66c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

doc/nvim-tree-lua.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ if the tree was previously open.
244244

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

250250
*nvim-tree.hijack_unnamed_buffer_when_opening*

lua/nvim-tree/explorer/sorters.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,38 @@ function M.node_comparator_modification_time(a, b)
124124
return last_modified_b <= last_modified_a
125125
end
126126

127+
function M.node_comparator_extension(a, b)
128+
if not (a and b) then
129+
return true
130+
end
131+
132+
if a.nodes and not b.nodes then
133+
return true
134+
elseif not a.nodes and b.nodes then
135+
return false
136+
end
137+
138+
if not (a.extension and b.extension) then
139+
return true
140+
end
141+
142+
if a.extension and not b.extension then
143+
return true
144+
elseif not a.extension and b.extension then
145+
return false
146+
end
147+
148+
return a.extension:lower() <= b.extension:lower()
149+
end
150+
127151
function M.setup(opts)
128152
M.sort_by = opts.sort_by
129153
if M.sort_by == "modification_time" then
130154
M.node_comparator = M.node_comparator_modification_time
131155
elseif M.sort_by == "case_sensitive" then
132156
M.node_comparator = M.node_comparator_name_case_sensisive
157+
elseif M.sort_by == "extension" then
158+
M.node_comparator = M.node_comparator_extension
133159
else
134160
M.node_comparator = M.node_comparator_name_ignorecase
135161
end

0 commit comments

Comments
 (0)