From 7270194a16b3f93e20bbf0d39d14860fc4ba7d32 Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Sun, 6 Sep 2026 20:51:24 +0200 Subject: [PATCH] test(inline): cover BOF and adjacent-hunk fold ranges Also minor fixes to comments. --- doc/diffview.txt | 4 +- lua/diffview/session.lua | 4 +- .../tests/functional/inline_diff_spec.lua | 49 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/doc/diffview.txt b/doc/diffview.txt index 9c56044c..fe86b860 100644 --- a/doc/diffview.txt +++ b/doc/diffview.txt @@ -1182,7 +1182,9 @@ view.inline *diffview-config-view.inline* context lines follows `context:N` in |'diffopt'|, defaulting to 6 when omitted. Pure-deletion anchors remain unfolded so their deleted virtual lines stay visible. The initial fold - level is controlled by |diffview-config-view.foldlevel|. + level is controlled by |diffview-config-view.foldlevel|; + setting it high enough to keep every fold open (e.g., `99`) + makes this option a no-op. view.file_history.pin_local *diffview-config-view.file_history.pin_local* Type: `boolean`, Default: `false` diff --git a/lua/diffview/session.lua b/lua/diffview/session.lua index 759d0653..0d0c695d 100644 --- a/lua/diffview/session.lua +++ b/lua/diffview/session.lua @@ -415,8 +415,8 @@ local function warn_failed(err) end ---Turn a raw sidecar `cursor_map` into `StandardView.CarryState`s, or nil if ----nothing usable remains. Buffer handles do not survive. This nvim run may ----have handed the same number to an unrelated file. +---nothing usable remains. Accepts entries as either a bare `winsaveview` dict +---or a `{ winview = ... }` wrapper. ---@param raw any ---@return table? local function sanitize_cursor_map(raw) diff --git a/lua/diffview/tests/functional/inline_diff_spec.lua b/lua/diffview/tests/functional/inline_diff_spec.lua index 99d6e12b..ced8082e 100644 --- a/lua/diffview/tests/functional/inline_diff_spec.lua +++ b/lua/diffview/tests/functional/inline_diff_spec.lua @@ -1200,6 +1200,55 @@ describe("diffview.scene.inline_diff", function() local deleted = virt_line_counts(extmarks(bufnr)) assert.are.same({ { row = 8, count = 1, above = false } }, deleted) end) + + it("keeps line 1 unfolded when the deletion is at the start of file", function() + local old = {} + for i = 1, 20 do + old[i] = "line " .. i + end + + local new = vim.deepcopy(old) + table.remove(new, 1) + local bufnr = fresh_buf(new) + inline_diff.render(bufnr, old, new) + inline_diff.update_fold_ranges(bufnr, 2) + + api.nvim_buf_call(bufnr, function() + for lnum = 1, 3 do + assert.equals(0, inline_diff.foldexpr(lnum)) + end + assert.equals(1, inline_diff.foldexpr(4)) + end) + + local deleted = virt_line_counts(extmarks(bufnr)) + assert.are.same({ { row = 0, count = 1, above = true } }, deleted) + end) + + it("merges overlapping context ranges of adjacent hunks", function() + local old = {} + for i = 1, 15 do + old[i] = "line " .. i + end + + local new = vim.deepcopy(old) + new[5] = "changed 5" + new[10] = "changed 10" + local bufnr = fresh_buf(new) + inline_diff.render(bufnr, old, new) + inline_diff.update_fold_ranges(bufnr, 2) + + api.nvim_buf_call(bufnr, function() + for lnum = 1, 2 do + assert.equals(1, inline_diff.foldexpr(lnum)) + end + for lnum = 3, 12 do + assert.equals(0, inline_diff.foldexpr(lnum)) + end + for lnum = 13, 15 do + assert.equals(1, inline_diff.foldexpr(lnum)) + end + end) + end) end) describe("helpers", function()