Skip to content

Commit 3d02260

Browse files
committed
refactor(#3255): merge api pre and post
1 parent c902e02 commit 3d02260

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

lua/nvim-tree/api/impl.lua

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
---Hydrates API meta functions with their implementations.
2-
---For performance reasons, all implementation requires must be done at API call time, not when this module is required.
2+
---For startup performance reasons, all API implementation's requires must be
3+
---done at call time, not when this module is required.
34

45
local M = {}
56

7+
---Walk the entire API, hydrating all functions with the error notification.
8+
---Do not hydrate classes i.e. anything with a metatable.
9+
---@param api table not properly typed to prevent LSP from referencing implementations
10+
local function hydrate_not_setup(api)
11+
for k, v in pairs(api) do
12+
if type(v) == "function" then
13+
api[k] = function()
14+
require("nvim-tree.notify").error("nvim-tree setup not called")
15+
end
16+
elseif type(v) == "table" and not getmetatable(v) then
17+
hydrate_not_setup(v)
18+
end
19+
end
20+
end
21+
622
---Returns the node under the cursor.
723
---@return Node?
824
local function node_at_cursor()
@@ -90,29 +106,14 @@ local function __(fn)
90106
end
91107
end
92108

93-
--Hydrates meta api empty definitions pre-setup:
94-
-- - Pre-setup functions will be hydrated with their implementation.
95-
-- - Post-setup functions will notify error: "nvim-tree setup not called"
96-
-- - All classes will be hydrated with their implementations.
97-
--Called once when api is first required
109+
---Hydrates API meta functions pre-setup:
110+
--- Pre-setup functions will be hydrated with their implementation.
111+
--- Post-setup functions will notify error: "nvim-tree setup not called"
112+
--- All classes will be hydrated with their implementations.
113+
---Called once when api is first required
114+
---@param api table not properly typed to prevent LSP from referencing implementations
98115
function M.hydrate_pre_setup(api)
99-
100-
---Walk the api, hydrating all functions with the error notification.
101-
---Do not hydrate classes: anything with a metatable.
102-
---@param t table
103-
local function hydrate_error(t)
104-
for k, v in pairs(t) do
105-
if type(v) == "function" then
106-
t[k] = function()
107-
require("nvim-tree.notify").error("nvim-tree setup not called")
108-
end
109-
elseif type(v) == "table" and not getmetatable(v) then
110-
hydrate_error(v)
111-
end
112-
end
113-
end
114-
115-
hydrate_error(api)
116+
hydrate_not_setup(api)
116117

117118
api.appearance.hi_test = __(function() require("nvim-tree.appearance.hi-test")() end)
118119

0 commit comments

Comments
 (0)