33---
44--- Call this after nvim-tree setup
55---
6- --- This is expensive as there are many cascading requires and is avoided
7- --- until after setup has been called, so that the user may require API cheaply.
6+ --- All requires must be done lazily so that requiring api post setup is cheap.
87
98local legacy = require (" nvim-tree.legacy" )
109
11- local actions = require (" nvim-tree.actions" )
12- local config = require (" nvim-tree.config" )
13- local help = require (" nvim-tree.help" )
14- local keymap = require (" nvim-tree.keymap" )
15- local utils = require (" nvim-tree.utils" )
16- local view = require (" nvim-tree.view" )
17-
1810local M = {}
1911
12+ --- convenience wrappers for lazy module requires
13+ local function actions () return require (" nvim-tree.actions" ) end
14+ local function core () return require (" nvim-tree.core" ) end
15+ local function config () return require (" nvim-tree.config" ) end
16+ local function help () return require (" nvim-tree.help" ) end
17+ local function keymap () return require (" nvim-tree.keymap" ) end
18+ local function utils () return require (" nvim-tree.utils" ) end
19+ local function view () return require (" nvim-tree.view" ) end
20+
21+ -- TODO 3255 wrap* must be able to take a function. May be best to have that function accept (node, ...)
22+
2023--- Invoke a method on the singleton explorer.
2124--- Print error when setup not called.
2225--- @param explorer_method string explorer method name
2326--- @return fun ( ... ): any
2427local function wrap_explorer (explorer_method )
2528 return function (...)
26- local explorer = require ( " nvim-tree. core" ).get_explorer ()
29+ local explorer = core ( ).get_explorer ()
2730 if explorer then
2831 return explorer [explorer_method ](explorer , ... )
2932 end
6164local function wrap_explorer_member_args (explorer_member , member_method , ...)
6265 local method_args = ...
6366 return function (...)
64- local explorer = require ( " nvim-tree. core" ).get_explorer ()
67+ local explorer = core ( ).get_explorer ()
6568 if explorer then
6669 return explorer [explorer_member ][member_method ](explorer [explorer_member ], method_args , ... )
6770 end
7578--- @return fun ( ... ): any
7679local function wrap_explorer_member (explorer_member , member_method )
7780 return function (...)
78- local explorer = require ( " nvim-tree. core" ).get_explorer ()
81+ local explorer = core ( ).get_explorer ()
7982 if explorer then
8083 return explorer [explorer_member ][member_method ](explorer [explorer_member ], ... )
8184 end
@@ -101,132 +104,137 @@ local function wrap_node_or_visual(fn)
101104 end
102105end
103106
104- --- Re-Hydrate api functions and classes post-setup
105- --- @param api table not properly typed to prevent LSP from referencing implementations
106- function M .hydrate (api )
107- api .tree .open = actions .tree .open .fn
108- api .tree .focus = api .tree .open
109-
110- api .tree .toggle = actions .tree .toggle .fn
111- api .tree .close = view .close
112- api .tree .close_in_this_tab = view .close_this_tab_only
113- api .tree .close_in_all_tabs = view .close_all_tabs
114- api .tree .reload = wrap_explorer (" reload_explorer" )
115-
116- api .tree .resize = actions .tree .resize .fn
107+ local function hydrate_config (api )
108+ api .config .global = function () return config ().g_clone () end
109+ api .config .user = function () return config ().u_clone () end
110+ end
117111
118- api .tree .change_root = actions .tree .change_dir .fn
112+ local function hydrate_filter (api )
113+ api .filter .custom .toggle = wrap_explorer_member_args (" filters" , " toggle" , " custom" )
114+ api .filter .dotfiles .toggle = wrap_explorer_member_args (" filters" , " toggle" , " dotfiles" )
115+ api .filter .git .clean .toggle = wrap_explorer_member_args (" filters" , " toggle" , " git_clean" )
116+ api .filter .git .ignored .toggle = wrap_explorer_member_args (" filters" , " toggle" , " git_ignored" )
117+ api .filter .live .clear = wrap_explorer_member (" live_filter" , " clear_filter" )
118+ api .filter .live .start = wrap_explorer_member (" live_filter" , " start_filtering" )
119+ api .filter .no_bookmark .toggle = wrap_explorer_member_args (" filters" , " toggle" , " no_bookmark" )
120+ api .filter .no_buffer .toggle = wrap_explorer_member_args (" filters" , " toggle" , " no_buffer" )
121+ api .filter .toggle = wrap_explorer_member (" filters" , " toggle" )
122+ end
119123
120- api .tree .change_root_to_node = wrap_node (wrap_explorer (" change_dir_to_node" ))
121- api .tree .change_root_to_parent = wrap_node (wrap_explorer (" dir_up" ))
122- api .tree .get_node_under_cursor = wrap_explorer (" get_node_at_cursor" )
123- api .tree .get_nodes = wrap_explorer (" get_nodes" )
124-
125- api .tree .find_file = actions .tree .find_file .fn
126- api .tree .search_node = actions .finders .search_node .fn
127-
128- api .tree .collapse_all = actions .tree .collapse .all
129-
130- api .tree .expand_all = wrap_node (wrap_explorer (" expand_all" ))
131- api .tree .toggle_help = help .toggle
132- api .tree .is_tree_buf = utils .is_nvim_tree_buf
133-
134- api .tree .is_visible = view .is_visible
135-
136- api .tree .winid = view .winid
137-
138- api .fs .create = wrap_node_or_nil (actions .fs .create_file .fn )
139- api .fs .remove = wrap_node_or_visual (actions .fs .remove_file .fn )
140- api .fs .trash = wrap_node_or_visual (actions .fs .trash .fn )
141- api .fs .rename_node = wrap_node (actions .fs .rename_file .rename_node )
142- api .fs .rename = wrap_node (actions .fs .rename_file .rename_node )
143- api .fs .rename_sub = wrap_node (actions .fs .rename_file .rename_sub )
144- api .fs .rename_basename = wrap_node (actions .fs .rename_file .rename_basename )
145- api .fs .rename_full = wrap_node (actions .fs .rename_file .rename_full )
146- api .fs .cut = wrap_node_or_visual (wrap_explorer_member (" clipboard" , " cut" ))
147- api .fs .paste = wrap_node (wrap_explorer_member (" clipboard" , " paste" ))
148- api .fs .clear_clipboard = wrap_explorer_member (" clipboard" , " clear_clipboard" )
149- api .fs .print_clipboard = wrap_explorer_member (" clipboard" , " print_clipboard" )
150- api .fs .copy .node = wrap_node_or_visual (wrap_explorer_member (" clipboard" , " copy" ))
124+ local function hydrate_fs (api )
125+ api .fs .clear_clipboard = wrap_explorer_member (" clipboard" , " clear_clipboard" )
151126 api .fs .copy .absolute_path = wrap_node (wrap_explorer_member (" clipboard" , " copy_absolute_path" ))
152- api .fs .copy .filename = wrap_node (wrap_explorer_member (" clipboard" , " copy_filename" ))
153- api .fs .copy .basename = wrap_node (wrap_explorer_member (" clipboard" , " copy_basename" ))
127+ api .fs .copy .basename = wrap_node (wrap_explorer_member (" clipboard" , " copy_basename" ))
128+ api .fs .copy .filename = wrap_node (wrap_explorer_member (" clipboard" , " copy_filename" ))
129+ api .fs .copy .node = wrap_node_or_visual (wrap_explorer_member (" clipboard" , " copy" ))
154130 api .fs .copy .relative_path = wrap_node (wrap_explorer_member (" clipboard" , " copy_path" ))
131+ api .fs .create = wrap_node_or_nil (actions ().fs .create_file .fn )
132+ api .fs .cut = wrap_node_or_visual (wrap_explorer_member (" clipboard" , " cut" ))
133+ api .fs .paste = wrap_node (wrap_explorer_member (" clipboard" , " paste" ))
134+ api .fs .print_clipboard = wrap_explorer_member (" clipboard" , " print_clipboard" )
135+ api .fs .remove = wrap_node_or_visual (actions ().fs .remove_file .fn )
136+ api .fs .rename = wrap_node (actions ().fs .rename_file .rename_node )
137+ api .fs .rename_basename = wrap_node (actions ().fs .rename_file .rename_basename )
138+ api .fs .rename_full = wrap_node (actions ().fs .rename_file .rename_full )
139+ api .fs .rename_node = wrap_node (actions ().fs .rename_file .rename_node )
140+ api .fs .rename_sub = wrap_node (actions ().fs .rename_file .rename_sub )
141+ api .fs .trash = wrap_node_or_visual (actions ().fs .trash .fn )
142+ end
155143
156- api .node .open .edit = wrap_node (actions .node .open_file .edit )
157- api .node .open .drop = wrap_node (actions .node .open_file .drop )
158- api .node .open .tab_drop = wrap_node (actions .node .open_file .tab_drop )
159- api .node .open .replace_tree_buffer = wrap_node (actions .node .open_file .replace_tree_buffer )
160- api .node .open .no_window_picker = wrap_node (actions .node .open_file .no_window_picker )
161- api .node .open .vertical = wrap_node (actions .node .open_file .vertical )
162- api .node .open .vertical_no_picker = wrap_node (actions .node .open_file .vertical_no_picker )
163- api .node .open .horizontal = wrap_node (actions .node .open_file .horizontal )
164- api .node .open .horizontal_no_picker = wrap_node (actions .node .open_file .horizontal_no_picker )
165- api .node .open .tab = wrap_node (actions .node .open_file .tab )
166- api .node .open .toggle_group_empty = wrap_node (actions .node .open_file .toggle_group_empty )
167- api .node .open .preview = wrap_node (actions .node .open_file .preview )
168- api .node .open .preview_no_picker = wrap_node (actions .node .open_file .preview_no_picker )
169-
170- api .node .show_info_popup = wrap_node (actions .node .file_popup .toggle_file_info )
171- api .node .run .cmd = wrap_node (actions .node .run_command .run_file_command )
172- api .node .run .system = wrap_node (actions .node .system_open .fn )
173-
174- api .node .navigate .sibling .next = wrap_node (actions .moves .sibling .next )
175- api .node .navigate .sibling .prev = wrap_node (actions .moves .sibling .prev )
176- api .node .navigate .sibling .first = wrap_node (actions .moves .sibling .first )
177- api .node .navigate .sibling .last = wrap_node (actions .moves .sibling .last )
178-
179- api .node .navigate .parent = wrap_node (actions .moves .parent .move )
180- api .node .navigate .parent_close = wrap_node (actions .moves .parent .move_close )
181-
182- api .node .navigate .git .next = actions .moves .item .git_next
183- api .node .navigate .git .next_skip_gitignored = actions .moves .item .git_next_skip_gitignored
184- api .node .navigate .git .next_recursive = actions .moves .item .git_next_recursive
185- api .node .navigate .git .prev = actions .moves .item .git_prev
186- api .node .navigate .git .prev_skip_gitignored = actions .moves .item .git_prev_skip_gitignored
187- api .node .navigate .git .prev_recursive = actions .moves .item .git_prev_recursive
188-
189- api .node .navigate .diagnostics .next = actions .moves .item .diagnostics_next
190- api .node .navigate .diagnostics .next_recursive = actions .moves .item .diagnostics_next_recursive
191- api .node .navigate .diagnostics .prev = actions .moves .item .diagnostics_prev
192- api .node .navigate .diagnostics .prev_recursive = actions .moves .item .diagnostics_prev_recursive
193-
194- api .node .navigate .opened .next = actions .moves .item .opened_next
195- api .node .navigate .opened .prev = actions .moves .item .opened_prev
196-
197- api .node .expand = wrap_node (wrap_explorer (" expand_node" ))
198- api .node .collapse = wrap_node (actions .tree .collapse .node )
199-
200- api .node .buffer .delete = wrap_node (function (node , opts ) actions .node .buffer .delete (node , opts ) end )
201- api .node .buffer .wipe = wrap_node (function (node , opts ) actions .node .buffer .wipe (node , opts ) end )
202-
203- api .tree .reload_git = wrap_explorer (" reload_git" )
204-
205- api .filter .live .start = wrap_explorer_member (" live_filter" , " start_filtering" )
206- api .filter .live .clear = wrap_explorer_member (" live_filter" , " clear_filter" )
207- api .filter .toggle = wrap_explorer_member (" filters" , " toggle" )
208- api .filter .git .ignored .toggle = wrap_explorer_member_args (" filters" , " toggle" , " git_ignored" )
209- api .filter .git .clean .toggle = wrap_explorer_member_args (" filters" , " toggle" , " git_clean" )
210- api .filter .no_buffer .toggle = wrap_explorer_member_args (" filters" , " toggle" , " no_buffer" )
211- api .filter .custom .toggle = wrap_explorer_member_args (" filters" , " toggle" , " custom" )
212- api .filter .dotfiles .toggle = wrap_explorer_member_args (" filters" , " toggle" , " dotfiles" )
213- api .filter .no_bookmark .toggle = wrap_explorer_member_args (" filters" , " toggle" , " no_bookmark" )
144+ local function hydrate_map (api )
145+ api .map .keymap .current = function () return keymap ().get_keymap () end
146+ end
214147
215- api . marks . get = wrap_node ( wrap_explorer_member ( " marks " , " get " ) )
216- api .marks .list = wrap_explorer_member (" marks" , " list " )
217- api .marks .toggle = wrap_node_or_visual ( wrap_explorer_member (" marks" , " toggle " ) )
218- api .marks .clear = wrap_explorer_member (" marks" , " clear " )
219- api .marks .bulk . delete = wrap_explorer_member (" marks" , " bulk_delete " )
220- api .marks .bulk . trash = wrap_explorer_member (" marks" , " bulk_trash " )
221- api .marks .bulk . move = wrap_explorer_member (" marks" , " bulk_move " )
222- api .marks .navigate .next = wrap_explorer_member (" marks" , " navigate_next" )
223- api .marks .navigate .prev = wrap_explorer_member (" marks" , " navigate_prev" )
148+ local function hydrate_marks ( api )
149+ api .marks .bulk . delete = wrap_explorer_member (" marks" , " bulk_delete " )
150+ api .marks .bulk . move = wrap_explorer_member (" marks" , " bulk_move " )
151+ api .marks .bulk . trash = wrap_explorer_member (" marks" , " bulk_trash " )
152+ api .marks .clear = wrap_explorer_member (" marks" , " clear " )
153+ api .marks .get = wrap_node ( wrap_explorer_member (" marks" , " get " ) )
154+ api .marks .list = wrap_explorer_member (" marks" , " list " )
155+ api .marks .navigate .next = wrap_explorer_member (" marks" , " navigate_next" )
156+ api .marks .navigate .prev = wrap_explorer_member (" marks" , " navigate_prev" )
224157 api .marks .navigate .select = wrap_explorer_member (" marks" , " navigate_select" )
158+ api .marks .toggle = wrap_node_or_visual (wrap_explorer_member (" marks" , " toggle" ))
159+ end
225160
226- api .map .keymap .current = keymap .get_keymap
161+ local function hydrate_node (api )
162+ api .node .buffer .delete = wrap_node (function (node , opts ) actions ().node .buffer .delete (node , opts ) end )
163+ api .node .buffer .wipe = wrap_node (function (node , opts ) actions ().node .buffer .wipe (node , opts ) end )
164+ api .node .collapse = wrap_node (actions ().tree .collapse .node )
165+ api .node .expand = wrap_node (wrap_explorer (" expand_node" ))
166+ api .node .navigate .diagnostics .next = function () return actions ().moves .item .diagnostics_next () end
167+ api .node .navigate .diagnostics .next_recursive = function () return actions ().moves .item .diagnostics_next_recursive () end
168+ api .node .navigate .diagnostics .prev = function () return actions ().moves .item .diagnostics_prev () end
169+ api .node .navigate .diagnostics .prev_recursive = function () return actions ().moves .item .diagnostics_prev_recursive () end
170+ api .node .navigate .git .next = function () return actions ().moves .item .git_next () end
171+ api .node .navigate .git .next_recursive = function () return actions ().moves .item .git_next_recursive () end
172+ api .node .navigate .git .next_skip_gitignored = function () return actions ().moves .item .git_next_skip_gitignored () end
173+ api .node .navigate .git .prev = function () return actions ().moves .item .git_prev () end
174+ api .node .navigate .git .prev_recursive = function () return actions ().moves .item .git_prev_recursive () end
175+ api .node .navigate .git .prev_skip_gitignored = function () return actions ().moves .item .git_prev_skip_gitignored () end
176+ api .node .navigate .opened .next = function () return actions ().moves .item .opened_next () end
177+ api .node .navigate .opened .prev = function () return actions ().moves .item .opened_prev () end
178+ api .node .navigate .parent = wrap_node (actions ().moves .parent .move )
179+ api .node .navigate .parent_close = wrap_node (actions ().moves .parent .move_close )
180+ api .node .navigate .sibling .first = wrap_node (actions ().moves .sibling .first )
181+ api .node .navigate .sibling .last = wrap_node (actions ().moves .sibling .last )
182+ api .node .navigate .sibling .next = wrap_node (actions ().moves .sibling .next )
183+ api .node .navigate .sibling .prev = wrap_node (actions ().moves .sibling .prev )
184+ api .node .open .drop = wrap_node (actions ().node .open_file .drop )
185+ api .node .open .edit = wrap_node (actions ().node .open_file .edit )
186+ api .node .open .horizontal = wrap_node (actions ().node .open_file .horizontal )
187+ api .node .open .horizontal_no_picker = wrap_node (actions ().node .open_file .horizontal_no_picker )
188+ api .node .open .no_window_picker = wrap_node (actions ().node .open_file .no_window_picker )
189+ api .node .open .preview = wrap_node (actions ().node .open_file .preview )
190+ api .node .open .preview_no_picker = wrap_node (actions ().node .open_file .preview_no_picker )
191+ api .node .open .replace_tree_buffer = wrap_node (actions ().node .open_file .replace_tree_buffer )
192+ api .node .open .tab = wrap_node (actions ().node .open_file .tab )
193+ api .node .open .tab_drop = wrap_node (actions ().node .open_file .tab_drop )
194+ api .node .open .toggle_group_empty = wrap_node (actions ().node .open_file .toggle_group_empty )
195+ api .node .open .vertical = wrap_node (actions ().node .open_file .vertical )
196+ api .node .open .vertical_no_picker = wrap_node (actions ().node .open_file .vertical_no_picker )
197+ api .node .run .cmd = wrap_node (actions ().node .run_command .run_file_command )
198+ api .node .run .system = wrap_node (actions ().node .system_open .fn )
199+ api .node .show_info_popup = wrap_node (actions ().node .file_popup .toggle_file_info )
200+ end
227201
228- api .config .global = config .g_clone
229- api .config .user = config .u_clone
202+ local function hydrate_tree (api )
203+ api .tree .change_root = function () return actions ().tree .change_dir .fn () end
204+ api .tree .change_root_to_node = wrap_node (wrap_explorer (" change_dir_to_node" ))
205+ api .tree .change_root_to_parent = wrap_node (wrap_explorer (" dir_up" ))
206+ api .tree .close = function () return view ().close () end
207+ api .tree .close_in_all_tabs = function () return view ().close_all_tabs () end
208+ api .tree .close_in_this_tab = function () return view ().close_this_tab_only () end
209+ api .tree .collapse_all = function () return actions ().tree .collapse .all () end
210+ api .tree .expand_all = wrap_node (wrap_explorer (" expand_all" ))
211+ api .tree .find_file = function () return actions ().tree .find_file .fn () end
212+ api .tree .focus = api .tree .open
213+ api .tree .get_node_under_cursor = wrap_explorer (" get_node_at_cursor" )
214+ api .tree .get_nodes = wrap_explorer (" get_nodes" )
215+ api .tree .is_tree_buf = function () return utils ().is_nvim_tree_buf () end
216+ api .tree .is_visible = function () return view ().is_visible () end
217+ api .tree .open = function () return actions ().tree .open .fn () end
218+ api .tree .reload = wrap_explorer (" reload_explorer" )
219+ api .tree .reload_git = wrap_explorer (" reload_git" )
220+ api .tree .resize = function () return actions ().tree .resize .fn () end
221+ api .tree .search_node = function () return actions ().finders .search_node .fn () end
222+ api .tree .toggle = function () return actions ().tree .toggle .fn () end
223+ api .tree .toggle_help = function () return help ().toggle () end
224+ api .tree .winid = function () return view ().winid () end
225+ end
226+
227+ --- Re-Hydrate api functions and classes post-setup
228+ --- @param api table not properly typed to prevent LSP from referencing implementations
229+ function M .hydrate (api )
230+ -- hydration has been split into functions for readability and formatting
231+ hydrate_config (api )
232+ hydrate_filter (api )
233+ hydrate_fs (api )
234+ hydrate_map (api )
235+ hydrate_marks (api )
236+ hydrate_node (api )
237+ hydrate_tree (api )
230238
231239 -- (Re)hydrate any legacy by mapping to concrete set above
232240 legacy .map_api (api )
0 commit comments