Skip to content

Commit 9f96903

Browse files
committed
docs(#3241): api exposes nvim_tree.api.decorator.Decorator interface, UserDecorator deprecated
1 parent 5a2e128 commit 9f96903

File tree

10 files changed

+78
-139
lines changed

10 files changed

+78
-139
lines changed

doc/nvim-tree-lua.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -890,31 +890,31 @@ Require and register it during |nvim-tree-setup|:
890890
<
891891
Contents of `my-decorator.lua`:
892892
>lua
893-
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
893+
---@class (exact) MyDecorator: nvim_tree.api.decorator.Decorator
894894
---@field private my_icon1 nvim_tree.api.decorator.highlighted_string
895895
---@field private my_icon2 nvim_tree.api.decorator.highlighted_string
896896
---@field private my_icon_node nvim_tree.api.decorator.highlighted_string
897897
---@field private my_highlight_group string
898-
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()
899-
898+
local MyDecorator = require("nvim-tree.api").decorator.Decorator:extend()
899+
900900
---Mandatory constructor :new() will be called once per tree render, with no arguments.
901901
function MyDecorator:new()
902902
self.enabled = true
903903
self.highlight_range = "name"
904904
self.icon_placement = "after"
905-
905+
906906
-- create your icons and highlights once, applied to every node
907907
self.my_icon1 = { str = "1", hl = { "DiffAdd" } }
908908
self.my_icon2 = { str = "2", hl = { "DiffText" } }
909909
self.my_icon_node = { str = "N", hl = { "Error" } }
910910
self.my_highlight_group = "IncSearch"
911-
911+
912912
-- Define the icon signs only once
913913
-- Only needed if you are using icon_placement = "signcolumn"
914914
-- self:define_sign(self.my_icon1)
915915
-- self:define_sign(self.my_icon2)
916916
end
917-
917+
918918
---Override node icon
919919
---@param node nvim_tree.api.Node
920920
---@return nvim_tree.api.decorator.highlighted_string? icon_node
@@ -925,7 +925,7 @@ Contents of `my-decorator.lua`:
925925
return nil
926926
end
927927
end
928-
928+
929929
---Return two icons for DecoratorIconPlacement "after"
930930
---@param node nvim_tree.api.Node
931931
---@return nvim_tree.api.decorator.highlighted_string[]? icons
@@ -936,7 +936,7 @@ Contents of `my-decorator.lua`:
936936
return nil
937937
end
938938
end
939-
939+
940940
---Exactly one highlight group for DecoratorHighlightRange "name"
941941
---@param node nvim_tree.api.Node
942942
---@return string? highlight_group
@@ -947,7 +947,7 @@ Contents of `my-decorator.lua`:
947947
return nil
948948
end
949949
end
950-
950+
951951
return MyDecorator
952952
<
953953
==============================================================================
@@ -1366,7 +1366,7 @@ Config: renderer *nvim-tree-config-renderer*
13661366
• {symlink_destination}? (`boolean`, default: `true`) Appends an
13671367
arrow followed by the target of the
13681368
symlink.
1369-
{decorators}? (`(string|nvim_tree.api.decorator.UserDecorator)[]`)
1369+
{decorators}? (`(string|nvim_tree.api.decorator.Decorator)[]`)
13701370
(default:
13711371
`{ "Git", "Open", "Hidden", "Modified", "Bookmark", "Diagnostics", "Copied", "Cut", }`)
13721372
• {highlight_git}? (`nvim_tree.config.renderer.highlight`)

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ function M.setup(conf)
813813
vim.g.NvimTreeSetup = 1
814814
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
815815

816-
require("nvim-tree.api.impl.post")(api)
816+
require("nvim-tree.api.impl.post").hydrate(api)
817817
end
818818

819819
vim.g.NvimTreeRequired = 1
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
11
---@meta
2-
error("Cannot require a meta file")
3-
2+
---
43
local nvim_tree = { api = { decorator = {} } }
54

5+
local Class = require("nvim-tree.classic")
6+
67
---Highlight group range as per nvim-tree.renderer.highlight_*
78
---@alias nvim_tree.api.decorator.highlight_range nvim_tree.config.renderer.highlight
89

910
---Icon position as per renderer.icons.*_placement
1011
---@alias nvim_tree.api.decorator.icon_placement "none"|nvim_tree.config.renderer.icons.placement
1112

1213
---Names of builtin decorators or your decorator classes. Builtins are ordered lowest to highest priority.
13-
---@alias nvim_tree.api.decorator.types nvim_tree.api.decorator.UserDecorator|"Git"|"Opened"|"Hidden"|"Modified"|"Bookmarks"|"Diagnostics"|"Copied"|"Cut"
14+
---@alias nvim_tree.api.decorator.types nvim_tree.api.decorator.Decorator|"Git"|"Opened"|"Hidden"|"Modified"|"Bookmarks"|"Diagnostics"|"Copied"|"Cut"
1415

1516
---A string for rendering, with optional highlight groups to apply to it
1617
---@class (exact) nvim_tree.api.decorator.highlighted_string
1718
---@field str string
1819
---@field hl string[]
1920

20-
---Custom decorator, see :help nvim-tree-decorators
21+
---Abstract Decorator interface
2122
---
22-
---@class (exact) nvim_tree.api.decorator.UserDecorator
23+
---@class (exact) nvim_tree.api.decorator.Decorator: Class
2324
---@field protected enabled boolean
2425
---@field protected highlight_range nvim_tree.api.decorator.highlight_range
2526
---@field protected icon_placement nvim_tree.api.decorator.icon_placement
26-
nvim_tree.api.decorator.UserDecorator = {}
27-
28-
---Create your decorator class
29-
---
30-
function nvim_tree.api.decorator.UserDecorator:extend() end
31-
32-
---Abstract: no-args constructor must be implemented and will be called once per tree render.
33-
---Must set all fields.
34-
---
35-
function nvim_tree.api.decorator.UserDecorator:new() end
27+
nvim_tree.api.decorator.Decorator = Class:extend()
3628

3729
---Abstract: optionally implement to set the node's icon
3830
---
3931
---@param node nvim_tree.api.Node
4032
---@return nvim_tree.api.decorator.highlighted_string? icon_node
41-
function nvim_tree.api.decorator.UserDecorator:icon_node(node) end
33+
function nvim_tree.api.decorator.Decorator:icon_node(node) end
4234

4335
---Abstract: optionally implement to provide icons and the highlight groups for your icon_placement.
4436
---
4537
---@param node nvim_tree.api.Node
4638
---@return nvim_tree.api.decorator.highlighted_string[]? icons
47-
function nvim_tree.api.decorator.UserDecorator:icons(node) end
39+
function nvim_tree.api.decorator.Decorator:icons(node) end
4840

4941
---Abstract: optionally implement to provide one highlight group to apply to your highlight_range.
5042
---
5143
---@param node nvim_tree.api.Node
5244
---@return string? highlight_group
53-
function nvim_tree.api.decorator.UserDecorator:highlight_group(node) end
45+
function nvim_tree.api.decorator.Decorator:highlight_group(node) end
5446

55-
---Define a sign. This should be called in the constructor.
47+
---Defines a sign. This should be called in the constructor.
5648
---
5749
---@protected
5850
---@param icon nvim_tree.api.decorator.highlighted_string?
59-
function nvim_tree.api.decorator.UserDecorator:define_sign(icon) end
51+
function nvim_tree.api.decorator.Decorator:define_sign(icon) end
52+
53+
---@deprecated use `nvim_tree.api.decorator.Decorator`
54+
---@class nvim_tree.api.decorator.UserDecorator: nvim_tree.api.decorator.Decorator
55+
nvim_tree.api.decorator.UserDecorator = nvim_tree.api.decorator.Decorator
56+
57+
return nvim_tree.api.decorator

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ error("Cannot require a meta file")
6262
---@field symlink_destination? boolean
6363
---
6464
---(default: `{ "Git", "Open", "Hidden", "Modified", "Bookmark", "Diagnostics", "Copied", "Cut", }`)
65-
---@field decorators? (string|nvim_tree.api.decorator.UserDecorator)[]
65+
---@field decorators? (string|nvim_tree.api.decorator.Decorator)[]
6666
---
6767
---(default: `"none"`)
6868
---@field highlight_git? nvim_tree.config.renderer.highlight

lua/nvim-tree/api.lua

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@
6969

7070

7171

72-
--
73-
-- Load the (empty) meta definitions
74-
--
75-
local deprecated = require("nvim-tree._meta.api.deprecated")
76-
72+
---
7773
---nvim-tree Public API
74+
---
7875
---@class nvim_tree.api
7976
---@nodoc
8077
local api = {
8178
commands = require("nvim-tree._meta.api.commands"),
79+
decorator = require("nvim-tree._meta.api_decorator"),
8280
events = require("nvim-tree._meta.api.events"),
8381
filter = require("nvim-tree._meta.api.filter"),
8482
fs = require("nvim-tree._meta.api.fs"),
@@ -89,21 +87,11 @@ local api = {
8987
node = require("nvim-tree._meta.api.node"),
9088
tree = require("nvim-tree._meta.api.tree"),
9189

92-
config = deprecated.config, ---@deprecated
93-
diagnostics = deprecated.diagnostics, ---@deprecated
94-
live_filter = deprecated.live_filter, ---@deprecated
90+
config = require("nvim-tree._meta.api.deprecated").config, ---@deprecated
91+
diagnostics = require("nvim-tree._meta.api.deprecated").diagnostics, ---@deprecated
92+
live_filter = require("nvim-tree._meta.api.deprecated").live_filter, ---@deprecated
9593
}
9694

97-
98-
--
99-
-- Map before-setup function implementations, most throw an error notification "nvim-tree setup not called".
100-
--
101-
require("nvim-tree.api.impl.pre")(api)
102-
103-
104-
--#TODO 3241
105-
--Public API classes
106-
--api.decorator = require("nvim-tree.api.decorator")
107-
95+
require("nvim-tree.api.impl.pre").hydrate(api)
10896

10997
return api

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
local M = {}
2+
13
---Silently create new api entries pointing legacy functions to current
24
---@param api table not properly typed to prevent LSP from referencing implementations
3-
return function(api)
5+
function M.hydrate(api)
46
api.config = api.config or {}
57
api.config.mappings = api.config.mappings or {}
68
api.config.mappings.get_keymap = api.map.keymap.current
@@ -22,4 +24,8 @@ return function(api)
2224

2325
api.diagnostics = api.diagnostics or {}
2426
api.diagnostics.hi_test = api.health.hi_test
27+
28+
api.decorator.UserDecorator = api.decorator.Decorator
2529
end
30+
31+
return M

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ local DirectoryNode = require("nvim-tree.node.directory")
1616
local FileLinkNode = require("nvim-tree.node.file-link")
1717
local RootNode = require("nvim-tree.node.root")
1818

19+
local M = {}
20+
1921
---Invoke a method on the singleton explorer.
2022
---Print error when setup not called.
2123
---@param explorer_method string explorer method name
@@ -133,9 +135,9 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
133135
end
134136
end
135137

136-
---Hydrate all implementations barring those that were called during hydrate_pre
138+
---Re-Hydrate api functions and classes post-setup
137139
---@param api table not properly typed to prevent LSP from referencing implementations
138-
local function hydrate_post(api)
140+
function M.hydrate(api)
139141
api.tree.open = actions.tree.open.fn
140142
api.tree.focus = api.tree.open
141143

@@ -252,16 +254,9 @@ local function hydrate_post(api)
252254
api.marks.navigate.select = wrap_explorer_member("marks", "navigate_select")
253255

254256
api.map.keymap.current = keymap.get_keymap
255-
end
256-
257-
---#TODO 3241 hydrate function, for clarity
258-
259-
---Re-hydrate api
260-
---@param api table not properly typed to prevent LSP from referencing implementations
261-
return function(api)
262-
-- All concrete implementations
263-
hydrate_post(api)
264257

265258
-- (Re)hydrate any legacy by mapping to function set above
266-
require("nvim-tree.api.impl.legacy")(api)
259+
require("nvim-tree.api.impl.legacy").hydrate(api)
267260
end
261+
262+
return M

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

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.
1+
--Hydrates meta api empty definitions pre-setup:
2+
-- - Pre-setup functions will be hydrated with their concrete implementation.
3+
-- - Post-setup functions will notify error: "nvim-tree setup not called"
4+
-- - All classes will be hydrated with their implementations.
45
--
56
--Call it once when api is first required
67
--
@@ -15,67 +16,44 @@ local notify = require("nvim-tree.notify") -- already required by events and
1516

1617
local UserDecorator = require("nvim-tree.renderer.decorator.user")
1718

18-
---Walk the api, hydrating all functions with the error notification
19-
---@param t table api root or sub-module
19+
local M = {}
20+
21+
---Walk the api, hydrating all functions with the error notification.
22+
---Do not hydrate classes: anything with a metatable.
23+
---@param t table
2024
local function hydrate_error(t)
2125
for k, v in pairs(t) do
2226
if type(v) == "function" then
2327
t[k] = function()
2428
notify.error("nvim-tree setup not called")
2529
end
26-
elseif type(v) == "table" then
30+
elseif type(v) == "table" and not getmetatable(v) then
2731
hydrate_error(v)
2832
end
2933
end
3034
end
3135

32-
---Hydrate implementations that may be called pre setup
36+
---Hydrate api functions and classes pre-setup
3337
---@param api table not properly typed to prevent LSP from referencing implementations
34-
local function hydrate_pre(api)
35-
--
36-
-- Essential
37-
--
38+
function M.hydrate(api)
39+
-- default to the error message
40+
hydrate_error(api)
41+
42+
-- eager functions
3843
api.events.Event = events.Event
3944
api.events.subscribe = events.subscribe
40-
4145
api.map.on_attach.default = keymap.on_attach_default
42-
43-
44-
--
45-
-- May be lazily requried on execution
46-
--
47-
api.health.hi_test = function() require("nvim-tree.appearance.hi-test")() end
48-
49-
50-
--
51-
-- Already required elsewhere
52-
--
5346
api.commands.get = commands.get
54-
5547
api.map.keymap.default = keymap.get_keymap_default
5648

49+
-- lazy functions
50+
api.health.hi_test = function() require("nvim-tree.appearance.hi-test")() end
5751

58-
--
59-
-- TODO #3241
60-
--
61-
api.decorator = {}
62-
---Create a decorator class by calling :extend()
63-
---See :help nvim-tree-decorators
64-
---@type nvim_tree.api.decorator.UserDecorator
65-
api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]
66-
end
67-
68-
---#TODO 3241 hydrate function, for clarity
69-
70-
---Hydrate api
71-
---@param api table not properly typed to prevent LSP from referencing implementations
72-
return function(api)
73-
-- Default: error
74-
hydrate_error(api)
75-
76-
-- Exceptions: may be called
77-
hydrate_pre(api)
52+
-- classes
53+
api.decorator.Decorator = UserDecorator:extend()
7854

7955
-- Hydrate any legacy by mapping to function set above
80-
require("nvim-tree.api.impl.legacy")(api)
56+
require("nvim-tree.api.impl.legacy").hydrate(api)
8157
end
58+
59+
return M

lua/nvim-tree/renderer/builder.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local GitDecorator = require("nvim-tree.renderer.decorator.git")
1414
local HiddenDecorator = require("nvim-tree.renderer.decorator.hidden")
1515
local ModifiedDecorator = require("nvim-tree.renderer.decorator.modified")
1616
local OpenDecorator = require("nvim-tree.renderer.decorator.opened")
17-
local UserDecorator = require("nvim-tree.api").decorator.UserDecorator
17+
local UserDecorator = require("nvim-tree.renderer.decorator.user")
1818

1919
local pad = require("nvim-tree.renderer.components.padding")
2020

0 commit comments

Comments
 (0)