From 76cab2265eeec9b5b45c33db602a74a4212eebcd Mon Sep 17 00:00:00 2001 From: Cyber Oliveira Date: Sat, 1 Jul 2023 14:11:48 -0300 Subject: [PATCH 1/3] feat: adds new type of sorting based on vim's filetype detection --- doc/nvim-tree-lua.txt | 5 +++-- lua/nvim-tree/explorer/sorters.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 882c90b0302..2c93ae0fb00 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`, +`filetype` 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` + - `filetype`: `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..8e0e838bcd2 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -191,6 +191,32 @@ function C.extension(a, b) return a.extension:lower() <= b.extension:lower() end +function C.filetype(a, b) + local a_ft = vim.filetype.match { filename = a.name } + local b_ft = vim.filetype.match { filename = b.name } + + -- directories first + if a.nodes and not b.nodes then + return true + elseif not a.nodes and b.nodes then + return false + end + + -- both nil, whatever + if not (a_ft and b_ft) then + return true + end + + -- one is nil, the other wins + if a_ft and not b_ft then + return true + elseif not a_ft and b_ft then + return false + end + + return a_ft <= b_ft +end + function M.setup(opts) M.config = {} M.config.sort_by = opts.sort_by From b345a86118b1348c7f5d8b75c7892cc31ad794d1 Mon Sep 17 00:00:00 2001 From: Cyber Oliveira Date: Sun, 2 Jul 2023 18:36:38 -0300 Subject: [PATCH 2/3] fix(ft/sorter): fallbacks to C.name when both ft's are nil or equal --- lua/nvim-tree/explorer/sorters.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/explorer/sorters.lua b/lua/nvim-tree/explorer/sorters.lua index 8e0e838bcd2..ffedf6feb7c 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -202,11 +202,6 @@ function C.filetype(a, b) return false end - -- both nil, whatever - if not (a_ft and b_ft) then - return true - end - -- one is nil, the other wins if a_ft and not b_ft then return true @@ -214,7 +209,12 @@ function C.filetype(a, b) return false end - return a_ft <= b_ft + -- same filetype or both nil, sort by name + if a_ft == b_ft then + return C.name(a, b) + end + + return a_ft < b_ft end function M.setup(opts) From c5b827e35ac23d8f410aa48245f442a5a1912eb1 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 9 Jul 2023 12:29:37 +1000 Subject: [PATCH 3/3] feat: adds new type of sorting based on vim's filetype detection --- lua/nvim-tree/explorer/sorters.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/nvim-tree/explorer/sorters.lua b/lua/nvim-tree/explorer/sorters.lua index ffedf6feb7c..4414e33536b 100644 --- a/lua/nvim-tree/explorer/sorters.lua +++ b/lua/nvim-tree/explorer/sorters.lua @@ -80,6 +80,7 @@ function M.sort(t) absolute_path = n.absolute_path, executable = n.executable, extension = n.extension, + filetype = vim.filetype.match { filename = n.name }, link_to = n.link_to, name = n.name, type = n.type,