Skip to content

Commit a79a495

Browse files
committed
docs(#3088): extract post impl functions, lazy some requires
1 parent ba4f507 commit a79a495

File tree

1 file changed

+114
-122
lines changed

1 file changed

+114
-122
lines changed

lua/nvim-tree/api/impl/post.lua

Lines changed: 114 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,131 @@
1-
local core = require("nvim-tree.core")
21
local view = require("nvim-tree.view")
3-
local utils = require("nvim-tree.utils")
42
local actions = require("nvim-tree.actions")
5-
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
6-
local help = require("nvim-tree.help")
7-
local keymap = require("nvim-tree.keymap")
83

94
local DirectoryNode = require("nvim-tree.node.directory")
105
local FileNode = require("nvim-tree.node.file")
116
local FileLinkNode = require("nvim-tree.node.file-link")
127
local RootNode = require("nvim-tree.node.root")
138

14-
---Hydrate all implementations barring those that were called during hydrate_pre
15-
---@param api table
16-
local function hydrate_post(api)
17-
---Invoke a method on the singleton explorer.
18-
---Print error when setup not called.
19-
---@param explorer_method string explorer method name
20-
---@return fun(...): any
21-
local function wrap_explorer(explorer_method)
22-
return function(...)
23-
local explorer = core.get_explorer()
24-
if explorer then
25-
return explorer[explorer_method](explorer, ...)
26-
end
9+
---Invoke a method on the singleton explorer.
10+
---Print error when setup not called.
11+
---@param explorer_method string explorer method name
12+
---@return fun(...): any
13+
local function wrap_explorer(explorer_method)
14+
return function(...)
15+
local explorer = require("nvim-tree.core").get_explorer()
16+
if explorer then
17+
return explorer[explorer_method](explorer, ...)
2718
end
2819
end
20+
end
2921

30-
---Inject the node as the first argument if present otherwise do nothing.
31-
---@param fn fun(node: Node, ...): any
32-
---@return fun(node: Node?, ...): any
33-
local function wrap_node(fn)
34-
return function(node, ...)
35-
node = node or wrap_explorer("get_node_at_cursor")()
36-
if node then
37-
return fn(node, ...)
38-
end
22+
---Inject the node as the first argument if present otherwise do nothing.
23+
---@param fn fun(node: Node, ...): any
24+
---@return fun(node: Node?, ...): any
25+
local function wrap_node(fn)
26+
return function(node, ...)
27+
node = node or wrap_explorer("get_node_at_cursor")()
28+
if node then
29+
return fn(node, ...)
3930
end
4031
end
32+
end
33+
34+
---Inject the node or nil as the first argument if absent.
35+
---@param fn fun(node: Node?, ...): any
36+
---@return fun(node: Node?, ...): any
37+
local function wrap_node_or_nil(fn)
38+
return function(node, ...)
39+
node = node or wrap_explorer("get_node_at_cursor")()
40+
return fn(node, ...)
41+
end
42+
end
4143

42-
---Inject the node or nil as the first argument if absent.
43-
---@param fn fun(node: Node?, ...): any
44-
---@return fun(node: Node?, ...): any
45-
local function wrap_node_or_nil(fn)
46-
return function(node, ...)
47-
node = node or wrap_explorer("get_node_at_cursor")()
48-
return fn(node, ...)
44+
---Invoke a member's method on the singleton explorer.
45+
---Print error when setup not called.
46+
---@param explorer_member string explorer member name
47+
---@param member_method string method name to invoke on member
48+
---@param ... any passed to method
49+
---@return fun(...): any
50+
local function wrap_explorer_member_args(explorer_member, member_method, ...)
51+
local method_args = ...
52+
return function(...)
53+
local explorer = require("nvim-tree.core").get_explorer()
54+
if explorer then
55+
return explorer[explorer_member][member_method](explorer[explorer_member], method_args, ...)
4956
end
5057
end
58+
end
5159

52-
---Invoke a member's method on the singleton explorer.
53-
---Print error when setup not called.
54-
---@param explorer_member string explorer member name
55-
---@param member_method string method name to invoke on member
56-
---@param ... any passed to method
57-
---@return fun(...): any
58-
local function wrap_explorer_member_args(explorer_member, member_method, ...)
59-
local method_args = ...
60-
return function(...)
61-
local explorer = core.get_explorer()
62-
if explorer then
63-
return explorer[explorer_member][member_method](explorer[explorer_member], method_args, ...)
64-
end
60+
---Invoke a member's method on the singleton explorer.
61+
---Print error when setup not called.
62+
---@param explorer_member string explorer member name
63+
---@param member_method string method name to invoke on member
64+
---@return fun(...): any
65+
local function wrap_explorer_member(explorer_member, member_method)
66+
return function(...)
67+
local explorer = require("nvim-tree.core").get_explorer()
68+
if explorer then
69+
return explorer[explorer_member][member_method](explorer[explorer_member], ...)
6570
end
6671
end
72+
end
6773

68-
---Invoke a member's method on the singleton explorer.
69-
---Print error when setup not called.
70-
---@param explorer_member string explorer member name
71-
---@param member_method string method name to invoke on member
72-
---@return fun(...): any
73-
local function wrap_explorer_member(explorer_member, member_method)
74-
return function(...)
75-
local explorer = core.get_explorer()
76-
if explorer then
77-
return explorer[explorer_member][member_method](explorer[explorer_member], ...)
78-
end
74+
---@class NodeEditOpts
75+
---@field quit_on_open boolean|nil default false
76+
---@field focus boolean|nil default true
77+
78+
---@param mode string
79+
---@param node Node
80+
---@param edit_opts NodeEditOpts?
81+
local function edit(mode, node, edit_opts)
82+
local file_link = node:as(FileLinkNode)
83+
local path = file_link and file_link.link_to or node.absolute_path
84+
local cur_tabpage = vim.api.nvim_get_current_tabpage()
85+
86+
actions.node.open_file.fn(mode, path)
87+
88+
edit_opts = edit_opts or {}
89+
90+
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
91+
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
92+
view.close(cur_tabpage)
93+
end
94+
95+
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
96+
local focus = edit_opts.focus == nil or edit_opts.focus == true
97+
if not mode_unsupported_focus and not focus then
98+
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
99+
if mode == "tabnew" then
100+
vim.cmd(":tabprev")
79101
end
102+
view.focus()
80103
end
104+
end
81105

106+
---@param mode string
107+
---@param toggle_group boolean?
108+
---@return fun(node: Node, edit_opts: NodeEditOpts?)
109+
local function open_or_expand_or_dir_up(mode, toggle_group)
110+
---@param node Node
111+
---@param edit_opts NodeEditOpts?
112+
return function(node, edit_opts)
113+
local root = node:as(RootNode)
114+
local dir = node:as(DirectoryNode)
115+
116+
if root or node.name == ".." then
117+
actions.root.change_dir.fn("..")
118+
elseif dir then
119+
dir:expand_or_collapse(toggle_group)
120+
elseif not toggle_group then
121+
edit(mode, node, edit_opts)
122+
end
123+
end
124+
end
125+
126+
---Hydrate all implementations barring those that were called during hydrate_pre
127+
---@param api table
128+
local function hydrate_post(api)
82129
api.tree.open = actions.tree.open.fn
83130
api.tree.focus = api.tree.open
84131

@@ -119,8 +166,8 @@ local function hydrate_post(api)
119166
api.tree.collapse_all = actions.tree.modifiers.collapse.all
120167

121168
api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
122-
api.tree.toggle_help = help.toggle
123-
api.tree.is_tree_buf = utils.is_nvim_tree_buf
169+
api.tree.toggle_help = function() require("nvim-tree.help").toggle() end
170+
api.tree.is_tree_buf = function() require("nvim-tree.utils").is_nvim_tree_buf() end
124171

125172
api.tree.is_visible = view.is_visible
126173

@@ -143,58 +190,6 @@ local function hydrate_post(api)
143190
api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename"))
144191
api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename"))
145192
api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path"))
146-
---
147-
---@class NodeEditOpts
148-
---@field quit_on_open boolean|nil default false
149-
---@field focus boolean|nil default true
150-
151-
---@param mode string
152-
---@param node Node
153-
---@param edit_opts NodeEditOpts?
154-
local function edit(mode, node, edit_opts)
155-
local file_link = node:as(FileLinkNode)
156-
local path = file_link and file_link.link_to or node.absolute_path
157-
local cur_tabpage = vim.api.nvim_get_current_tabpage()
158-
159-
actions.node.open_file.fn(mode, path)
160-
161-
edit_opts = edit_opts or {}
162-
163-
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
164-
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
165-
view.close(cur_tabpage)
166-
end
167-
168-
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
169-
local focus = edit_opts.focus == nil or edit_opts.focus == true
170-
if not mode_unsupported_focus and not focus then
171-
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
172-
if mode == "tabnew" then
173-
vim.cmd(":tabprev")
174-
end
175-
view.focus()
176-
end
177-
end
178-
179-
---@param mode string
180-
---@param toggle_group boolean?
181-
---@return fun(node: Node, edit_opts: NodeEditOpts?)
182-
local function open_or_expand_or_dir_up(mode, toggle_group)
183-
---@param node Node
184-
---@param edit_opts NodeEditOpts?
185-
return function(node, edit_opts)
186-
local root = node:as(RootNode)
187-
local dir = node:as(DirectoryNode)
188-
189-
if root or node.name == ".." then
190-
actions.root.change_dir.fn("..")
191-
elseif dir then
192-
dir:expand_or_collapse(toggle_group)
193-
elseif not toggle_group then
194-
edit(mode, node, edit_opts)
195-
end
196-
end
197-
end
198193

199194
api.node.open.edit = wrap_node(open_or_expand_or_dir_up("edit"))
200195
api.node.open.drop = wrap_node(open_or_expand_or_dir_up("drop"))
@@ -236,12 +231,8 @@ local function hydrate_post(api)
236231
api.node.expand = wrap_node(actions.tree.modifiers.expand.node)
237232
api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node)
238233

