Skip to content

Commit e466a37

Browse files
committed
docs(#3241): generate decorator help
1 parent dac8aee commit e466a37

8 files changed

Lines changed: 215 additions & 45 deletions

File tree

doc/nvim-tree-lua.txt

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,9 +2362,6 @@ Following is the default configuration, see |nvim_tree.config| for details. >lua
23622362
},
23632363
}
23642364
<
2365-
2366-
2367-
23682365
==============================================================================
23692366
API *nvim-tree-api*
23702367

@@ -3200,4 +3197,120 @@ winid({opts}) *nvim_tree.api.tree.winid()*
32003197
(`integer?`) |window-ID|, nil if tree is not visible.
32013198

32023199

3200+
==============================================================================
3201+
API: Decorator *nvim-tree-api-decorator*
3202+
3203+
*nvim_tree.api.decorator.Decorator*
3204+
Extends: |nvim_tree.Class|
3205+
3206+
Abstract Decorator interface
3207+
3208+
Fields: ~
3209+
{enabled} (`boolean`)
3210+
• {highlight_range} (`nvim_tree.api.decorator.highlight_range`)
3211+
• {icon_placement} (`nvim_tree.api.decorator.icon_placement`)
3212+
• {icon_node} (`fun(self: nvim_tree.api.decorator.Decorator, node: nvim_tree.api.Node): nvim_tree.api.decorator.highlighted_string?`)
3213+
See
3214+
|nvim_tree.api.decorator.Decorator:icon_node()|.
3215+
{icons} (`fun(self: nvim_tree.api.decorator.Decorator, node: nvim_tree.api.Node): nvim_tree.api.decorator.highlighted_string[]?`)
3216+
See |nvim_tree.api.decorator.Decorator:icons()|.
3217+
• {highlight_group} (`fun(self: nvim_tree.api.decorator.Decorator, node: nvim_tree.api.Node): string?`)
3218+
See
3219+
|nvim_tree.api.decorator.Decorator:highlight_group()|.
3220+
• {define_sign} (`fun(self: nvim_tree.api.decorator.Decorator, icon: nvim_tree.api.decorator.highlighted_string?)`)
3221+
See
3222+
|nvim_tree.api.decorator.Decorator:define_sign()|.
3223+
3224+
3225+
*nvim_tree.api.decorator.Decorator:define_sign()*
3226+
Decorator:define_sign({icon})
3227+
Defines a sign. This should be called in the constructor.
3228+
3229+
Parameters: ~
3230+
{icon} (`nvim_tree.api.decorator.highlighted_string?`)
3231+
3232+
*nvim_tree.api.decorator.Decorator:highlight_group()*
3233+
Decorator:highlight_group({node})
3234+
Abstract: optionally implement to provide one highlight group to apply to
3235+
your highlight_range.
3236+
3237+
Parameters: ~
3238+
{node} (`nvim_tree.api.Node`)
3239+
3240+
Return: ~
3241+
(`string?`) highlight_group
3242+
3243+
*nvim_tree.api.decorator.Decorator:icon_node()*
3244+
Decorator:icon_node({node})
3245+
Abstract: optionally implement to set the node's icon
3246+
3247+
Parameters: ~
3248+
{node} (`nvim_tree.api.Node`)
3249+
3250+
Return: ~
3251+
(`nvim_tree.api.decorator.highlighted_string?`) icon_node
3252+
3253+
Decorator:icons({node}) *nvim_tree.api.decorator.Decorator:icons()*
3254+
Abstract: optionally implement to provide icons and the highlight groups
3255+
for your icon_placement.
3256+
3257+
Parameters: ~
3258+
{node} (`nvim_tree.api.Node`)
3259+
3260+
Return: ~
3261+
(`nvim_tree.api.decorator.highlighted_string[]?`) icons
3262+
3263+
3264+
==============================================================================
3265+
API: Class *nvim-tree-api-class*
3266+
3267+
*nvim_tree.Class*
3268+
3269+
Fields: ~
3270+
{super} (`Class`)
3271+
{extend} (`fun(self: nvim_tree.Class)`) See
3272+
|nvim_tree.Class:extend()|.
3273+
{implement} (`fun(self: nvim_tree.Class, mixin: Class)`) See
3274+
|nvim_tree.Class:implement()|.
3275+
{is} (`fun(self: nvim_tree.Class, class: T): boolean`) See
3276+
|nvim_tree.Class:is()|.
3277+
{as} (`fun(self: nvim_tree.Class, class: T): T?`) See
3278+
|nvim_tree.Class:as()|.
3279+
{nop} (`fun(self: nvim_tree.Class, ...: any)`) See
3280+
|nvim_tree.Class:nop()|.
3281+
3282+
3283+
Class:as({class}) *nvim_tree.Class:as()*
3284+
Return object if :is otherwise nil
3285+
3286+
Parameters: ~
3287+
{class} (`any`)
3288+
3289+
Return: ~
3290+
(`T?`)
3291+
3292+
Class:extend() *nvim_tree.Class:extend()*
3293+
Extend a class, setting .super
3294+
3295+
Class:implement({mixin}) *nvim_tree.Class:implement()*
3296+
Implement the functions of a mixin Add the mixin to .implements
3297+
3298+
Parameters: ~
3299+
{mixin} (`Class`)
3300+
3301+
Class:is({class}) *nvim_tree.Class:is()*
3302+
Object is an instance of class or implements a mixin
3303+
3304+
Parameters: ~
3305+
{class} (`any`)
3306+
3307+
Return: ~
3308+
(`boolean`)
3309+
3310+
Class:nop({...}) *nvim_tree.Class:nop()*
3311+
3312+
Parameters: ~
3313+
{...} (`any`)
3314+
3315+
32033316
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,71 @@
11
---@meta
2-
---
32
local nvim_tree = { api = { decorator = {} } }
43

