FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffview.nvim/lua/diffview/diff.lua at main · lexmiin/diffview.nvim · GitHub
lexmiin
/
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
/
diff.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
225 lines (191 loc) · 4.89 KB
Breadcrumbs
diffview.nvim
/
lua
/
diffview
/
diff.lua
Copy path
File metadata and controls
225 lines (191 loc) · 4.89 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
--[[
An implementation of Myers' diff algorithm
Derived from: https://github.com/Swatinem/diff
]]
local
oop
=
require
(
"
diffview.oop
"
)
local
M
=
{}
---
@enum
EditToken
local
EditToken
=
oop
.
enum
({
NOOP
=
1
,
DELETE
=
2
,
INSERT
=
3
,
REPLACE
=
4
,
})
---
@class
Diff
:
diffview.Object
---
@operator
call
:
Diff
---
@field
a
any[]
---
@field
b
any[]
---
@field
moda
boolean[]
---
@field
modb
boolean[]
---
@field
up
table<integer
,
integer>
---
@field
down
table<integer
,
integer>
---
@field
eql_fn
function
local
Diff
=
oop
.
create_class
(
"
Diff
"
)
---
Diff constructor.
---
@param
a
any[]
---
@param
b
any[]
---
@param
eql_fn
function
|
nil
function
Diff
:
init
(
a
,
b
,
eql_fn
)
self
.
a
=
a
self
.
b
=
b
self
.
moda
=
{}
self
.
modb
=
{}
self
.
up
=
{}
self
.
down
=
{}
self
.
eql_fn
=
eql_fn
or
function
(
aa
,
bb
)
return
aa
==
bb
end
for
i
=
1
,
#
a
do
self
.
moda
[
i
]
=
false
end
for
i
=
1
,
#
b
do
self
.
modb
[
i
]
=
false
end
self
:
lcs
(
1
,
#
self
.
a
+
1
,
1
,
#
self
.
b
+
1
)
end
---
@return
EditToken[]
function
Diff
:
create_edit_script
()
local
astart
=
1
local
bstart
=
1
local
aend
=
#
self
.
moda
local
bend
=
#
self
.
modb
local
script
=
{}
while
astart
<=
aend
or
bstart
<=
bend
do
if
astart
<=
aend
and
bstart
<=
bend
then
if
not
self
.
moda
[
astart
]
and
not
self
.
modb
[
bstart
]
then
table.insert
(
script
,
EditToken
.
NOOP
)
astart
=
astart
+
1
bstart
=
bstart
+
1
goto
continue
elseif
self
.
moda
[
astart
]
and
self
.
modb
[
bstart
]
then
table.insert
(
script
,
EditToken
.
REPLACE
)
astart
=
astart
+
1
bstart
=
bstart
+
1
goto
continue
end
end
if
astart
<=
aend
and
(
bstart
>
bend
or
self
.
moda
[
astart
])
then
table.insert
(
script
,
EditToken
.
DELETE
)
astart
=
astart
+
1
end
if
bstart
<=
bend
and
(
astart
>
aend
or
self
.
modb
[
bstart
])
then
table.insert
(
script
,
EditToken
.
INSERT
)
bstart
=
bstart
+
1
end
::
continue
::
end
return
script
end
---
@private
---
@param
astart
integer
---
@param
aend
integer
---
@param
bstart
integer
---
@param
bend
integer
function
Diff
:
lcs
(
astart
,
aend
,
bstart
,
bend
)
--
separate common head
while
astart
<
aend
and
bstart
<
bend
and
self
.
eql_fn
(
self
.
a
[
astart
],
self
.
b
[
bstart
])
do
astart
=
astart
+
1
bstart
=
bstart
+
1
end
--
separate common tail
while
astart
<
aend
and
bstart
<
bend
and
self
.
eql_fn
(
self
.
a
[
aend
-
1
],
self
.
b
[
bend
-
1
])
do
aend
=
aend
-
1
bend
=
bend
-
1
end
if
astart
==
aend
then
--
only insertions
while
bstart
<
bend
do
self
.
modb
[
bstart
]
=
true
bstart
=
bstart
+
1
end
elseif
bend
==
bstart
then
--
only deletions
while
astart
<
aend
do
self
.
moda
[
astart
]
=
true
astart
=
astart
+
1
end
else
local
snake
=
self
:
snake
(
astart
,
aend
,
bstart
,
bend
)
self
:
lcs
(
astart
,
snake
.
x
,
bstart
,
snake
.
y
)
self
:
lcs
(
snake
.
u
,
aend
,
snake
.
v
,
bend
)
end
end
---
@class
Diff.Snake
---
@field
x
integer
---
@field
y
integer
---
@field
u
integer
---
@field
v
integer
---
@private
---
@param
astart
integer
---
@param
aend
integer
---
@param
bstart
integer
---
@param
bend
integer
---
@return
Diff.Snake
function
Diff
:
snake
(
astart
,
aend
,
bstart
,
bend
)
local
N
=
aend
-
astart
local
MM
=
bend
-
bstart
local
kdown
=
astart
-
bstart
local
kup
=
aend
-
bend
local
delta
=
N
-
MM
local
deltaOdd
=
delta
%
2
~=
0
self
.
down
[
kdown
+
1
]
=
astart
self
.
up
[
kup
-
1
]
=
aend
local
Dmax
=
(
N
+
MM
)
/
2
+
1
for
D
=
0
,
Dmax
do
local
x
,
y
--
Forward path
for
k
=
kdown
-
D
,
kdown
+
D
,
2
do
if
k
==
kdown
-
D
then
x
=
self
.
down
[
k
+
1
]
--
down
else
x
=
self
.
down
[
k
-
1
]
+
1
--
right
if
k
<
kdown
+
D
and
self
.
down
[
k
+
1
]
>=
x
then
x
=
self
.
down
[
k
+
1
]
--
down
end
end
y
=
x
-
k
while
x
<
aend
and
y
<
bend
and
self
.
eql_fn
(
self
.
a
[
x
],
self
.
b
[
y
])
do
x
=
x
+
1
y
=
y
+
1
--
diagonal
end
self
.
down
[
k
]
=
x
if
deltaOdd
and
kup
-
D
<
k
and
k
<
kup
+
D
and
self
.
up
[
k
]
<=
self
.
down
[
k
]
then
return
{
x
=
self
.
down
[
k
],
y
=
self
.
down
[
k
]
-
k
,
u
=
self
.
up
[
k
],
v
=
self
.
up
[
k
]
-
k
,
}
end
end
--
Reverse path
for
k
=
kup
-
D
,
kup
+
D
,
2
do
if
k
==
kup
+
D
then
x
=
self
.
up
[
k
-
1
]
--
up
else
x
=
self
.
up
[
k
+
1
]
-
1
--
left
if
k
>
kup
-
D
and
self
.
up
[
k
-
1
]
<
x
then
x
=
self
.
up
[
k
-
1
]
--
up
end
end
y
=
x
-
k
while
x
>
astart
and
y
>
bstart
and
self
.
eql_fn
(
self
.
a
[
x
-
1
],
self
.
b
[
y
-
1
])
do
x
=
x
-
1
y
=
y
-
1
--
diagonal
end
self
.
up
[
k
]
=
x
if
not
deltaOdd
and
kdown
-
D
<=
k
and
k
<=
kdown
+
D
and
self
.
up
[
k
]
<=
self
.
down
[
k
]
then
return
{
x
=
self
.
down
[
k
],
y
=
self
.
down
[
k
]
-
k
,
u
=
self
.
up
[
k
],
v
=
self
.
up
[
k
]
-
k
,
}
end
end
end
error
(
"
Unexpected state!
"
)
end
M
.
EditToken
=
EditToken
M
.
Diff
=
Diff
return
M
Back
|
FazBrowse Home
|
New Git URL