Skip to content

Commit 3bafec9

Browse files
committed
docs(#3088): hydrate API before setup with error functions, with some hydrated with their concrete function, wrap removal TODO
1 parent 02ce4fb commit 3bafec9

4 files changed

Lines changed: 64 additions & 21 deletions

File tree

doc/nvim-tree-lua.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,11 +3108,11 @@ resize({opts}) *nvim_tree.api.tree.resize()*
31083108

31093109
Parameters: ~
31103110
{opts} (`table?`) optional
3111-
{width}
3111+
{width}?
31123112
(`nvim_tree.config.view.width.spec|nvim_tree.config.view.width`)
31133113
New |nvim_tree.config.view| {width} value.
3114-
{absolute} (`integer`) Set the width.
3115-
{relative} (`integer`) Increase or decrease the width.
3114+
{absolute}? (`integer`) Set the width.
3115+
{relative}? (`integer`) Increase or decrease the width.
31163116

31173117
search_node() *nvim_tree.api.tree.search_node()*
31183118
Open the search dialogue.

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local api = require("nvim-tree.api")
12
local log = require("nvim-tree.log")
23
local view = require("nvim-tree.view")
34
local utils = require("nvim-tree.utils")
@@ -793,6 +794,8 @@ function M.setup(conf)
793794

794795
vim.g.NvimTreeSetup = 1
795796
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
797+
798+
require("nvim-tree.api-impl").hydrate_after_setup(api)
796799
end
797800

798801
vim.g.NvimTreeRequired = 1

lua/nvim-tree/api-impl.lua

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,55 @@ local FileLinkNode = require("nvim-tree.node.file-link")
1414
local RootNode = require("nvim-tree.node.root")
1515
local UserDecorator = require("nvim-tree.renderer.decorator.user")
1616

17-
-- hydrates meta api definitions with implementations
18-
return function(api)
17+
local M = {}
18+
19+
---Walk the api, setting all functions to the error notification
20+
---@param t table
21+
local function hydrate_notify(t)
22+
for k, v in pairs(t) do
23+
if type(v) == "function" then
24+
t[k] = function() notify.error("nvim-tree setup not called") end
25+
elseif type(v) == "table" then
26+
hydrate_notify(v)
27+
end
28+
end
29+
end
30+
31+
---Hydrates meta api definition functions with a function.
32+
---- Default: return "nvim-tree setup not called".
33+
---- Exceptions: concrete implementation for API that can be called before setup.
34+
---Call it once when api is first required
35+
---@param api table
36+
function M.hydrate_init(api)
37+
--
38+
-- Hydrate all with the error function
39+
--
40+
hydrate_notify(api)
41+
42+
--
43+
-- Rehydrate implementations that may be called before setup
44+
--
45+
api.events.subscribe = events.subscribe
46+
api.events.Event = events.Event
47+
48+
api.map.default_on_attach = keymap.default_on_attach
49+
50+
api.decorator = {}
51+
---Create a decorator class by calling :extend()
52+
---See :help nvim-tree-decorators
53+
---@type nvim_tree.api.decorator.UserDecorator
54+
api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]
55+
56+
--
57+
-- Map legacy to above.
58+
--
59+
require("nvim-tree.legacy").map_api(api)
60+
end
61+
62+
---Hydrates all API functions with concrete implementations.
63+
---Call this after nvim-tree setup
64+
---@param api table
65+
function M.hydrate_after_setup(api)
1966
---Print error when setup not called.
2067
---@param fn fun(...): any
2168
---@return fun(...): any
@@ -253,9 +300,6 @@ return function(api)
253300
api.node.expand = wrap_node(actions.tree.modifiers.expand.node)
254301
api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node)
255302

256-
---@class ApiNodeDeleteWipeBufferOpts
257-
---@field force boolean|nil default false
258-
259303
api.node.buffer.delete = wrap_node(function(node, opts)
260304
actions.node.buffer.delete(node, opts)
261305
end)
@@ -265,9 +309,6 @@ return function(api)
265309

266310
api.tree.reload_git = wrap_explorer("reload_git")
267311

268-
api.events.subscribe = events.subscribe
269-
api.events.Event = events.Event
270-
271312
api.filter.live.start = wrap_explorer_member("live_filter", "start_filtering")
272313
api.filter.live.clear = wrap_explorer_member("live_filter", "clear_filter")
273314
api.filter.toggle = wrap_explorer_member("filters", "toggle")
@@ -291,17 +332,17 @@ return function(api)
291332

292333
api.map.get_keymap = wrap(keymap.get_keymap)
293334
api.map.get_keymap_default = wrap(keymap.get_keymap_default)
294-
api.map.default_on_attach = keymap.default_on_attach
295335

296336
api.health.hi_test = wrap(appearance_hi_test)
297337

298338
api.commands.get = wrap(function()
299339
return require("nvim-tree.commands").get()
300340
end)
301341

302-
api.decorator = {}
303-
---Create a decorator class by calling :extend()
304-
---See :help nvim-tree-decorators
305-
---@type nvim_tree.api.decorator.UserDecorator
306-
api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]
342+
--
343+
-- Remap legacy to above
344+
--
345+
require("nvim-tree.legacy").map_api(api)
307346
end
347+
348+
return M

lua/nvim-tree/api.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ local api = {}
3535

3636

3737
--
38-
--Load the (empty) meta definitions
38+
-- Load the (empty) meta definitions
3939
--
4040
api.commands = require("nvim-tree._meta.api.commands")
4141
api.events = require("nvim-tree._meta.api.events")
@@ -49,9 +49,8 @@ api.tree = require("nvim-tree._meta.api.tree")
4949

5050

5151
--
52-
--Map implementations
52+
-- Map implementations
5353
--
54-
require("nvim-tree.api-impl")(api)
55-
require("nvim-tree.legacy").map_api(api)
54+
require("nvim-tree.api-impl").hydrate_init(api)
5655

5756
return api

0 commit comments

Comments
 (0)