aboutsummaryrefslogtreecommitdiffstats
path: root/ftplugin/hunk.lua
blob: db8ac06a7fe062b571028ae672474d41aee54f1c (plain) (blame)
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
-- 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(inline, lineNo)
    local pat = "@@%s+%-(%d+),(%d+)%s+%+(%d+),(%d+)%s+@@%s*(.*)"
    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

-- 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, getlfn)
    local context_lines = 0
    local old_lines = 0
    local new_lines = 0

    for line in pairs(getlfn(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