Skip to content

Commit 919a0a3

Browse files
committed
feat(#2948): extract _meta following nvim pattern
1 parent 45a14f6 commit 919a0a3

File tree

7 files changed

+84
-44
lines changed

7 files changed

+84
-44
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ check: luals
1313
# subtasks
1414
#
1515
luacheck:
16-
luacheck -q lua
16+
luacheck --codes --quiet lua --exclude-files "**/_meta/**"
1717

1818
# --diagnosis-as-error does not function for workspace, hence we post-process the output
1919
style-check:

lua/nvim-tree/_meta/aliases.lua

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---@meta
2+
error("Cannot require a meta file")
3+
4+
--
5+
--Internal convenience aliases for api types
6+
--
7+
8+
--
9+
--api.lua
10+
--
11+
12+
---@alias HighlightedString nvim_tree.api.HighlightedString
13+
14+
--
15+
--api_decorator.lua
16+
--
17+
18+
---@alias DecoratorHighlightRange nvim_tree.api.decorator.HighlightRange
19+
---@alias DecoratorIconPlacement nvim_tree.api.decorator.IconPlacement

lua/nvim-tree/_meta/api.lua

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
---@meta
2-
error('Cannot require a meta file')
3-
4-
-- TODO describe class
5-
-- TODO describe user decorator
2+
error("Cannot require a meta file")
63

74
--
85
-- Nodes
96
--
107

118
---Base Node, Abstract
12-
---@class (exact) nvim_tree.api.Node: Class
9+
---@class (exact) nvim_tree.api.Node
1310
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
1411
---@field absolute_path string
1512
---@field executable boolean
@@ -34,7 +31,7 @@ error('Cannot require a meta file')
3431
---@class (exact) nvim_tree.api.RootNode: nvim_tree.api.DirectoryNode
3532

3633
---Link mixin
37-
---@class (exact) nvim_tree.api.LinkNode: Class
34+
---@class (exact) nvim_tree.api.LinkNode
3835
---@field link_to string
3936
---@field fs_stat_target uv.fs_stat.result
4037

@@ -52,9 +49,3 @@ error('Cannot require a meta file')
5249
---@class (exact) nvim_tree.api.HighlightedString
5350
---@field str string
5451
---@field hl string[]
55-
56-
--
57-
-- Internal Aliases
58-
--
59-
---@alias HighlightedString nvim_tree.api.HighlightedString
60-

lua/nvim-tree/_meta/api_decorator.lua

+58-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,88 @@
11
---@meta
22
error('Cannot require a meta file')
33

4+
local nvim_tree = { api = { decorator = { BaseDecorator = {} } } }
5+
46
---Highlight group range as per nvim-tree.renderer.highlight_*
57
---@alias nvim_tree.api.decorator.HighlightRange "none" | "icon" | "name" | "all"
68

79
---Icon position as per renderer.icons.*_placement
810
---@alias nvim_tree.api.decorator.IconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"
911

10-
---UserDecorator Constructor Arguments
11-
---@class (exact) nvim_tree.api.decorator.UserDecoratorArgs
12+
--
13+
-- BaseDecorator Class, see example implementation below
14+
--
15+
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
25+
---@class (exact) nvim_tree.api.decorator.BaseDecorator
26+
---@field protected enabled boolean
27+
---@field protected highlight_range nvim_tree.api.decorator.HighlightRange
28+
---@field protected icon_placement nvim_tree.api.decorator.IconPlacement
29+
30+
---Constructor Arguments
31+
---@class (exact) nvim_tree.api.decorator.BaseDecoratorArgs
1232
---@field enabled boolean
1333
---@field highlight_range nvim_tree.api.decorator.HighlightRange
1434
---@field icon_placement nvim_tree.api.decorator.IconPlacement
1535

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)
41+
---@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
45+
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
50+
---@param node nvim_tree.api.Node
51+
---@return HighlightedString? icon_node
52+
function nvim_tree.api.decorator.BaseDecorator:icon_node(node) end
53+
54+
---Implement this method to provide icons and the highlight groups to apply to IconPlacement
55+
---@param node nvim_tree.api.Node
56+
---@return HighlightedString[]? icons
57+
function nvim_tree.api.decorator.BaseDecorator:icons(node) end
58+
59+
---Implement this method to provide one highlight group to apply to HighlightRange
60+
---@param node nvim_tree.api.Node
61+
---@return string? highlight_group
62+
function nvim_tree.api.decorator.BaseDecorator:highlight_group(node) end
63+
1664

