Skip to content

Commit 4864e1f

Browse files
authored
feat: add a custom function for sorting files and directories (#377)
1 parent 0417ebb commit 4864e1f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ use {
123123
popup_border_style = "rounded",
124124
enable_git_status = true,
125125
enable_diagnostics = true,
126+
sort_case_insensitive = false, -- used when sorting files and directories in the tree
127+
sort_function = nil , -- use a custom function for sorting files and directories in the tree
128+
-- sort_function = function (a,b)
129+
-- if a.type == b.type then
130+
-- return a.path > b.path
131+
-- else
132+
-- return a.type > b.type
133+
-- end
134+
-- end , -- this sorts files and directories descendantly
126135
default_component_configs = {
127136
container = {
128137
enable_character_fade = true

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local config = {
2525
-- set to -1 to disable the resize timer entirely
2626
-- -- NOTE: this will speed up to 50 ms for 1 second following a resize
2727
sort_case_insensitive = false, -- used when sorting files and directories in the tree
28+
sort_function = nil , -- uses a custom function for sorting files and directories in the tree
2829
use_popups_for_input = true, -- If false, inputs will use vim.ui.input() instead of custom floats.
2930
use_default_mappings = true,
3031
--

lua/neo-tree/sources/common/file-items.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ local function sort_items_case_insensitive(a, b)
2222
end
2323
end
2424

25+
local function sort_function_is_valid(func)
26+
if func == nil then
27+
return false
28+
end
29+
30+
local a = { type = "dir", path = "foo" }
31+
local b = { type = "dir", path = "baz" }
32+
33+
local success, result = pcall(func, a, b)
34+
if success and type(result) == "boolean" then
35+
return true
36+
end
37+
38+
log.error("sort function isn't valid ", result)
39+
return false
40+
end
41+
2542
local function deep_sort(tbl, sort_func)
2643
if sort_func == nil then
2744
local config = require("neo-tree").config
28-
if config.sort_case_insensitive then
45+
if sort_function_is_valid(config.sort_function) then
46+
sort_func = config.sort_function
47+
elseif config.sort_case_insensitive then
2948
sort_func = sort_items_case_insensitive
3049
else
3150
sort_func = sort_items

0 commit comments

Comments
 (0)