Skip to content

Commit 93cf3f9

Browse files
committed
feat(#2948): document API
1 parent 294a96e commit 93cf3f9

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

lua/nvim-tree/_meta/api_decorator.lua

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,56 @@ error("Cannot require a meta file")
33

44
local nvim_tree = { api = { decorator = { BaseDecorator = {} } } }
55

6+
---Create a custom decorator, extending nvim_tree.api.decorator.BaseDecorator
7+
---It may:
8+
--- Add icons
9+
--- Set name highlight group
10+
--- Override node icon
11+
---Class must be created via nvim_tree.api.decorator.create()
12+
---Mandatory constructor :new() will be called once per tree render, with no arguments.
13+
---Constructor must call:
14+
--- :init
15+
--- :define_sign when using "signcolumn" range
16+
617
---Highlight group range as per nvim-tree.renderer.highlight_*
718
---@alias nvim_tree.api.decorator.HighlightRange "none" | "icon" | "name" | "all"
819

920
---Icon position as per renderer.icons.*_placement
1021
---@alias nvim_tree.api.decorator.IconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"
1122

12-
--
13-
-- BaseDecorator Class, see example implementation below
14-
--
23+
---Names of predefined decorators or your decorator classes
24+
---@alias nvim_tree.api.decorator.Name "Cut" | "Copied" | "Diagnostics" | "Bookmarks" | "Modified" | "Hidden" | "Opened" | "Git" | nvim_tree.api.decorator.BaseDecorator
1525

16-
---User defined decorator to optionally add:
17-
--- Additional icons
18-
--- Name highlight group
19-
--- Node icon override
20-
---Class must be created via nvim_tree.api.decorator.BaseDecorator:extend()
21-
---Mandatory constructor :new() will be called once per tree render, with no arguments.
22-
---Constructor must call:
23-
--- .super.new(self, args) passing nvim_tree.api.decorator.BaseDecoratorArgs
24-
--- :define_sign(...) when using "signcolumn" range
26+
---BaseDecorator Class, your decorator will extend this
2527
---@class (exact) nvim_tree.api.decorator.BaseDecorator
2628
---@field protected enabled boolean
2729
---@field protected highlight_range nvim_tree.api.decorator.HighlightRange
2830
---@field protected icon_placement nvim_tree.api.decorator.IconPlacement
2931

30-
---Constructor Arguments
31-
---@class (exact) nvim_tree.api.decorator.BaseDecoratorArgs
32+
---No-args constructor must be implemented
33+
function nvim_tree.api.decorator.BaseDecorator:new() end
34+
35+
---Must be called from your constructor
36+
---@class (exact) nvim_tree.api.decorator.InitArgs
3237
---@field enabled boolean
3338
---@field highlight_range nvim_tree.api.decorator.HighlightRange
3439
---@field icon_placement nvim_tree.api.decorator.IconPlacement
35-
36-
---Use to instantiate your decorator class
37-
function nvim_tree.api.decorator.BaseDecorator:extend() end
38-
39-
---Super constructor must be called from your constructor
40-
---BaseDecorator.super.new(self, args)
40+
---
4141
---@protected
42-
---@param self nvim_tree.api.decorator.BaseDecorator your instance
43-
---@param args nvim_tree.api.decorator.BaseDecoratorArgs
44-
function nvim_tree.api.decorator.BaseDecorator.new(self, args) end
42+
---@param args nvim_tree.api.decorator.InitArgs
43+
function nvim_tree.api.decorator.BaseDecorator:init(args) end
4544

46-
---Must implement a constructor and call super
47-
function nvim_tree.api.decorator.BaseDecorator:new() end
48-
49-
---Implement this method to set the node's icon
45+
---Optionally implement this method to set the node's icon
5046
---@param node nvim_tree.api.Node
5147
---@return HighlightedString? icon_node
5248
function nvim_tree.api.decorator.BaseDecorator:icon_node(node) end
5349

54-
---Implement this method to provide icons and the highlight groups to apply to IconPlacement
50+
---Optionally implement this method to provide icons and the highlight groups for your icon_placement
5551
---@param node nvim_tree.api.Node
5652
---@return HighlightedString[]? icons
5753
function nvim_tree.api.decorator.BaseDecorator:icons(node) end
5854

59-
---Implement this method to provide one highlight group to apply to HighlightRange
55+
---Optionally implement this method to provide one highlight group to apply to your highlight_range
6056
---@param node nvim_tree.api.Node
6157
---@return string? highlight_group
6258
function nvim_tree.api.decorator.BaseDecorator:highlight_group(node) end
@@ -65,23 +61,21 @@ function nvim_tree.api.decorator.BaseDecorator:highlight_group(node) end
6561
-- Example Decorator
6662
--
6763

68-
local BaseDecorator = require("nvim-tree.api").decorator.BaseDecorator
69-
7064
---@class (exact) MyDecorator: nvim_tree.api.decorator.BaseDecorator
7165
---@field private my_icon nvim_tree.api.HighlightedString
72-
local MyDecorator = BaseDecorator:extend()
66+
local MyDecorator = require("nvim-tree.api").decorator.create()
7367

7468
---Mandatory constructor :new() will be called once per tree render, with no arguments.
7569
function MyDecorator:new()
76-
----@type nvim_tree.api.decorator.BaseDecoratorArgs
70+
---@type nvim_tree.api.decorator.InitArgs
7771
local args = {
7872
enabled = true,
7973
highlight_range = "all",
8074
icon_placement = "signcolumn",
8175
}
8276

83-
-- construct super with args
84-
BaseDecorator.new(self, args)
77+
-- init
78+
self:init(args)
8579

8680
-- create your icon once, for convenience
8781
self.my_icon = { str = "I", hl = { "MyIcon" } }

lua/nvim-tree/api.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local decorator_registry = require("nvim-tree.renderer.decorator.registry")
1212
local DirectoryNode = require("nvim-tree.node.directory")
1313
local FileLinkNode = require("nvim-tree.node.file-link")
1414
local RootNode = require("nvim-tree.node.root")
15+
local DecoratorUser = require("nvim-tree.renderer.decorator.user")
1516

1617
local Api = {
1718
tree = {},
@@ -313,10 +314,26 @@ Api.commands.get = wrap(function()
313314
return require("nvim-tree.commands").get()
314315
end)
315316

316-
-- TODO provide a registration convenience to hide classic
317-
-- TODO add doc
318-
Api.decorator.BaseDecorator = require("nvim-tree.renderer.decorator.user") --[[@as nvim_tree.api.decorator.BaseDecorator ]]
319-
Api.decorator.register = decorator_registry.register
320-
Api.decorator.unregister = decorator_registry.unregister
317+
---Create a new decorator class
318+
---
319+
---@return nvim_tree.api.decorator.BaseDecorator
320+
Api.decorator.create = function() return DecoratorUser:extend() end
321+
322+
---Register a decorator class
323+
---
324+
---@class RegisterOpts
325+
---@field decorator nvim_tree.api.decorator.BaseDecorator
326+
---@field below nvim_tree.api.decorator.Name?
327+
---
328+
---@param opts RegisterOpts
329+
Api.decorator.register = function(opts) decorator_registry.register(opts) end
330+
331+
---Unregister a decorator class
332+
---
333+
---@class UnRegisterOpts
334+
---@field decorator nvim_tree.api.decorator.BaseDecorator
335+
---
336+
---@param opts UnRegisterOpts
337+
Api.decorator.unregister = function(opts) decorator_registry.unregister(opts) end
321338

322339
return Api

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local notify = require("nvim-tree.notify")
21
local utils = require("nvim-tree.utils")
32

43
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
@@ -11,12 +10,8 @@ local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
1110
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
1211
local DecoratorUser = require("nvim-tree.renderer.decorator.user")
1312

14-
-- Globally registered decorators including user
15-
-- Lowest priority first
16-
17-
---@alias DecoratorName nvim_tree.api.decorator.BaseDecorator | "Cut" | "Copied" | "Diagnostics" | "Bookmarks" | "Modified" | "Hidden" | "Opened" | "Git"
18-
1913
local M = {
14+
-- Globally registered decorators including user. Lowest priority first.
2015
---@type Decorator[]
2116
registered = {
2217
DecoratorGit,
@@ -30,18 +25,13 @@ local M = {
3025
}
3126
}
3227

33-
---@class RegisterOpts
34-
---@field decorator nvim_tree.api.decorator.BaseDecorator
35-
---@field below DecoratorName?
36-
3728
---@param opts RegisterOpts
3829
function M.register(opts)
3930
if not opts or not opts.decorator then
4031
return
4132
end
4233

4334
if vim.tbl_contains(M.registered, opts.decorator) then
44-
notify.error("decorator already registered")
4535
return
4636
end
4737

@@ -56,9 +46,6 @@ function M.register(opts)
5646
table.insert(M.registered, opts.decorator)
5747
end
5848

59-
---@class UnRegisterOpts
60-
---@field decorator nvim_tree.api.decorator.BaseDecorator
61-
6249
---@param opts UnRegisterOpts
6350
function M.unregister(opts)
6451
if not opts or not opts.decorator then

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ local Decorator = require("nvim-tree.renderer.decorator")
44
---@class (exact) DecoratorUser: Decorator
55
local DecoratorUser = Decorator:extend()
66

7+
---User calls this instead of new
8+
---@param args nvim_tree.api.decorator.InitArgs
9+
function DecoratorUser:init(args)
10+
DecoratorUser.super.new(self, args)
11+
end
12+
713
return DecoratorUser

0 commit comments

Comments
 (0)