aboutsummaryrefslogblamecommitdiffstats
path: root/ftplugin/hunk.lua
blob: db8ac06a7fe062b571028ae672474d41aee54f1c (plain) (tree)



























                                                                                                       

                                                                                     




                                                                        





















                                                                       
        
-- 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