239-
api.node.buffer.delete = wrap_node(function(node, opts)
240-
actions.node.buffer.delete(node, opts)
241-
end)
242-
api.node.buffer.wipe = wrap_node(function(node, opts)
243-
actions.node.buffer.wipe(node, opts)
244-
end)
234+
api.node.buffer.delete = wrap_node(function(node, opts) actions.node.buffer.delete(node, opts) end)
235+
api.node.buffer.wipe = wrap_node(function(node, opts) actions.node.buffer.wipe(node, opts) end)
245236

246237
api.tree.reload_git = wrap_explorer("reload_git")
247238

@@ -266,12 +257,13 @@ local function hydrate_post(api)
266257
api.marks.navigate.prev = wrap_explorer_member("marks", "navigate_prev")
267258
api.marks.navigate.select = wrap_explorer_member("marks", "navigate_select")
268259

269-
api.map.get_keymap = keymap.get_keymap
270-
api.map.get_keymap_default = keymap.get_keymap_default
260+
api.map.get_keymap = function() require("nvim-tree.keymap").get_keymap() end
261+
api.map.get_keymap_default = function() require("nvim-tree.keymap").get_keymap_default() end
271262

272-
api.health.hi_test = appearance_hi_test
263+
api.health.hi_test = function() require("nvim-tree.appearance.hi-test")() end
273264

274-
api.commands.get = require("nvim-tree.commands").get
265+
-- TODO #3231 this should be OK pre
266+
api.commands.get = function() require("nvim-tree.commands").get() end
275267
end
276268

277269
---Hydrates all API functions with concrete implementations.

0 commit comments

Comments
 (0)