FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffview.nvim/lua/diffview/oop.lua at main · vramana/diffview.nvim · GitHub
vramana
/
diffview.nvim
Public
forked from
sindrets/diffview.nvim
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
diffview.nvim
/
lua
/
diffview
/
oop.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
227 lines (186 loc) · 4.59 KB
Breadcrumbs
diffview.nvim
/
lua
/
diffview
/
oop.lua
Copy path
File metadata and controls
227 lines (186 loc) · 4.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
local
lazy
=
require
(
"
diffview.lazy
"
)
local
utils
=
lazy
.
require
(
"
diffview.utils
"
)
---
@module
"
diffview.utils
"
local
fmt
=
string.format
local
M
=
{}
function
M
.
abstract_stub
()
error
(
"
Unimplemented abstract method!
"
)
end
---
@generic
T
---
@param
t
T
---
@return
T
function
M
.
enum
(
t
)
utils
.
add_reverse_lookup
(
t
)
return
t
end
---
Wrap metatable methods to ensure they're called with the instance as `self`.
---
@param
func
function
---
@param
instance
table
---
@return
function
local
function
wrap_mt_func
(
func
,
instance
)
return
function
(
_
,
k
)
return
func
(
instance
,
k
)
end
end
local
mt_func_names
=
{
"
__index
"
,
"
__tostring
"
,
"
__eq
"
,
"
__add
"
,
"
__sub
"
,
"
__mul
"
,
"
__div
"
,
"
__mod
"
,
"
__pow
"
,
"
__unm
"
,
"
__len
"
,
"
__lt
"
,
"
__le
"
,
"
__concat
"
,
"
__newindex
"
,
"
__call
"
,
}
local
function
new_instance
(
class
, ...)
local
inst
=
{
class
=
class
}
local
mt
=
{
__index
=
class
}
for
_
,
mt_name
in
ipairs
(
mt_func_names
)
do
local
class_mt_func
=
class
[
mt_name
]
if
type
(
class_mt_func
)
==
"
function
"
then
mt
[
mt_name
]
=
wrap_mt_func
(
class_mt_func
,
inst
)
elseif
class_mt_func
~=
nil
then
mt
[
mt_name
]
=
class_mt_func
end
end
local
self
=
setmetatable
(
inst
,
mt
)
self
:
init
(
...
)
return
self
end
local
function
tostring
(
class
)
return
fmt
(
"
<class %s>
"
,
class
.
__name
)
end
---
@generic
T
:
diffview.Object
---
@generic
U
:
diffview.Object
---
@param
name
string
---
@param
super_class
?
T
---
@return
U new_class
function
M
.
create_class
(
name
,
super_class
)
super_class
=
super_class
or
M
.
Object
return
setmetatable
(
{
__name
=
name
,
super_class
=
super_class
,
},
{
__index
=
super_class
,
__call
=
new_instance
,
__tostring
=
tostring
,
}
)
end
local
function
classm_safeguard
(
x
)
assert
(
x
.
class
==
nil
,
"
Class method should not be invoked from an instance!
"
)
end
local
function
instancem_safeguard
(
x
)
assert
(
type
(
x
.
class
)
==
"
table
"
,
"
Instance method must be called from a class instance!
"
)
end
---
@class
diffview.Object
---
@field
protected
__name string
---
@field
private
__init_caller
?
table
---
@field
class
table
|
diffview.Object
---
@field
super_class
table
|
diffview.Object
local
Object
=
M
.
create_class
(
"
Object
"
)
M
.
Object
=
Object
function
Object
:
__tostring
()
return
fmt
(
"
<a %s>
"
,
self
.
class
.
__name
)
end
--
### CLASS METHODS ###
---
@return
string
function
Object
:
name
()
classm_safeguard
(
self
)
return
self
.
__name
end
---
Check if this class is an ancestor of the given instance. `A` is an ancestor
---
of `b` if - and only if - `b` is an instance of a subclass of `A`.
---
@param
other
any
---
@return
boolean
function
Object
:
ancestorof
(
other
)
classm_safeguard
(
self
)
if
not
M
.
is_instance
(
other
)
then
return
false
end
return
other
:
instanceof
(
self
)
end
---
@return
string
function
Object
:
classpath
()
classm_safeguard
(
self
)
local
ret
=
self
.
__name
local
cur
=
self
.
super_class
while
cur
do
ret
=
cur
.
__name
..
"
.
"
..
ret
cur
=
cur
.
super_class
end
return
ret
end
--
### INSTANCE METHODS ###
---
Call constructor.
function
Object
:
init
(...)
end
---
Call super constructor.
---
@param
...
any
function
Object
:
super
(...)
instancem_safeguard
(
self
)
local
next_super
--
Keep track of what class is currently calling the constructor such that we
--
can avoid loops.
if
self
.
__init_caller
then
next_super
=
self
.
__init_caller
.
super_class
else
next_super
=
self
.
super_class
end
if
not
next_super
then
return
end
self
.
__init_caller
=
next_super
next_super
.
init
(
self
,
...
)
self
.
__init_caller
=
nil
end
---
@param
other
diffview.Object
---
@return
boolean
function
Object
:
instanceof
(
other
)
instancem_safeguard
(
self
)
local
cur
=
self
.
class
while
cur
do
if
cur
==
other
then
return
true
end
cur
=
cur
.
super_class
end
return
false
end
---
@param
x
any
---
@return
boolean
function
M
.
is_class
(
x
)
if
type
(
x
)
~=
"
table
"
then
return
false
end
return
type
(
rawget
(
x
,
"
__name
"
))
==
"
string
"
and
x
.
instanceof
==
Object
.
instanceof
end
---
@param
x
any
---
@return
boolean
function
M
.
is_instance
(
x
)
if
type
(
x
)
~=
"
table
"
then
return
false
end
return
M
.
is_class
(
x
.
class
)
end
---
@class
Symbol
---
@operator
call
:
Symbol
---
@field
public
name
?
string
---
@field
public
id integer
---
@field
private
_id_counter integer
local
Symbol
=
M
.
create_class
(
"
Symbol
"
)
M
.
Symbol
=
Symbol
---
@private
Symbol
.
_id_counter
=
1
---
@param
name
?
string
function
Symbol
:
init
(
name
)
self
.
name
=
name
self
.
id
=
Symbol
.
_id_counter
Symbol
.
_id_counter
=
Symbol
.
_id_counter
+
1
end
function
Symbol
:
__tostring
()
if
self
.
name
then
return
fmt
(
"
<Symbol('%s)>
"
,
self
.
name
)
else
return
fmt
(
"
<Symbol(#%d)>
"
,
self
.
id
)
end
end
return
M
Back
|
FazBrowse Home
|
New Git URL