54
local Class = require("nvim-tree.classic")
65

6+
---
77
---Highlight group range as per nvim-tree.renderer.highlight_*
8+
---
89
---@alias nvim_tree.api.decorator.highlight_range nvim_tree.config.renderer.highlight
910

1011
---Icon position as per renderer.icons.*_placement
12+
---
1113
---@alias nvim_tree.api.decorator.icon_placement "none"|nvim_tree.config.renderer.icons.placement
1214

15+
---
1316
---Names of builtin decorators or your decorator classes. Builtins are ordered lowest to highest priority.
17+
---
1418
---@alias nvim_tree.api.decorator.types nvim_tree.api.decorator.Decorator|"Git"|"Opened"|"Hidden"|"Modified"|"Bookmarks"|"Diagnostics"|"Copied"|"Cut"
1519

20+
---
1621
---A string for rendering, with optional highlight groups to apply to it
22+
---
1723
---@class (exact) nvim_tree.api.decorator.highlighted_string
1824
---@field str string
1925
---@field hl string[]
2026

27+
---
2128
---Abstract Decorator interface
2229
---
23-
---@class (exact) nvim_tree.api.decorator.Decorator: Class
24-
---@field protected enabled boolean
25-
---@field protected highlight_range nvim_tree.api.decorator.highlight_range
26-
---@field protected icon_placement nvim_tree.api.decorator.icon_placement
27-
nvim_tree.api.decorator.Decorator = Class:extend()
30+
---@class nvim_tree.api.decorator.Decorator: nvim_tree.Class
31+
---@field enabled boolean
32+
---@field highlight_range nvim_tree.api.decorator.highlight_range
33+
---@field icon_placement nvim_tree.api.decorator.icon_placement
34+
local Decorator = Class:extend()
35+
nvim_tree.api.decorator.Decorator = Decorator
2836

37+
---
2938
---Abstract: optionally implement to set the node's icon
3039
---
3140
---@param node nvim_tree.api.Node
3241
---@return nvim_tree.api.decorator.highlighted_string? icon_node
33-
function nvim_tree.api.decorator.Decorator:icon_node(node) end
42+
function Decorator:icon_node(node) end
3443

44+
---
3545
---Abstract: optionally implement to provide icons and the highlight groups for your icon_placement.
3646
---
3747
---@param node nvim_tree.api.Node
3848
---@return nvim_tree.api.decorator.highlighted_string[]? icons
39-
function nvim_tree.api.decorator.Decorator:icons(node) end
49+
function Decorator:icons(node) end
4050

51+
---
4152
---Abstract: optionally implement to provide one highlight group to apply to your highlight_range.
4253
---
4354
---@param node nvim_tree.api.Node
4455
---@return string? highlight_group
45-
function nvim_tree.api.decorator.Decorator:highlight_group(node) end
56+
function Decorator:highlight_group(node) end
4657

58+
---
4759
---Defines a sign. This should be called in the constructor.
4860
---
49-
---@protected
5061
---@param icon nvim_tree.api.decorator.highlighted_string?
51-
function nvim_tree.api.decorator.Decorator:define_sign(icon) end
62+
function Decorator:define_sign(icon) end
5263

53-
---@deprecated use `nvim_tree.api.decorator.Decorator`
64+
---
5465
---@class nvim_tree.api.decorator.UserDecorator: nvim_tree.api.decorator.Decorator
66+
---@nodoc
67+
---@deprecated use `nvim_tree.api.decorator.Decorator`
68+
---
5569
nvim_tree.api.decorator.UserDecorator = nvim_tree.api.decorator.Decorator
5670

