Skip to content

Commit 54c59b9

Browse files
committed
add Class
1 parent fdddb46 commit 54c59b9

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

lua/nvim-tree/class.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---Generic class, useful for inheritence.
2+
---@class (exact) Class
3+
---@field private __index? table
4+
local Class = {}
5+
6+
---@param o Class?
7+
---@return Class
8+
function Class:new(o)
9+
o = o or {}
10+
11+
setmetatable(o, self)
12+
self.__index = self
13+
14+
return o
15+
end
16+
17+
---Object is an instance of class
18+
---This will start with the lowest class and loop over all the superclasses.
19+
---@param class table
20+
---@return boolean
21+
function Class:is(class)
22+
local mt = getmetatable(self)
23+
while mt do
24+
if mt == class then
25+
return true
26+
end
27+
mt = getmetatable(mt)
28+
end
29+
return false
30+
end
31+
32+
---Return object if it is an instance of class, otherwise nil
33+
---@generic T
34+
---@param class T
35+
---@return `T`|nil
36+
function Class:as(class)
37+
return self:is(class) and self or nil
38+
end
39+
40+
return Class

lua/nvim-tree/node/init.lua

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
local git = require("nvim-tree.git")
22

3+
local Class = require("nvim-tree.class")
4+
35
---TODO #2886
46
---TODO remove all @cast
57
---TODO remove all references to directory fields:
68

79
---Abstract Node class.
810
---Uses the abstract factory pattern to instantiate child instances.
9-
---@class (exact) BaseNode
10-
---@field private __index? table
11+
---@class (exact) BaseNode: Class
1112
---@field type NODE_TYPE
1213
---@field explorer Explorer
1314
---@field absolute_path string
@@ -20,51 +21,17 @@ local git = require("nvim-tree.git")
2021
---@field watcher Watcher?
2122
---@field diag_status DiagStatus?
2223
---@field is_dot boolean cached is_dotfile
23-
local BaseNode = {}
24+
local BaseNode = Class:new()
2425

2526
---@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode
2627

27-
---@param o BaseNode?
28-
---@return BaseNode
29-
function BaseNode:new(o)
30-
o = o or {}
31-
32-
setmetatable(o, self)
33-
self.__index = self
34-
35-
return o
36-
end
37-
3828
function BaseNode:destroy()
3929
if self.watcher then
4030
self.watcher:destroy()
4131
self.watcher = nil
4232
end
4333
end
4434

45-
---Object is an instance of class
46-
---This will start with the lowest class and loop over all the superclasses.
47-
---@param class table
48-
---@return boolean
49-
function BaseNode:is(class)
50-
local mt = getmetatable(self)
51-
while mt do
52-
if mt == class then
53-
return true
54-
end
55-
mt = getmetatable(mt)
56-
end
57-
return false
58-
end
59-
60-
---Return object if it is an instance of class, otherwise nil
61-
---@generic T
62-
---@param class T
63-
---@return `T`|nil
64-
function BaseNode:as(class)
65-
return self:is(class) and self or nil
66-
end
67-
6835
---@return boolean
6936
function BaseNode:has_one_child_folder()
7037
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1+
local Class = require("nvim-tree.class")
2+
13
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
24
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
35

46
---Abstract Decorator
57
---Uses the factory pattern to instantiate child instances.
6-
---@class (exact) Decorator
7-
---@field private __index? table
8+
---@class (exact) Decorator: Class
89
---@field protected explorer Explorer
910
---@field protected enabled boolean
1011
---@field protected hl_pos HL_POSITION
1112
---@field protected icon_placement ICON_PLACEMENT
12-
local Decorator = {}
13-
14-
---@param o Decorator|nil
15-
---@return Decorator
16-
function Decorator:new(o)
17-
o = o or {}
18-
19-
setmetatable(o, self)
20-
self.__index = self
21-
22-
return o
23-
end
13+
local Decorator = Class:new()
2414

2515
---Maybe highlight groups
2616
---@param node Node

0 commit comments

Comments
 (0)