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
|
-- TODO: show current hunk in status line
-- TODO: incorporate more patchutils functionality
-- FIXME https://www.reddit.com/r/neovim/comments/unwvkw/github_hkuptyrunesnvim_lua_test_framework_for/
-- FIXME https://github.com/hkupty/runes.nvim
pretty = require("pl.pretty")
local M = {}
-- Header object
M.Header = {}
function M.Header:parse(lineNo)
local inline
local pat = "@@%s+%-(%d+),(%d+)%s+%+(%d+),(%d+)%s+@@%s*(.*)"
if (lineNo == "." or type(lineNo) == "number") then
-- to convert lineNo into proper number, if it was "." originally
inline, lineNo = M.getline(lineNo)
else
inline = lineNo
lineNo = 0
end
local of, oc, nf, nc, rem = inline:match(pat)
return {
line = lineNo,
oldFirst = tonumber(of),
oldCount = tonumber(oc),
newFirst = tonumber(nf),
newCount = tonumber(nc),
remainderLine = rem
}
end
-- Generate hunk header line
-- TODO some protection against bad parameters (creating impossible regions of lines)
-- TODO return empty string in such case
function M.createHunkHeader(oldStart, oldLen, newStart, newLen, remaind)
return "@@ -" .. oldStart .. "," .. oldLen ..
" +" .. newStart .. "," .. newLen .. " @@ " .. remaind
end
-- Compatibility shim for getline()
function M.getline(where, endWhere)
if vis ~= nil then
local win = vis.win
local line = where
if where == "." then
line = win.selection.line
end
if endWhere == nil then
return win.file.lines[line], line
end
-- now endWhere ~= nil and we need to return an array
local out = {}
for i=line, endWhere do
table.insert(out, win.file.lines[i])
end
return out, line
else
return vim.fn.getline(where), vim.fn.line(where)
end
end
-- Return number of lines in the range between start and end line (both
-- inclusive) which are from the state before patch and the one after
-- patch is applied.
function M.countLines(start, endline)
local context_lines = 0
local old_lines = 0
local new_lines = 0
for line, _ in pairs(M.getline(start, endline)) do
local first_char = line:sub(1, 1)
if first_char == ' ' then
local context_lines = context_lines + 1
elseif first_char == '-' then
local old_lines = old_lines + 1
elseif first_char == '+' then
local new_lines = new_lines + 1
end
end
return {context_lines + old_lines, context_lines + new_lines}
end
return M
|