1765
--
18-
-- Example UserDecorator
66+
-- Example Decorator
1967
--
2068

21-
local UserDecorator = require("nvim-tree.renderer.decorator.user")
69+
local BaseDecorator = require("nvim-tree.api").decorator.BaseDecorator
2270

23-
---@class (exact) MyDecorator: UserDecorator
71+
---@class (exact) MyDecorator: nvim_tree.api.decorator.BaseDecorator
2472
---@field private my_icon nvim_tree.api.HighlightedString
25-
local MyDecorator = UserDecorator:extend()
73+
local MyDecorator = BaseDecorator:extend()
2674

27-
---Constructor
75+
---Mandatory constructor :new() will be called once per tree render, with no arguments.
2876
function MyDecorator:new()
29-
30-
---@type nvim_tree.api.decorator.UserDecoratorArgs
77+
----@type nvim_tree.api.decorator.BaseDecoratorArgs
3178
local args = {
3279
enabled = true,
3380
highlight_range = "all",
3481
icon_placement = "signcolumn",
3582
}
3683

3784
-- construct super with args
38-
MyDecorator.super.new(self, args)
85+
BaseDecorator.new(self, args)
3986

4087
-- create your icon once, for convenience
4188
self.my_icon = { str = "I", hl = { "MyIcon" } }
@@ -45,7 +92,7 @@ function MyDecorator:new()
4592
self:define_sign(self.my_icon)
4693
end
4794

48-
---Overridde node icon
95+
---Override node icon
4996
---@param node nvim_tree.api.Node
5097
---@return nvim_tree.api.HighlightedString? icon_node
5198
function MyDecorator:icon_node(node)
@@ -79,10 +126,3 @@ function MyDecorator:highlight_group(node)
79126
end
80127

81128
return MyDecorator
82-
83-
--
84-
-- Internal Aliases
85-
--
86-
---@alias DecoratorHighlightRange nvim_tree.api.decorator.HighlightRange
87-
---@alias DecoratorIconPlacement nvim_tree.api.decorator.IconPlacement
88-

lua/nvim-tree/api.lua

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ local Api = {
3939
},
4040
commands = {},
4141
diagnostics = {},
42+
decorator = {},
4243
}
4344

4445
---Print error when setup not called.
@@ -311,4 +312,6 @@ Api.commands.get = wrap(function()
311312
return require("nvim-tree.commands").get()
312313
end)
313314

315+
Api.decorator.BaseDecorator = require("nvim-tree.renderer.decorator.user") --[[@as nvim_tree.api.decorator.BaseDecorator ]]
316+
314317
return Api

lua/nvim-tree/renderer/builder.lua

-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ end
205205
---@return HighlightedString icon
206206
---@return HighlightedString name
207207
function Builder:icon_name_decorated(node)
208-
209208
-- base case
210209
local icon = node:highlighted_icon()
211210
local name = node:highlighted_name()

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

-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
local Decorator = require("nvim-tree.renderer.decorator")
22

3-
---Define a Decorator to optionally set:
4-
--- Additional icons
5-
--- Highlight group
6-
--- Node icon
7-
---Mandator constructor MyDecorator:new() will be called once per tree render, with no arguments.
8-
---Must call:
9-
--- super passing DecoratorArgs MyDecorator.super.new(self, args)
10-
--- define_sign when using "signcolumn"
11-
123
---@class (exact) UserDecorator: Decorator
134
local UserDecorator = Decorator:extend()
145

15-
---Override this method to set the node's icon
166
---@param node nvim_tree.api.Node
177
---@return HighlightedString? icon_node
188
function UserDecorator:icon_node(node)
199
return self:nop(node)
2010
end
2111

22-
---Override this method to provide icons and the highlight groups to apply to DecoratorIconPlacement
2312
---@param node nvim_tree.api.Node
2413
---@return HighlightedString[]? icons
2514
function UserDecorator:icons(node)
2615
self:nop(node)
2716
end
2817

29-
---Override this method to provide one highlight group to apply to DecoratorRange
3018
---@param node nvim_tree.api.Node
3119
---@return string? highlight_group
3220
function UserDecorator:highlight_group(node)

0 commit comments

Comments
 (0)