From bc17b1a4318ac5de88c2024eb48252a5da14799c Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 17 Mar 2023 16:03:41 +0100 Subject: Move countLines to hunk as well. --- ftplugin/diff_navigator.lua | 29 ++++------------------------- ftplugin/hunk.lua | 24 ++++++++++++++++++++++++ ftplugin/hunk_spec.lua | 8 ++++++++ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/ftplugin/diff_navigator.lua b/ftplugin/diff_navigator.lua index f2b5e0d..c36cfab 100644 --- a/ftplugin/diff_navigator.lua +++ b/ftplugin/diff_navigator.lua @@ -96,27 +96,6 @@ function getCurrentHunkHeader() return hnk.Header.parse(vim.fn.getline(lineno), lineno) 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 countLines(start, endline) - local context_lines = 0 - local old_lines = 0 - local new_lines = 0 - - for line in pairs(vim.fn.getline(start, endline)) do - local first_char = vim.fn.strpart(line, 0, 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 -- ------------------------------------------------------------- @@ -189,8 +168,8 @@ function DiffSplitHunk() -- -- -- Start line below header and stop one line above the current line - local diff_lines = countLines(old_cur_header['line'] + 1, - cur_line_no - 1) + local diff_lines = hnk.countLines(old_cur_header['line'] + 1, + cur_line_no - 1, vim.fn.getline) local diff_old = diff_lines[1] local diff_new = diff_lines[2] @@ -251,8 +230,8 @@ function DiffDeleteHunk() vim.cmd("normal " .. start_hunk_line .. ",$" .. "d") -- we are in the middle of the file ... it's a bit more complicated else - local count_lines = countLines(start_hunk_line + 1, - next_hunk_line - 1) + local count_lines = hnk.countLines(start_hunk_line + 1, + next_hunk_line - 1, vim.fn.getline) local added_lines = count_lines[2] - count_lines[1] vim.fn.setpos(".", {0, next_hunk_line, 1, 0}) vim.cmd("normal " .. start_hunk_line .. "," .. (next_hunk_line - 1) .. "d") diff --git a/ftplugin/hunk.lua b/ftplugin/hunk.lua index c01e0a9..db8ac06 100644 --- a/ftplugin/hunk.lua +++ b/ftplugin/hunk.lua @@ -26,9 +26,33 @@ function M.Header:parse(inline, lineNo) 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 diff --git a/ftplugin/hunk_spec.lua b/ftplugin/hunk_spec.lua index c640afb..13d14f3 100644 --- a/ftplugin/hunk_spec.lua +++ b/ftplugin/hunk_spec.lua @@ -38,9 +38,17 @@ describe("Class functions", function() hk = require("hunk") end) +-- TODO some protection of hk.createHunkHeader() against bad +-- TODO parameters (creating impossible regions of lines) return +-- TODO empty string in such case it("generates correct header", function() local header_str = hk.createHunkHeader(12,15,25,30,"something") local exp_str = "@@ -12,15 +25,30 @@ something" assert.are.equal(exp_str, header_str) end) + + it("counts lines in chunk correctly", function() + local header_str = hk.countLines(12,15, function(beg, fin) return {} end) + pending("This test is pending ...") + end) end) -- cgit