From aa65ee408acbdcf17a3082088297cecf8826ac32 Mon Sep 17 00:00:00 2001 From: Cyber Oliveira Date: Sun, 2 Jul 2023 19:32:53 -0300 Subject: [PATCH 1/4] feat: adds new type of sorting based on the filename's suffix --- doc/nvim-tree-lua.txt | 5 +-- lua/nvim-tree/explorer/sorters.lua | 53 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 882c90b0302..6a279759e29 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -568,8 +568,8 @@ Reloads the explorer every time a buffer is written to. *nvim-tree.sort_by* Changes how files within the same directory are sorted. -Can be one of `name`, `case_sensitive`, `modification_time`, `extension` or a -function. +Can be one of `name`, `case_sensitive`, `modification_time`, `extension`, +`suffix` or a function. Type: `string` | `function(nodes)`, Default: `"name"` Function may perform a sort or return a string with one of the above @@ -577,6 +577,7 @@ function. - `absolute_path`: `string` - `executable`: `boolean` - `extension`: `string` + - `suffix`: `string` - `link_to`: `string` - `name`: `string` - `type`: `"directory"` | `"file"` | `"link"` diff --git a/lua/nvim-tree/explorer/sorters.lua b/lua/nvim-tree/explorer/sorters.lua index 9ceb64c266b..3944e8c94f5 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -167,6 +167,59 @@ function C.modification_time(a, b) return last_modified_b <= last_modified_a end +function C.suffix(a, b) + if not (a and b) then + return true + 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) + end + + -- dotfiles go second + local a_first = string.sub(a.name, 1, 1) + local b_first = string.sub(b.name, 1, 1) + + if a_first == "." and b_first ~= "." then + return true + elseif a_first ~= "." and b_first == "." then + return false + elseif a_first == "." and b_first == "." then + return C.name(a, b) + end + + -- check if we have suffixes + local a_suffix_ndx = string.find(a.name, "%.%a+$") + local b_suffix_ndx = string.find(b.name, "%.%a+$") + + if a_suffix_ndx and not b_suffix_ndx then + return true + elseif not a_suffix_ndx and b_suffix_ndx then + return false + elseif not (a_suffix_ndx and b_suffix_ndx) then + return C.name(a, b) + end + + -- finally, compare by suffixes + local a_suffix = string.sub(a.name, a_suffix_ndx) + local b_suffix = string.sub(b.name, b_suffix_ndx) + + if a_suffix and not b_suffix then + return true + elseif not a_suffix and b_suffix then + return false + elseif a_suffix:lower() == b_suffix:lower() then + return C.name(a, b) + end + + return a_suffix:lower() < b_suffix:lower() +end + function C.extension(a, b) if not (a and b) then return true From 2494ad3acfa7a1eeab98a280d671e2cafa92cce5 Mon Sep 17 00:00:00 2001 From: Cyber Oliveira Date: Mon, 3 Jul 2023 18:44:52 -0300 Subject: [PATCH 2/4] chore(syntax): using string colon methods --- lua/nvim-tree/explorer/sorters.lua | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lua/nvim-tree/explorer/sorters.lua b/lua/nvim-tree/explorer/sorters.lua index 3944e8c94f5..65a00a493a0 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -182,32 +182,29 @@ function C.suffix(a, b) end -- dotfiles go second - local a_first = string.sub(a.name, 1, 1) - local b_first = string.sub(b.name, 1, 1) - - if a_first == "." and b_first ~= "." then + if a.name:sub(1, 1) == "." and b.name:sub(1, 1) ~= "." then return true - elseif a_first ~= "." and b_first == "." then + elseif a.name:sub(1, 1) ~= "." and b.name:sub(1, 1) == "." then return false - elseif a_first == "." and b_first == "." then + elseif a.name:sub(1, 1) == "." and b.name:sub(1, 1) == "." then return C.name(a, b) end - -- check if we have suffixes - local a_suffix_ndx = string.find(a.name, "%.%a+$") - local b_suffix_ndx = string.find(b.name, "%.%a+$") + -- unsuffixed go third + local a_suffix_ndx = a.name:find("%.%a+$") + local b_suffix_ndx = b.name:find("%.%a+$") - if a_suffix_ndx and not b_suffix_ndx then + if not a_suffix_ndx and b_suffix_ndx then return true - elseif not a_suffix_ndx and b_suffix_ndx then + elseif a_suffix_ndx and not b_suffix_ndx then return false elseif not (a_suffix_ndx and b_suffix_ndx) then return C.name(a, b) end -- finally, compare by suffixes - local a_suffix = string.sub(a.name, a_suffix_ndx) - local b_suffix = string.sub(b.name, b_suffix_ndx) + local a_suffix = a.name:sub(a_suffix_ndx) + local b_suffix = b.name:sub(b_suffix_ndx) if a_suffix and not b_suffix then return true From 249d1ca627fe325ea790ef2b2a593964847f4eda Mon Sep 17 00:00:00 2001 From: Cyber Oliveira Date: Tue, 4 Jul 2023 11:43:30 -0300 Subject: [PATCH 3/4] fix(regex): use alphanumeric match for extensions --- lua/nvim-tree/explorer/sorters.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/explorer/sorters.lua b/lua/nvim-tree/explorer/sorters.lua index 65a00a493a0..f8d0ec32caa 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -191,8 +191,8 @@ function C.suffix(a, b) end -- unsuffixed go third - local a_suffix_ndx = a.name:find("%.%a+$") - local b_suffix_ndx = b.name:find("%.%a+$") + local a_suffix_ndx = a.name:find "%.%w+$" + local b_suffix_ndx = b.name:find "%.%w+$" if not a_suffix_ndx and b_suffix_ndx then return true From f1fa4342a2be8b36c286252dc88046c770be7623 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 9 Jul 2023 13:47:06 +1000 Subject: [PATCH 4/4] feat: adds new type of sorting based on the filename's suffix --- doc/nvim-tree-lua.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index fe2aadcd46f..4952a5b8e3e 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -568,8 +568,10 @@ Reloads the explorer every time a buffer is written to. *nvim-tree.sort_by* Changes how files within the same directory are sorted. -Can be one of `name`, `case_sensitive`, `modification_time`, `extension`, -`filetype`, `suffix` or a function. +Can be one of `"name"`, `"case_sensitive"`, `"modification_time"`, `"extension"`, +`"suffix"`, `"filetype"` or a function. +`"extension"` uses all suffixes e.g. `foo.tar.gz` -> `.tar.gz` +`"suffix"` uses the last e.g. `.gz` Type: `string` | `function(nodes)`, Default: `"name"` Function may perform a sort or return a string with one of the above @@ -577,7 +579,6 @@ Can be one of `name`, `case_sensitive`, `modification_time`, `extension`, - `absolute_path`: `string` - `executable`: `boolean` - `extension`: `string` - - `suffix`: `string` - `filetype`: `string` - `link_to`: `string` - `name`: `string`