Skip to content

Commit a708bd2

Browse files
feat: add sort_by "suffix" (#2307)
* feat: adds new type of sorting based on the filename's suffix * chore(syntax): using string colon methods * fix(regex): use alphanumeric match for extensions * feat: adds new type of sorting based on the filename's suffix --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent 04b2c1e commit a708bd2

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,10 @@ Reloads the explorer every time a buffer is written to.
568568

569569
*nvim-tree.sort_by*
570570
Changes how files within the same directory are sorted.
571-
Can be one of `name`, `case_sensitive`, `modification_time`, `extension`,
572-
`filetype` or a function.
571+
Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`,
572+
`"suffix"`, `"filetype"` or a function.
573+
`"extension"` uses all suffixes e.g. `foo.tar.gz` -> `.tar.gz`
574+
`"suffix"` uses the last e.g. `.gz`
573575
Type: `string` | `function(nodes)`, Default: `"name"`
574576

575577
Function may perform a sort or return a string with one of the above

lua/nvim-tree/explorer/sorters.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,56 @@ function C.modification_time(a, b)
168168
return last_modified_b <= last_modified_a
169169
end
170170

171+
function C.suffix(a, b)
172+
if not (a and b) then
173+
return true
174+
end
175+
176+
-- directories go first
177+
if a.nodes and not b.nodes then
178+
return true
179+
elseif not a.nodes and b.nodes then
180+
return false
181+
elseif a.nodes and b.nodes then
182+
return C.name(a, b)
183+
end
184+
185+
-- dotfiles go second
186+
if a.name:sub(1, 1) == "." and b.name:sub(1, 1) ~= "." then
187+
return true
188+
elseif a.name:sub(1, 1) ~= "." and b.name:sub(1, 1) == "." then
189+
return false
190+
elseif a.name:sub(1, 1) == "." and b.name:sub(1, 1) == "." then
191+
return C.name(a, b)
192+
end
193+
194+
-- unsuffixed go third
195+
local a_suffix_ndx = a.name:find "%.%w+$"
196+
local b_suffix_ndx = b.name:find "%.%w+$"
197+
198+
if not a_suffix_ndx and b_suffix_ndx then
199+
return true
200+
elseif a_suffix_ndx and not b_suffix_ndx then
201+
return false
202+
elseif not (a_suffix_ndx and b_suffix_ndx) then
203+
return C.name(a, b)
204+
end
205+
206+
-- finally, compare by suffixes
207+
local a_suffix = a.name:sub(a_suffix_ndx)
208+
local b_suffix = b.name:sub(b_suffix_ndx)
209+
210+
if a_suffix and not b_suffix then
211+
return true
212+
elseif not a_suffix and b_suffix then
213+
return false
214+
elseif a_suffix:lower() == b_suffix:lower() then
215+
return C.name(a, b)
216+
end
217+
218+
return a_suffix:lower() < b_suffix:lower()
219+
end
220+
171221
function C.extension(a, b)
172222
if not (a and b) then
173223
return true

0 commit comments

Comments
 (0)