From 428ded2c4c0bcfc32d3270973c145bf09f874c62 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Tue, 26 May 2026 17:30:18 +0200 Subject: [PATCH] chore: apply formatting --- lua/git/core/repo.lua | 8 +- lua/git/hunks.lua | 8 +- lua/git/log_view.lua | 4 +- lua/git/status_view.lua | 9 +- lua/git/statusline.lua | 10 +- plugin/git.lua | 1 - test/git/cmd_test.lua | 3 +- test/git/hunks_test.lua | 102 +++++++++-------- test/git/object_test.lua | 34 +++--- test/git/repo_test.lua | 208 +++++++++++++++++++--------------- test/git/status_test.lua | 49 ++++---- test/git/status_view_test.lua | 149 ++++++++++++------------ 12 files changed, 305 insertions(+), 280 deletions(-) diff --git a/lua/git/core/repo.lua b/lua/git/core/repo.lua index ce65f08..48e4d44 100644 --- a/lua/git/core/repo.lua +++ b/lua/git/core/repo.lua @@ -169,8 +169,7 @@ local function read_git_config(path) section = s out[section] = out[section] or {} elseif section then - local key, value = - trimmed:match("^(%S+)%s*=%s*(.-)$") + local key, value = trimmed:match("^(%S+)%s*=%s*(.-)$") if key then out[section][key] = value end @@ -225,10 +224,7 @@ function Repo:_fetch_status() end self.status = status.parse(result.stdout or "") local change = { - paths = status.diff_entries( - prior_entries, - self.status.entries - ), + paths = status.diff_entries(prior_entries, self.status.entries), branch_changed = not vim.deep_equal( prior_branch, self.status.branch diff --git a/lua/git/hunks.lua b/lua/git/hunks.lua index 094a0af..279b4ba 100644 --- a/lua/git/hunks.lua +++ b/lua/git/hunks.lua @@ -489,8 +489,7 @@ local function recompute(buf) state.head_sha = sha end, function() - local new = - vim.api.nvim_buf_get_lines(buf, 0, -1, false) + local new = vim.api.nvim_buf_get_lines(buf, 0, -1, false) state.hunks = state.index and compute_hunks(state.index, new) or {} @@ -524,7 +523,10 @@ function M.attach(buf) if not r then return end - local rel = vim.fs.relpath(r.worktree, vim.fn.resolve(vim.api.nvim_buf_get_name(buf))) + local rel = vim.fs.relpath( + r.worktree, + vim.fn.resolve(vim.api.nvim_buf_get_name(buf)) + ) if not rel then return end diff --git a/lua/git/log_view.lua b/lua/git/log_view.lua index 99f2a35..00944ee 100644 --- a/lua/git/log_view.lua +++ b/lua/git/log_view.lua @@ -14,7 +14,9 @@ local function open_under_cursor(buf) -- Anchor past the leading graph chars (matches the leading sha column, -- not any hex word that happens to appear later in the subject). local sha = r - and vim.api.nvim_get_current_line():match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)") + and vim.api + .nvim_get_current_line() + :match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)") if not sha then return false end diff --git a/lua/git/status_view.lua b/lua/git/status_view.lua index cf951a5..64d5838 100644 --- a/lua/git/status_view.lua +++ b/lua/git/status_view.lua @@ -38,8 +38,7 @@ end ---@type table local state = {} -local group = - vim.api.nvim_create_augroup("ow.git.status_win", { clear = true }) +local group = vim.api.nvim_create_augroup("ow.git.status_win", { clear = true }) local ns = vim.api.nvim_create_namespace("ow.git.status_win") ---@return integer? win @@ -349,11 +348,7 @@ local function view_row(s, row, focus_left) local left = older_pane(s, row) local right = newer_pane(s, row) if not left and not right then - util.warning( - "no content for %s row: %s", - row.section, - row.entry.path - ) + util.warning("no content for %s row: %s", row.section, row.entry.path) return end diff --git a/lua/git/statusline.lua b/lua/git/statusline.lua index d779c53..9ab33b3 100644 --- a/lua/git/statusline.lua +++ b/lua/git/statusline.lua @@ -20,10 +20,7 @@ local function render(entry) end local parts = {} for _, mark in ipairs(marks) do - table.insert( - parts, - string.format("%%#%s#%s%%*", mark.hl, mark.char) - ) + table.insert(parts, string.format("%%#%s#%s%%*", mark.hl, mark.char)) end return table.concat(parts, " ") end @@ -73,10 +70,7 @@ repo.on("change", function(r) local rel = vim.fs.relpath(r.worktree, vim.fn.resolve(name)) if rel then set_status(buf, r, rel) - if - not any_visible - and #vim.fn.win_findbuf(buf) > 0 - then + if not any_visible and #vim.fn.win_findbuf(buf) > 0 then any_visible = true end end diff --git a/plugin/git.lua b/plugin/git.lua index e393909..0ae9dd4 100644 --- a/plugin/git.lua +++ b/plugin/git.lua @@ -336,4 +336,3 @@ end, { silent = true, desc = "Open this file at the parent of the line's commit", }) - diff --git a/test/git/cmd_test.lua b/test/git/cmd_test.lua index 4474588..46a49b5 100644 --- a/test/git/cmd_test.lua +++ b/test/git/cmd_test.lua @@ -548,8 +548,7 @@ t.test("streaming :G fetch (no bang) does not open a window", function() local before = #vim.api.nvim_tabpage_list_wins(0) cmd.run({ "fetch" }) t.wait_for(function() - return has_status(calls, "failed") - or has_status(calls, "success") + return has_status(calls, "failed") or has_status(calls, "success") end, "streaming job to terminate", 5000) t.eq(#vim.api.nvim_tabpage_list_wins(0), before, "no new window") t.falsy(find_preview_win(), "no preview window") diff --git a/test/git/hunks_test.lua b/test/git/hunks_test.lua index e3d71e8..bfb17f9 100644 --- a/test/git/hunks_test.lua +++ b/test/git/hunks_test.lua @@ -29,9 +29,11 @@ end local function sign_marks(buf) local ns = vim.api.nvim_get_namespaces()["ow.git.hunks"] local out = {} - for _, m in ipairs(vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { - details = true, - })) do + for _, m in + ipairs(vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { + details = true, + })) + do local d = assert(m[4]) table.insert(out, { row = m[2], @@ -167,7 +169,10 @@ t.test("overlay: change hunk shows deletion and addition", function() t.eq(add_d.line_hl_group, "GitHunkAddLine") virt_d = assert(virt_d, "the deletion should render as virtual lines") local piece = assert(assert(assert(virt_d.virt_lines)[1])[1]) - t.truthy(vim.startswith(piece[1], "b"), "deleted line shows the old content") + t.truthy( + vim.startswith(piece[1], "b"), + "deleted line shows the old content" + ) t.eq(piece[2], "GitHunkDeleteLine") end) @@ -197,11 +202,8 @@ t.test("overlay: add hunk highlights the added lines", function() end) t.test("overlay: deleted lines are treesitter-highlighted", function() - local _, buf = setup( - "-- a note\nlocal x = 1\nlocal y = 2\n", - "local y = 2\n", - "a.lua" - ) + local _, buf = + setup("-- a note\nlocal x = 1\nlocal y = 2\n", "local y = 2\n", "a.lua") t.truthy( pcall(vim.treesitter.start, buf, "lua"), "the lua parser should be available" @@ -437,22 +439,25 @@ t.test("toggle_stage unstages one of two adjacent staged hunks", function() ) end) -t.test("toggle_stage refreshes the gutter when status stays modified", function() - local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") - t.eq(#assert(hunks.state(buf)).hunks, 3) +t.test( + "toggle_stage refreshes the gutter when status stays modified", + function() + local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") + t.eq(#assert(hunks.state(buf)).hunks, 3) - vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.toggle_stage(buf) - t.wait_for(function() - return #assert(hunks.state(buf)).hunks == 2 - end, "gutter to drop the first staged hunk") + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + hunks.toggle_stage(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).hunks == 2 + end, "gutter to drop the first staged hunk") - vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.toggle_stage(buf) - t.wait_for(function() - return #assert(hunks.state(buf)).hunks == 1 - end, "gutter to drop the middle staged hunk") -end) + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + hunks.toggle_stage(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).hunks == 1 + end, "gutter to drop the middle staged hunk") + end +) t.test("staged hunks show with the staged highlight", function() local _, buf = setup("a\nb\nc\n", "a\nB\nc\n") @@ -499,32 +504,35 @@ t.test("toggle_stage toggles a staged hunk back to unstaged", function() }) end) -t.test("toggle_stage unstages correctly when buffer lines are shifted", function() - local dir, buf = setup("a\nb\nc\n", "a\nb\nC\n") - vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.toggle_stage(buf) - t.wait_for(function() - return #assert(hunks.state(buf)).staged == 1 - end, "the line-3 change to be staged") +t.test( + "toggle_stage unstages correctly when buffer lines are shifted", + function() + local dir, buf = setup("a\nb\nc\n", "a\nb\nC\n") + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + hunks.toggle_stage(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).staged == 1 + end, "the line-3 change to be staged") - vim.api.nvim_buf_set_lines(buf, 0, 0, false, { "NEW" }) - vim.api.nvim_exec_autocmds("TextChanged", { buffer = buf }) - hunks._flush(buf) - t.wait_for(function() - return #assert(hunks.state(buf)).hunks == 1 - end, "the unstaged add at the top to register") + vim.api.nvim_buf_set_lines(buf, 0, 0, false, { "NEW" }) + vim.api.nvim_exec_autocmds("TextChanged", { buffer = buf }) + hunks._flush(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).hunks == 1 + end, "the unstaged add at the top to register") - vim.api.nvim_win_set_cursor(0, { 4, 0 }) - hunks.toggle_stage(buf) - t.wait_for(function() - return #assert(hunks.state(buf)).staged == 0 - end, "the shifted staged hunk to be unstaged") - t.eq( - h.git(dir, "show", ":0:a.txt").stdout, - "a\nb\nc", - "the index reverts to HEAD content for the unstaged hunk" - ) -end) + vim.api.nvim_win_set_cursor(0, { 4, 0 }) + hunks.toggle_stage(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).staged == 0 + end, "the shifted staged hunk to be unstaged") + t.eq( + h.git(dir, "show", ":0:a.txt").stdout, + "a\nb\nc", + "the index reverts to HEAD content for the unstaged hunk" + ) + end +) t.test("reset_hunk restores the index content for a change", function() local _, buf, state = setup("a\nb\nc\n", "a\nB\nc\n") diff --git a/test/git/object_test.lua b/test/git/object_test.lua index 3aa79b4..0082d21 100644 --- a/test/git/object_test.lua +++ b/test/git/object_test.lua @@ -80,28 +80,28 @@ t.test("M.open(HEAD:) loads file content at HEAD", function() object.open(r, "HEAD:a.txt", { split = false }) t.eq(vim.api.nvim_buf_get_name(0), "git://" .. sha .. ":a.txt") - t.eq( - vim.api.nvim_buf_get_lines(0, 0, -1, false), - { "first", "second" } - ) + t.eq(vim.api.nvim_buf_get_lines(0, 0, -1, false), { "first", "second" }) end) -t.test("M.open on a merge commit diffs against the first parent only", function() - local dir = h.make_repo({ ["a.txt"] = "one\n" }) - t.write(dir, "a.txt", "two\n") - h.git(dir, "stash") - local stash = h.git(dir, "rev-parse", "stash@{0}").stdout - local r = assert(require("git.core.repo").resolve(dir)) +t.test( + "M.open on a merge commit diffs against the first parent only", + function() + local dir = h.make_repo({ ["a.txt"] = "one\n" }) + t.write(dir, "a.txt", "two\n") + h.git(dir, "stash") + local stash = h.git(dir, "rev-parse", "stash@{0}").stdout + local r = assert(require("git.core.repo").resolve(dir)) - object.open(r, stash, { split = false }) - local count = 0 - for _, l in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, false)) do - if l:match("^diff %-%-git ") then - count = count + 1 + object.open(r, stash, { split = false }) + local count = 0 + for _, l in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, false)) do + if l:match("^diff %-%-git ") then + count = count + 1 + end end + t.eq(count, 1, "the stashed file's diff appears once, not per-parent") end - t.eq(count, 1, "the stashed file's diff appears once, not per-parent") -end) +) t.test("M.open errors on a bogus base, no buffer is opened", function() local dir = h.make_repo({ a = "first\n" }) diff --git a/test/git/repo_test.lua b/test/git/repo_test.lua index e418551..3292fb6 100644 --- a/test/git/repo_test.lua +++ b/test/git/repo_test.lua @@ -235,27 +235,33 @@ t.test("status_entry_for: exact match on case-sensitive repo", function() t.eq(r:status_entry_for("foo"), nil, "case mismatch returns nil") end) -t.test("status_entry_for: case-insensitive fallback when core.ignorecase=true", function() - local dir = h.make_repo({ Foo = "x" }) - h.git(dir, "config", "core.ignorecase", "true") - t.write(dir, "Foo", "modified") - local r = assert(require("git.core.repo").resolve(dir)) - wait_initial(r) - t.truthy(r:status_entry_for("Foo"), "exact match") - t.truthy(r:status_entry_for("foo"), "lowercase finds Foo") - t.truthy(r:status_entry_for("FOO"), "uppercase finds Foo") -end) +t.test( + "status_entry_for: case-insensitive fallback when core.ignorecase=true", + function() + local dir = h.make_repo({ Foo = "x" }) + h.git(dir, "config", "core.ignorecase", "true") + t.write(dir, "Foo", "modified") + local r = assert(require("git.core.repo").resolve(dir)) + wait_initial(r) + t.truthy(r:status_entry_for("Foo"), "exact match") + t.truthy(r:status_entry_for("foo"), "lowercase finds Foo") + t.truthy(r:status_entry_for("FOO"), "uppercase finds Foo") + end +) -t.test("_invalidate matches stash_refs on refs/stash and logs/refs/stash", function() - local dir = h.make_repo({ a = "x" }) - local r = assert(require("git.core.repo").resolve(dir)) - r._cache.stash_refs = {} - r:_invalidate("refs/stash") - t.eq(r._cache.stash_refs, nil) - r._cache.stash_refs = {} - r:_invalidate("logs/refs/stash") - t.eq(r._cache.stash_refs, nil) -end) +t.test( + "_invalidate matches stash_refs on refs/stash and logs/refs/stash", + function() + local dir = h.make_repo({ a = "x" }) + local r = assert(require("git.core.repo").resolve(dir)) + r._cache.stash_refs = {} + r:_invalidate("refs/stash") + t.eq(r._cache.stash_refs, nil) + r._cache.stash_refs = {} + r:_invalidate("logs/refs/stash") + t.eq(r._cache.stash_refs, nil) + end +) t.test("refresh with invalidate=true wipes cache on next fetch", function() local dir = h.make_repo({ a = "x" }) @@ -271,83 +277,103 @@ t.test("refresh with invalidate=true wipes cache on next fetch", function() t.eq(r._cache["resolve:abc"], nil) end) -t.test("refresh emits change.paths listing structurally-changed paths", function() - local dir = h.make_repo({ a = "1", b = "1" }) - local r = assert(require("git.core.repo").resolve(dir)) - wait_initial(r) - t.write(dir, "a", "2") - ---@type ow.Git.Repo.Change? - local change_seen - local unsub = r:on("change", function(change) - change_seen = change - end) - r:refresh() - t.wait_for(function() - return change_seen ~= nil - end, "refresh emit", 2000) - unsub() - local change = assert(change_seen) - t.truthy(change.paths["a"]) - t.falsy(change.paths["b"], "b is unchanged structurally") -end) +t.test( + "refresh emits change.paths listing structurally-changed paths", + function() + local dir = h.make_repo({ a = "1", b = "1" }) + local r = assert(require("git.core.repo").resolve(dir)) + wait_initial(r) + t.write(dir, "a", "2") + ---@type ow.Git.Repo.Change? + local change_seen + local unsub = r:on("change", function(change) + change_seen = change + end) + r:refresh() + t.wait_for(function() + return change_seen ~= nil + end, "refresh emit", 2000) + unsub() + local change = assert(change_seen) + t.truthy(change.paths["a"]) + t.falsy(change.paths["b"], "b is unchanged structurally") + end +) -t.test("submodule: parent enumerates initialized submodules by default", function() - local outer_path = h.make_submodule_repo() - local outer = assert(require("git.core.repo").resolve(outer_path)) - t.truthy(outer._submodules["sub"], "sub recorded as submodule") -end) +t.test( + "submodule: parent enumerates initialized submodules by default", + function() + local outer_path = h.make_submodule_repo() + local outer = assert(require("git.core.repo").resolve(outer_path)) + t.truthy(outer._submodules["sub"], "sub recorded as submodule") + end +) -t.test("submodule: eagerly creates child Repos and subscribes by default", function() - local outer_path = h.make_submodule_repo() - local outer = assert(require("git.core.repo").resolve(outer_path)) - wait_initial(outer) - local inner = require("git.core.repo").all()[outer_path .. "/sub"] - t.truthy(inner, "inner Repo eagerly created") - t.truthy(outer._submodules["sub"] and outer._submodules["sub"].unsub, "inner subscribed by outer") +t.test( + "submodule: eagerly creates child Repos and subscribes by default", + function() + local outer_path = h.make_submodule_repo() + local outer = assert(require("git.core.repo").resolve(outer_path)) + wait_initial(outer) + local inner = require("git.core.repo").all()[outer_path .. "/sub"] + t.truthy(inner, "inner Repo eagerly created") + t.truthy( + outer._submodules["sub"] and outer._submodules["sub"].unsub, + "inner subscribed by outer" + ) - t.write(outer_path .. "/sub", "a", "modified\n") - ---@type ow.Git.Repo.Change? - local outer_change - local unsub = outer:on("change", function(change) - outer_change = change - end) - inner:refresh() - t.wait_for(function() - return outer_change ~= nil - end, "outer notified by inner refresh", 2000) - unsub() + t.write(outer_path .. "/sub", "a", "modified\n") + ---@type ow.Git.Repo.Change? + local outer_change + local unsub = outer:on("change", function(change) + outer_change = change + end) + inner:refresh() + t.wait_for(function() + return outer_change ~= nil + end, "outer notified by inner refresh", 2000) + unsub() - local entry = outer.status.entries["sub"] - t.truthy(entry, "outer sub entry now present") - t.eq(entry.kind, "changed") -end) + local entry = outer.status.entries["sub"] + t.truthy(entry, "outer sub entry now present") + t.eq(entry.kind, "changed") + end +) -t.test("submodule: no eager creation when flag is explicitly disabled", function() - vim.g.git_submodule_recursion = false - t.defer(function() - vim.g.git_submodule_recursion = nil - end) - local outer_path = h.make_submodule_repo() - local outer = assert(require("git.core.repo").resolve(outer_path)) - wait_initial(outer) - t.eq( - require("git.core.repo").all()[outer_path .. "/sub"], - nil, - "inner Repo not created when flag is false" - ) - t.eq(next(outer._submodules), nil) -end) +t.test( + "submodule: no eager creation when flag is explicitly disabled", + function() + vim.g.git_submodule_recursion = false + t.defer(function() + vim.g.git_submodule_recursion = nil + end) + local outer_path = h.make_submodule_repo() + local outer = assert(require("git.core.repo").resolve(outer_path)) + wait_initial(outer) + t.eq( + require("git.core.repo").all()[outer_path .. "/sub"], + nil, + "inner Repo not created when flag is false" + ) + t.eq(next(outer._submodules), nil) + end +) -t.test("submodule: outer created after inner picks up existing child", function() - local outer_path = h.make_submodule_repo() - local inner = assert( - require("git.core.repo").resolve(outer_path .. "/sub") - ) - wait_initial(inner) - local outer = assert(require("git.core.repo").resolve(outer_path)) - wait_initial(outer) - t.truthy(outer._submodules["sub"] and outer._submodules["sub"].unsub, "outer subscribed to pre-existing inner") -end) +t.test( + "submodule: outer created after inner picks up existing child", + function() + local outer_path = h.make_submodule_repo() + local inner = + assert(require("git.core.repo").resolve(outer_path .. "/sub")) + wait_initial(inner) + local outer = assert(require("git.core.repo").resolve(outer_path)) + wait_initial(outer) + t.truthy( + outer._submodules["sub"] and outer._submodules["sub"].unsub, + "outer subscribed to pre-existing inner" + ) + end +) t.test("watcher cleans up after a slash-branch dir is removed", function() local dir = h.make_repo({ a = "x" }) diff --git a/test/git/status_test.lua b/test/git/status_test.lua index fab1ab9..f88bc32 100644 --- a/test/git/status_test.lua +++ b/test/git/status_test.lua @@ -1,5 +1,5 @@ -local t = require("test") local status = require("git.core.status") +local t = require("test") local NUL = "\0" @@ -204,7 +204,10 @@ t.test("mark_for: changed staged modified", function() path = "x", staged = "modified", } - t.eq(status.mark_for(entry, "staged"), { char = "M", hl = "GitStagedModified" }) + t.eq( + status.mark_for(entry, "staged"), + { char = "M", hl = "GitStagedModified" } + ) end) t.test("mark_for: changed unstaged deleted uses GitUnstagedDeleted", function() @@ -213,7 +216,10 @@ t.test("mark_for: changed unstaged deleted uses GitUnstagedDeleted", function() path = "x", unstaged = "deleted", } - t.eq(status.mark_for(entry, "unstaged"), { char = "D", hl = "GitUnstagedDeleted" }) + t.eq( + status.mark_for(entry, "unstaged"), + { char = "D", hl = "GitUnstagedDeleted" } + ) end) t.test("mark_for: changed renamed uses per-side renamed hl", function() @@ -223,7 +229,10 @@ t.test("mark_for: changed renamed uses per-side renamed hl", function() staged = "renamed", orig = "y", } - t.eq(status.mark_for(entry, "staged"), { char = "R", hl = "GitStagedRenamed" }) + t.eq( + status.mark_for(entry, "staged"), + { char = "R", hl = "GitStagedRenamed" } + ) end) t.test("mark_for: untracked / ignored / unmerged ignore side", function() @@ -364,24 +373,26 @@ t.test("entry_equal: differing unmerged conflict returns false", function() t.falsy(status.entry_equal(a, b)) end) -t.test("diff_entries: detects additions, removals, and modifications", function() - local prior = { - a = { kind = "changed", path = "a", staged = "modified" }, - b = { kind = "untracked", path = "b" }, - } - local next_ = { - a = { kind = "changed", path = "a", staged = "added" }, - c = { kind = "untracked", path = "c" }, - } - local changed = status.diff_entries(prior, next_) - t.truthy(changed.a, "a modified") - t.truthy(changed.b, "b removed") - t.truthy(changed.c, "c added") -end) +t.test( + "diff_entries: detects additions, removals, and modifications", + function() + local prior = { + a = { kind = "changed", path = "a", staged = "modified" }, + b = { kind = "untracked", path = "b" }, + } + local next_ = { + a = { kind = "changed", path = "a", staged = "added" }, + c = { kind = "untracked", path = "c" }, + } + local changed = status.diff_entries(prior, next_) + t.truthy(changed.a, "a modified") + t.truthy(changed.b, "b removed") + t.truthy(changed.c, "c added") + end +) t.test("diff_entries: empty when entries match", function() local prior = { a = { kind = "untracked", path = "a" } } local next_ = { a = { kind = "untracked", path = "a" } } t.eq(status.diff_entries(prior, next_), {}) end) - diff --git a/test/git/status_view_test.lua b/test/git/status_view_test.lua index 371780d..df5cd2e 100644 --- a/test/git/status_view_test.lua +++ b/test/git/status_view_test.lua @@ -406,82 +406,75 @@ t.test("sidebar buffer name does not get written to disk", function() ) end) -t.test( - "diffsplit from sidebar resets cursor so panes stay in sync", - function() - local committed, worktree = {}, {} - for i = 1, 100 do - committed[i] = "line " .. i - worktree[i] = i == 10 - and "CHANGED " .. i - or i == 40 and "CHANGED " .. i - or i == 70 and "CHANGED " .. i - or i == 90 and "CHANGED " .. i - or ("line " .. i) - end - local repo = h.make_repo({ - ["file.txt"] = table.concat(committed, "\n") .. "\n", - }) - t.write(repo, "file.txt", table.concat(worktree, "\n") .. "\n") - vim.cmd("cd " .. repo) - - -- Open the worktree file in a normal window and position cursor in - -- what becomes a folded section after diff is set up. - vim.cmd("edit file.txt") - vim.api.nvim_win_set_cursor(0, { 50, 0 }) - - require("git.status_view").open({ placement = "sidebar" }) - local sidebar_buf, sidebar_win = find_sidebar() - assert(sidebar_buf and sidebar_win) - local r = assert(require("git.core.repo").find(vim.fn.getcwd())) - r:refresh() - t.wait_for(function() - return r.status and #r.status:rows("unstaged") > 0 - end, "git status to report unstaged changes") - - local entry_line - for i, l in - ipairs(vim.api.nvim_buf_get_lines(sidebar_buf, 0, -1, false)) - do - if l:match("file.txt$") then - entry_line = i - break - end - end - if not entry_line then - error("entry line should exist") - end - - vim.api.nvim_set_current_win(sidebar_win) - vim.api.nvim_win_set_cursor(sidebar_win, { entry_line, 0 }) - t.press("") - t.wait_for(function() - return find_diff_win("left") ~= nil - and find_diff_win("right") ~= nil - end, "diff pair to appear") - - local left_win = assert(find_diff_win("left")) - local right_win = assert(find_diff_win("right")) - local left_top = - vim.api.nvim_win_call(left_win, function() return vim.fn.line("w0") end) - local right_top = vim.api.nvim_win_call( - right_win, - function() return vim.fn.line("w0") end - ) - t.eq( - left_top, - right_top, - "left and right panes should have the same topline after diffsplit" - ) - t.eq( - vim.api.nvim_win_get_cursor(left_win), - { 1, 0 }, - "left pane should start at line 1" - ) - t.eq( - vim.api.nvim_win_get_cursor(right_win), - { 1, 0 }, - "right pane should start at line 1" - ) +t.test("diffsplit from sidebar resets cursor so panes stay in sync", function() + local committed, worktree = {}, {} + for i = 1, 100 do + committed[i] = "line " .. i + worktree[i] = i == 10 and "CHANGED " .. i + or i == 40 and "CHANGED " .. i + or i == 70 and "CHANGED " .. i + or i == 90 and "CHANGED " .. i + or ("line " .. i) end -) + local repo = h.make_repo({ + ["file.txt"] = table.concat(committed, "\n") .. "\n", + }) + t.write(repo, "file.txt", table.concat(worktree, "\n") .. "\n") + vim.cmd("cd " .. repo) + + -- Open the worktree file in a normal window and position cursor in + -- what becomes a folded section after diff is set up. + vim.cmd("edit file.txt") + vim.api.nvim_win_set_cursor(0, { 50, 0 }) + + require("git.status_view").open({ placement = "sidebar" }) + local sidebar_buf, sidebar_win = find_sidebar() + assert(sidebar_buf and sidebar_win) + local r = assert(require("git.core.repo").find(vim.fn.getcwd())) + r:refresh() + t.wait_for(function() + return r.status and #r.status:rows("unstaged") > 0 + end, "git status to report unstaged changes") + + local entry_line + for i, l in ipairs(vim.api.nvim_buf_get_lines(sidebar_buf, 0, -1, false)) do + if l:match("file.txt$") then + entry_line = i + break + end + end + if not entry_line then + error("entry line should exist") + end + + vim.api.nvim_set_current_win(sidebar_win) + vim.api.nvim_win_set_cursor(sidebar_win, { entry_line, 0 }) + t.press("") + t.wait_for(function() + return find_diff_win("left") ~= nil and find_diff_win("right") ~= nil + end, "diff pair to appear") + + local left_win = assert(find_diff_win("left")) + local right_win = assert(find_diff_win("right")) + local left_top = vim.api.nvim_win_call(left_win, function() + return vim.fn.line("w0") + end) + local right_top = vim.api.nvim_win_call(right_win, function() + return vim.fn.line("w0") + end) + t.eq( + left_top, + right_top, + "left and right panes should have the same topline after diffsplit" + ) + t.eq( + vim.api.nvim_win_get_cursor(left_win), + { 1, 0 }, + "left pane should start at line 1" + ) + t.eq( + vim.api.nvim_win_get_cursor(right_win), + { 1, 0 }, + "right pane should start at line 1" + ) +end)