Skip to content

Commit 338e95a

Browse files
committed
docs(#3088): move api help and classes back into api.lua
1 parent 63e3679 commit 338e95a

6 files changed

Lines changed: 136 additions & 125 deletions

File tree

lua/nvim-tree/_meta/api.lua

Lines changed: 0 additions & 112 deletions
This file was deleted.

lua/nvim-tree/_meta/classes.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---@meta
2+
error("Cannot require a meta file")
3+
4+
5+
-- These node subclasses are not ready for public exposure as they are:
6+
-- - not classic classes
7+
-- - only used in a few locations: api.tree.get_nodes and UserDecorator
8+
9+
---
10+
---File
11+
---
12+
---@class (exact) nvim_tree.api.FileNode: nvim_tree.api.Node
13+
---@field extension string
14+
15+
---
16+
---Directory
17+
---
18+
---@class (exact) nvim_tree.api.DirectoryNode: nvim_tree.api.Node
19+
---@field has_children boolean
20+
---@field nodes nvim_tree.api.Node[]
21+
---@field open boolean
22+
23+
---
24+
---Root Directory
25+
---
26+
---@class (exact) nvim_tree.api.RootNode: nvim_tree.api.DirectoryNode
27+
28+
---
29+
---Link mixin
30+
---
31+
---@class (exact) nvim_tree.api.LinkNode
32+
---@field link_to string
33+
---@field fs_stat_target uv.fs_stat.result
34+
35+
---
36+
---File Link
37+
---
38+
---@class (exact) nvim_tree.api.FileLinkNode: nvim_tree.api.FileNode, nvim_tree.api.LinkNode
39+
40+
---
41+
---DirectoryLink
42+
---
43+
---@class (exact) nvim_tree.api.DirectoryLinkNode: nvim_tree.api.DirectoryNode, nvim_tree.api.LinkNode

lua/nvim-tree/api.lua

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,75 @@
11
local api = {}
22

3+
---@brief
4+
---nvim-tree exposes a public API. This is non breaking, with additions made as necessary.
5+
---
6+
---Please do not require or use modules other than `nvim-tree.api`, as internal modules will change without notice.
7+
---
8+
---The API is separated into multiple modules:
9+
---
10+
---- [nvim-tree-api-commands]
11+
---- [nvim-tree-api-events]
12+
---- [nvim-tree-api-filter]
13+
---- [nvim-tree-api-fs]
14+
---- [nvim-tree-api-health]
15+
---- [nvim-tree-api-map]
16+
---- [nvim-tree-api-marks]
17+
---- [nvim-tree-api-node]
18+
---- [nvim-tree-api-tree]
19+
---
20+
---Modules are accessed via `api.<module>.<function>`
21+
---
22+
---Example invocation of the `reload` function in the `tree` module:
23+
---```lua
24+
---
25+
---local api = require("nvim-tree.api")
26+
---api.tree.reload()
27+
---```
28+
---Generally, functions accepting a [nvim_tree.api.Node] as their first argument will use the node under the cursor when that argument is not present or nil. e.g. the following are functionally identical:
29+
---```lua
30+
---
31+
---api.node.open.edit(nil, { focus = true })
32+
---
33+
---api.node.open.edit(api.tree.get_node_under_cursor(), { focus = true })
34+
---```
35+
36+
37+
38+
---The Node class is a data class. Instances may be provided by API functions for use as a:
39+
---- handle to pass back to API functions e.g. [nvim_tree.api.node.run.cmd()]
40+
---- reference in callbacks e.g. [nvim_tree.config.sort.Sorter] {sorter}
41+
---
42+
---Please do not mutate the contents of any Node object.
43+
---
44+
---@class nvim_tree.api.Node
45+
---@field absolute_path string of the file or directory
46+
---@field name string file or directory name
47+
---@field parent? nvim_tree.api.DirectoryNode parent directory, nil for root
48+
---@field type "file" | "directory" | "link" [uv.fs_stat()] {type}
49+
---@field executable boolean file is executable
50+
---@field fs_stat? uv.fs_stat.result at time of last tree display, see [uv.fs_stat()]
51+
---@field git_status nvim_tree.git.Status? for files and directories
52+
---@field diag_severity? lsp.DiagnosticSeverity diagnostic status
53+
---@field hidden boolean node is not visible in the tree
54+
55+
56+
57+
---
58+
---Git statuses for a single node.
59+
---
60+
---`nvim_tree.git.XY`: 2 character string, see `man 1 git-status` "Short Format"
61+
---@alias nvim_tree.git.XY string
62+
---
63+
---{dir} status is derived from its contents:
64+
---- `direct`: inherited from child files
65+
---- `indirect`: inherited from child directories
66+
---
67+
---@class nvim_tree.git.Status
68+
---@field file? nvim_tree.git.XY status of a file node
69+
---@field dir? table<"direct" | "indirect", nvim_tree.git.XY[]> direct inclusive-or indirect status
70+
71+
72+
373
--
474
-- Load the (empty) meta definitions
575
--
@@ -13,9 +83,13 @@ api.marks = require("nvim-tree._meta.api.marks")
1383
api.node = require("nvim-tree._meta.api.node")
1484
api.tree = require("nvim-tree._meta.api.tree")
1585

86+
87+
1688
--
17-
-- Map implementations
89+
-- Map before-setup implementations, most throw an error notification "nvim-tree setup not called".
1890
--
1991
require("nvim-tree.api.impl.pre")(api)
2092

93+
94+
2195
return api

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
---Hydrates all API functions with concrete implementations.
2+
---All "nvim-tree setup not called" error functions from pre.lua will be replaced.
3+
---
4+
---Call this after nvim-tree setup
5+
---
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.
8+
19
local view = require("nvim-tree.view")
210
local actions = require("nvim-tree.actions")
311

@@ -243,13 +251,7 @@ local function hydrate_post(api)
243251
api.map.get_keymap = function() require("nvim-tree.keymap").get_keymap() end
244252
end
245253

246-
---Hydrates all API functions with concrete implementations.
247-
---All "nvim-tree setup not called" error functions will be replaced.
248-
---
249-
---Call this after nvim-tree setup
250-
---
251-
---This is expensive as there are many cascading requires and is avoided
252-
---until after setup has been called, so that the user may require API cheaply.
254+
---Re-hydrate api
253255
---@param api table
254256
return function(api)
255257
-- All concrete implementations

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
--Hydrates meta api empty definition functions with a new function:
2+
-- - Default: error notification "nvim-tree setup not called".
3+
-- - Exceptions: concrete implementation for API that can be called before setup.
4+
--
5+
--Call it once when api is first required
6+
--
17
--This file should have minimal requires that are cheap and have no dependencies or are already required.
8+
--
29
--Everything must be as lazily loaded as possible: the user must be able to require api cheaply.
310

411
local commands = require("nvim-tree.commands") -- already required by plugin.lua
@@ -58,10 +65,7 @@ local function hydrate_pre(api)
5865
api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]
5966
end
6067

61-
--Hydrates meta api empty definition functions with a new function:
62-
-- - Default: error notification "nvim-tree setup not called".
63-
-- - Exceptions: concrete implementation for API that can be called before setup.
64-
--Call it once when api is first required
68+
---Hydrate api
6569
---@param api table
6670
return function(api)
6771
-- Default: error

scripts/gen_vimdoc_config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ local srcs = {
3838

3939
{ helptag = "nvim-tree-config-default", section = "Config: Default", path = "./lua/nvim_tree/_meta/config/default.lua", },
4040

41-
{ helptag = "nvim-tree-api", section = "API", path = "./lua/nvim_tree/_meta/api.lua", },
41+
{ helptag = "nvim-tree-api", section = "API", path = "./lua/nvim_tree/api.lua", },
4242

4343
{ helptag = "nvim-tree-api-commands", section = "API: commands", path = "./lua/nvim_tree/_meta/api/commands.lua", },
4444
{ helptag = "nvim-tree-api-events", section = "API: events", path = "./lua/nvim_tree/_meta/api/events.lua", },

0 commit comments

Comments
 (0)