5771
return nvim_tree.api.decorator

lua/nvim-tree/_meta/config/default.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
---}
1010
---```
1111
---
12+
---placeholder for next section [nvim-tree-api]()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function M.hydrate(api)
255255

256256
api.map.keymap.current = keymap.get_keymap
257257

258-
-- (Re)hydrate any legacy by mapping to function set above
258+
-- (Re)hydrate any legacy by mapping to concrete set above
259259
require("nvim-tree.api.impl.legacy").hydrate(api)
260260
end
261261

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function M.hydrate(api)
5252
-- classes
5353
api.decorator.Decorator = UserDecorator:extend()
5454

55-
-- Hydrate any legacy by mapping to function set above
55+
-- Hydrate any legacy by mapping to concrete set above
5656
require("nvim-tree.api.impl.legacy").hydrate(api)
5757
end
5858

lua/nvim-tree/classic.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
-- https://github.com/rxi/classic
1010
--
1111

12-
---@class (exact) Class
12+
---TODO #3241 document and rename
13+
---@class Class: nvim_tree.Class
14+
---@nodoc
15+
16+
---@class nvim_tree.Class
1317
---@field super Class
1418
---@field private implements table<Class, boolean>
1519
local Class = {}

lua/nvim-tree/renderer/decorator/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---@field protected icon_placement nvim_tree.api.decorator.icon_placement
66
local Decorator = require("nvim-tree._meta.api.decorator").Decorator:extend()
77

8-
---TODO #3241 maybe create an internal decorator class and lose the UserDecorator
8+
---TODO #3241 create an internal decorator class with explorer member and lose the UserDecorator
99
---@class (exact) DecoratorArgs
1010
---@field explorer Explorer
1111

scripts/gen_vimdoc_config.lua

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
---@field section string arbitrary
1111
---@field path string relative to root
1212

13+
---TODO #3241 do this for all classes, moving decorator up to nvim_tree.api.Decorator
14+
15+
---Module overrides
16+
---@type table<string, string>
17+
local module_overrides = {
18+
["nvim_tree.classic"] = "nvim_tree"
19+
}
20+
1321
local pre = "runtime/lua/nvim_tree/"
1422

1523
---@type Src[]
@@ -39,24 +47,24 @@ local srcs_config = {
3947
{ helptag = "nvim-tree-config-log", section = "Config: log", path = pre .. "_meta/config/log.lua", },
4048

4149
{ helptag = "nvim-tree-config-default", section = "Config: Default", path = pre .. "_meta/config/default.lua", },
42-
43-
{ helptag = "nvim-tree-api", section = "placeholder for next Config", path = pre .. "api.lua", },
4450
}
4551

4652
---@type Src[]
4753
local srcs_api = {
48-
{ helptag = "nvim-tree-api", section = "API", path = pre .. "api.lua", },
49-
50-
{ helptag = "nvim-tree-api-commands", section = "API: commands", path = pre .. "_meta/api/commands.lua", },
51-
{ helptag = "nvim-tree-api-events", section = "API: events", path = pre .. "_meta/api/events.lua", },
52-
{ helptag = "nvim-tree-api-filter", section = "API: filter", path = pre .. "_meta/api/filter.lua", },
53-
{ helptag = "nvim-tree-api-fs", section = "API: fs", path = pre .. "_meta/api/fs.lua", },
54-
{ helptag = "nvim-tree-api-git", section = "API: git", path = pre .. "_meta/api/git.lua", },
55-
{ helptag = "nvim-tree-api-health", section = "API: health", path = pre .. "_meta/api/health.lua", },
56-
{ helptag = "nvim-tree-api-map", section = "API: map", path = pre .. "_meta/api/map.lua", },
57-
{ helptag = "nvim-tree-api-marks", section = "API: marks", path = pre .. "_meta/api/marks.lua", },
58-
{ helptag = "nvim-tree-api-node", section = "API: node", path = pre .. "_meta/api/node.lua", },
59-
{ helptag = "nvim-tree-api-tree", section = "API: tree", path = pre .. "_meta/api/tree.lua", },
54+
{ helptag = "nvim-tree-api", section = "API", path = pre .. "api.lua", },
55+
56+
{ helptag = "nvim-tree-api-commands", section = "API: commands", path = pre .. "_meta/api/commands.lua", },
57+
{ helptag = "nvim-tree-api-events", section = "API: events", path = pre .. "_meta/api/events.lua", },
58+
{ helptag = "nvim-tree-api-filter", section = "API: filter", path = pre .. "_meta/api/filter.lua", },
59+
{ helptag = "nvim-tree-api-fs", section = "API: fs", path = pre .. "_meta/api/fs.lua", },
60+
{ helptag = "nvim-tree-api-git", section = "API: git", path = pre .. "_meta/api/git.lua", },
61+
{ helptag = "nvim-tree-api-health", section = "API: health", path = pre .. "_meta/api/health.lua", },
62+
{ helptag = "nvim-tree-api-map", section = "API: map", path = pre .. "_meta/api/map.lua", },
63+
{ helptag = "nvim-tree-api-marks", section = "API: marks", path = pre .. "_meta/api/marks.lua", },
64+
{ helptag = "nvim-tree-api-node", section = "API: node", path = pre .. "_meta/api/node.lua", },
65+
{ helptag = "nvim-tree-api-tree", section = "API: tree", path = pre .. "_meta/api/tree.lua", },
66+
{ helptag = "nvim-tree-api-decorator", section = "API: Decorator", path = pre .. "_meta/api/decorator.lua", },
67+
{ helptag = "nvim-tree-api-class", section = "API: Class", path = pre .. "classic.lua", },
6068
}
6169

6270
---Map paths to file names
@@ -92,6 +100,17 @@ local function src_by_name(name, srcs)
92100
error(string.format("\n\nPath for lower, extension stripped file name='%s' not found in\nsrcs=%s\n", name, vim.inspect(srcs)))
93101
end
94102

103+
---HACK
104+
---Problem:
105+
--- Generator generates fields for a class' methods.
106+
--- This is a problem as method fields don't have a module and aren't transformed.
107+
--- Method field fun only contains: classvar, desc, name and (function) type
108+
---Solution:
109+
--- Collect a map of "class:method" to modules when the real method passes through fn_xform
110+
--- This works as the real method function is processed before the field method.
111+
---@type table<string, string>
112+
local modules_by_method = {}
113+
95114
-- @type nvim.gen_vimdoc.Config[]
96115
return {
97116
-- Config
@@ -110,23 +129,42 @@ return {
110129
section_fmt = function(name) return src_by_name(name, srcs_api).section end,
111130
helptag_fmt = function(name) return src_by_name(name, srcs_api).helptag end,
112131

113-
-- optional, no default xform to override
114132
fn_xform = function(fun)
115-
if (fun.module) then
116-
-- generator doesn't strip meta
117-
-- also cascades into fn_helptag_fmt
118-
local module = fun.module:gsub("._meta", "", 1)
119-
120-
-- remove the API prefix from the left aligned function name
121-
-- this will cascade into fn_helptag_fmt, which will apply the module prefix anyway
122-
local name, replaced = fun.name:gsub("^" .. module .. "%.", "", 1)
133+
-- generator doesn't strip _meta; this propagates to fn_helptag_fmt
134+
fun.module = fun.module:gsub("._meta", "", 1)
135+
fun.module = module_overrides[fun.module] or fun.module
136+
137+
if not fun.class then
138+
-- functions require the module to be stripped from the name, classes do not
139+
local name, replaced = fun.name:gsub("^" .. fun.module .. "%.", "", 1)
123140
if (replaced ~= 1) then
124-
error(string.format("\n\nfun.name='%s' does not start with _meta stripped module='%s'\nfun=%s", fun.name, module, vim.inspect(fun)))
141+
error(string.format("\n\nfn_xform: fun.name='%s' does not start with _meta stripped \nfun=%s",
142+
fun.name, vim.inspect(fun)))
125143
end
126-
127-
fun.module = module
128144
fun.name = name
145+
elseif fun.class and fun.classvar and fun.name then
146+
-- note the module for use by method fields
147+
modules_by_method[fun.classvar .. ":" .. fun.name] = fun.module
148+
end
149+
end,
150+
151+
-- fn_helptag_fmt_common modified:
152+
-- module prepended to classes
153+
-- module is fetched from modules_by_method when fun.module unavailable
154+
fn_helptag_fmt = function(fun)
155+
local fn_sfx = fun.table and "" or "()"
156+
if fun.classvar then
157+
local module = fun.module or modules_by_method[fun.classvar .. ":" .. fun.name]
158+
if not module then
159+
error(string.format("\n\nfn_helptag_fmt: no module:\nfun=%s\nmodules_by_method=%s",
160+
vim.inspect(fun), vim.inspect(modules_by_method)))
161+
end
162+
return string.format("%s.%s:%s%s", module, fun.classvar, fun.name, fn_sfx)
163+
end
164+
if fun.module then
165+
return string.format("%s.%s%s", fun.module, fun.name, fn_sfx)
129166
end
167+
return fun.name .. fn_sfx
130168
end,
131169
}
132170
}

0 commit comments

Comments
 (0)