@@ -3,7 +3,7 @@ local lib = require "nvim-tree.lib"
3
3
4
4
local M = {}
5
5
6
- local keypress_funcs = {
6
+ local Actions = {
7
7
close = view .close ,
8
8
close_node = require (" nvim-tree.actions.movements" ).parent_node (true ),
9
9
collapse_all = require (" nvim-tree.actions.collapse-all" ).fn ,
@@ -18,8 +18,6 @@ local keypress_funcs = {
18
18
first_sibling = require (" nvim-tree.actions.movements" ).sibling (- math.huge ),
19
19
full_rename = require (" nvim-tree.actions.rename-file" ).fn (true ),
20
20
last_sibling = require (" nvim-tree.actions.movements" ).sibling (math.huge ),
21
- live_filter = require (" nvim-tree.live-filter" ).start_filtering ,
22
- clear_live_filter = require (" nvim-tree.live-filter" ).clear_filter ,
23
21
next_diag_item = require (" nvim-tree.actions.movements" ).find_item (" next" , " diag" ),
24
22
next_git_item = require (" nvim-tree.actions.movements" ).find_item (" next" , " git" ),
25
23
next_sibling = require (" nvim-tree.actions.movements" ).sibling (1 ),
@@ -36,60 +34,80 @@ local keypress_funcs = {
36
34
toggle_file_info = require (" nvim-tree.actions.file-popup" ).toggle_file_info ,
37
35
system_open = require (" nvim-tree.actions.system-open" ).fn ,
38
36
toggle_dotfiles = require (" nvim-tree.actions.toggles" ).dotfiles ,
39
- toggle_help = require (" nvim-tree.actions.toggles" ).help ,
40
37
toggle_custom = require (" nvim-tree.actions.toggles" ).custom ,
41
38
toggle_git_ignored = require (" nvim-tree.actions.toggles" ).git_ignored ,
42
39
trash = require (" nvim-tree.actions.trash" ).fn ,
43
40
}
44
41
45
- function M . dispatch (action )
46
- if view . is_help_ui () and action == " close " then
47
- action = " toggle_help "
42
+ local function handle_action_on_help_ui (action )
43
+ if action == " close " or action == " toggle_help " then
44
+ require ( " nvim-tree.actions.toggles " ). help ()
48
45
end
49
- if view .is_help_ui () and action ~= " toggle_help" then
50
- return
46
+ end
47
+
48
+ local function handle_filter_actions (action )
49
+ if action == " live_filter" then
50
+ require (" nvim-tree.live-filter" ).start_filtering ()
51
+ elseif action == " clear_live_filter" then
52
+ require (" nvim-tree.live-filter" ).clear_filter ()
51
53
end
54
+ end
52
55
53
- if action == " live_filter" or action == " clear_live_filter" then
54
- return keypress_funcs [action ]()
56
+ local function change_dir_action (node )
57
+ if node .name == " .." then
58
+ require (" nvim-tree.actions.change-dir" ).fn " .."
59
+ elseif node .nodes ~= nil then
60
+ require (" nvim-tree.actions.change-dir" ).fn (lib .get_last_group_node (node ).absolute_path )
55
61
end
62
+ end
63
+
64
+ local function open_file (action , node )
65
+ local path = node .absolute_path
66
+ if node .link_to and not node .nodes then
67
+ path = node .link_to
68
+ end
69
+ require (" nvim-tree.actions.open-file" ).fn (action , path )
70
+ end
56
71
72
+ local function handle_tree_actions (action )
57
73
local node = lib .get_node_at_cursor ()
58
74
if not node then
59
75
return
60
76
end
61
77
62
78
local custom_function = M .custom_keypress_funcs [action ]
63
- local default_function = keypress_funcs [action ]
79
+ local defined_action = Actions [action ]
64
80
65
81
if type (custom_function ) == " function" then
66
82
return custom_function (node )
67
- elseif default_function then
68
- return default_function (node )
83
+ elseif defined_action then
84
+ return defined_action (node )
69
85
end
70
86
71
- if action == " preview" then
72
- if node .name == " .." then
73
- return
74
- end
75
- if not node .nodes then
76
- return require (" nvim-tree.actions.open-file" ).fn (" preview" , node .absolute_path )
77
- end
78
- elseif node .name == " .." then
79
- return require (" nvim-tree.actions.change-dir" ).fn " .."
80
- elseif action == " cd" then
81
- if node .nodes ~= nil then
82
- require (" nvim-tree.actions.change-dir" ).fn (lib .get_last_group_node (node ).absolute_path )
83
- end
87
+ local is_parent = node .name == " .."
88
+
89
+ if action == " preview" and is_parent then
84
90
return
85
91
end
86
92
87
- if node .link_to and not node .nodes then
88
- require (" nvim-tree.actions.open-file" ).fn (action , node .link_to )
89
- elseif node .nodes ~= nil then
93
+ if action == " cd" or is_parent then
94
+ return change_dir_action (node )
95
+ end
96
+
97
+ if node .nodes then
90
98
lib .expand_or_collapse (node )
91
99
else
92
- require (" nvim-tree.actions.open-file" ).fn (action , node .absolute_path )
100
+ open_file (action , node )
101
+ end
102
+ end
103
+
104
+ function M .dispatch (action )
105
+ if view .is_help_ui () then
106
+ handle_action_on_help_ui (action )
107
+ elseif action :match " live" ~= nil then
108
+ handle_filter_actions (action )
109
+ else
110
+ handle_tree_actions (action )
93
111
end
94
112
end
95
113
0 commit comments