Skip to content

Commit 359398d

Browse files
committed
add mixins to classic
1 parent 3501514 commit 359398d

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

lua/nvim-tree/classic.lua

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
-- This module is free software; you can redistribute it and/or modify it under
77
-- the terms of the MIT license. See LICENSE for details.
88
--
9+
-- https://github.com/rxi/classic
10+
--
911

10-
12+
---@class (exact) Object
13+
---@field super Object
14+
---@field private implements table<Object, boolean>
1115
local Object = {}
12-
Object.__index = Object
13-
16+
Object.__index = Object ---@diagnostic disable-line: inject-field
1417

15-
function Object:new()
18+
---Default constructor
19+
function Object:new(...)
1620
end
1721

18-
22+
---Extend a class T
23+
---super will be set to T
24+
---@generic T
25+
---@param self T
26+
---@return T
1927
function Object:extend()
2028
local cls = {}
2129
for k, v in pairs(self) do
@@ -29,49 +37,61 @@ function Object:extend()
2937
return cls
3038
end
3139

32-
33-
function Object:implement(...)
34-
for _, cls in pairs({...}) do
35-
for k, v in pairs(cls) do
36-
if self[k] == nil and type(v) == "function" then
37-
self[k] = v
38-
end
40+
---Implement the functions of a mixin
41+
---Add the mixin to the implements table
42+
---@param class Object
43+
function Object:implement(class)
44+
if not rawget(self, "implements") then
45+
rawset(self, "implements", {})
46+
end
47+
self.implements[class] = true
48+
for k, v in pairs(class) do
49+
if self[k] == nil and type(v) == "function" then
50+
self[k] = v
3951
end
4052
end
4153
end
4254

43-
44-
function Object:is(T)
55+
---Object is an instance of class or implements a mixin
56+
---@generic T
57+
---@param class T
58+
---@return boolean
59+
function Object:is(class)
4560
local mt = getmetatable(self)
4661
while mt do
47-
if mt == T then
62+
if mt == class then
63+
return true
64+
end
65+
if mt.implements and mt.implements[class] then
4866
return true
4967
end
5068
mt = getmetatable(mt)
5169
end
5270
return false
5371
end
5472

55-
56-
---Return object if it is an instance of class, otherwise nil
73+
---Return object if :is otherwise nil
5774
---@generic T
58-
---@param cls T
75+
---@param class T
5976
---@return T|nil
60-
function Object:as(cls)
61-
return self:is(cls) and self or nil
77+
function Object:as(class)
78+
return self:is(class) and self or nil
6279
end
6380

64-
65-
function Object:__tostring()
66-
return "Object"
67-
end
68-
69-
81+
---Constructor that invokes :new on a new instance
82+
---@generic T
83+
---@param self T
84+
---@param ... any
85+
---@return T
7086
function Object:__call(...)
7187
local obj = setmetatable({}, self)
7288
obj:new(...)
7389
return obj
7490
end
7591

92+
-- avoid unused param warnings in abstract methods
93+
---@param ... any
94+
function Object:nop(...) --luacheck: ignore 212
95+
end
7696

7797
return Object

lua/nvim-tree/node/directory-link.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ local git_utils = require("nvim-tree.git.utils")
22
local utils = require("nvim-tree.utils")
33

44
local DirectoryNode = require("nvim-tree.node.directory")
5+
local LinkNode = require("nvim-tree.node.link")
56

67
---@class (exact) DirectoryLinkNode: DirectoryNode, LinkNode
78
local DirectoryLinkNode = DirectoryNode:extend()
9+
DirectoryLinkNode:implement(LinkNode)
810

911
---@param explorer Explorer
1012
---@param parent DirectoryNode

lua/nvim-tree/node/file-link.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ local git_utils = require("nvim-tree.git.utils")
22
local utils = require("nvim-tree.utils")
33

44
local FileNode = require("nvim-tree.node.file")
5+
local LinkNode = require("nvim-tree.node.link")
56

67
---@class (exact) FileLinkNode: FileNode, LinkNode
78
local FileLinkNode = FileNode:extend()
9+
FileLinkNode:implement(LinkNode)
810

911
---@param explorer Explorer
1012
---@param parent DirectoryNode

0 commit comments

Comments
 (0)