Skip to content

Commit 792e590

Browse files
committed
Check if context lines between two hunks roll between each other.
1 parent 4f4b9d2 commit 792e590

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

demo/xtra/DIFF.LUA

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env lua
22

3+
CONTEXT_LINES = 3
34
local function diff_u(fn1, fn2)
45
local function open(fn) local f, e = io.open(fn) if not f then print(e) os.exit(1) end return f end
56
local f1, f2, pre, add, del, p1, p2, x, y, h, l1, l2 = open(fn1), open(fn2), {}, {}, {}, 0, 0, 0, 0
@@ -50,31 +51,33 @@ local function diff_u(fn1, fn2)
5051
end
5152
local function diagonal()
5253
flush_hunk()
53-
if #pre >= 3 then table.remove(pre, 1) end
54+
if #pre > CONTEXT_LINES then table.remove(pre, 1) end
5455
if p1 > 0 or p2 > 0 then
5556
if p1 > 0 and p2 > 0 then
56-
if p1 > 1 or p2 > 1 then table.insert(buf.line, " " .. l2) end
57+
if p1 > CONTEXT_LINES or p2 > CONTEXT_LINES then table.insert(buf.line, " " .. l2) end
5758
p1, p2 = p1 - 1, p2 - 1
5859
elseif p1 > 0 then
59-
if p1 > 1 then table.insert(buf.line, " " .. l1) end
60+
if p1 > CONTEXT_LINES then table.insert(buf.line, " " .. l1) end
6061
p1 = p1 - 1
6162
elseif p2 > 0 then
62-
if p2 > 1 then table.insert(buf.line, " " .. l2) end
63+
if p2 > CONTEXT_LINES then table.insert(buf.line, " " .. l2) end
6364
p2 = p2 - 1
64-
end
65+
elseif p2 <= CONTEXT_LINES then table.insert(pre, l2)
66+
elseif p1 <= CONTEXT_LINES then table.insert(pre, l1) end
67+
6568
if p1 == 0 or p2 == 0 then flush_buf() table.insert(pre, l2) end
6669
else
6770
table.insert(pre, l2)
6871
end
6972
x, y, l1, l2 = x + 1, y + 1, f1:read("*l"), f2:read("*l")
7073
end
7174
local function right()
72-
flush_pre() p1 = 4
75+
flush_pre() p1 = (CONTEXT_LINES * 2) + 1
7376
table.insert(del, l1)
7477
x, l1 = x + 1, f1:read("*l")
7578
end
7679
local function down()
77-
flush_pre() p2 = 4
80+
flush_pre() p2 = (CONTEXT_LINES * 2) + 1
7881
table.insert(add, l2)
7982
y, l2 = y + 1, f2:read("*l")
8083
end

0 commit comments

Comments
 (0)