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