From 942dbdcaa074ea9828e68e406cb250a075fa0be9 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 20 May 2026 13:19:01 +0200 Subject: [PATCH] refactor(git): rename stage_hunk to toggle_stage --- lua/core/keymap.lua | 2 +- lua/git/hunks.lua | 2 +- plugin/git.lua | 4 +-- test/git/hunks_test.lua | 66 ++++++++++++++++++++--------------------- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lua/core/keymap.lua b/lua/core/keymap.lua index f73e76e..0d1150b 100644 --- a/lua/core/keymap.lua +++ b/lua/core/keymap.lua @@ -235,7 +235,7 @@ vim.keymap.set("n", "gc", "(git-commit)") vim.keymap.set("n", "ga", "(git-commit-amend)") vim.keymap.set("n", "gl", "(git-log)") vim.keymap.set("n", "gv", "(git-hunk-select)") -vim.keymap.set("n", "gs", "(git-hunk-stage)") +vim.keymap.set("n", "gs", "(git-hunk-stage-toggle)") vim.keymap.set("n", "gr", "(git-hunk-reset)") vim.keymap.set("n", "g", "(git-hunk-preview)") vim.keymap.set("n", "go", "(git-overlay-toggle)") diff --git a/lua/git/hunks.lua b/lua/git/hunks.lua index bde75b1..badc1c1 100644 --- a/lua/git/hunks.lua +++ b/lua/git/hunks.lua @@ -813,7 +813,7 @@ local function apply_patch(state, buf, patch, zero_context) end ---@param buf? integer -function M.stage_hunk(buf) +function M.toggle_stage(buf) buf = resolve_buf(buf) local state = states[buf] if not state then diff --git a/plugin/git.lua b/plugin/git.lua index dffee7b..c84a238 100644 --- a/plugin/git.lua +++ b/plugin/git.lua @@ -295,8 +295,8 @@ end, { silent = true, desc = "Jump to next git hunk" }) vim.keymap.set({ "n", "x" }, "(git-hunk-prev)", function() require("git.hunks").nav("prev") end, { silent = true, desc = "Jump to previous git hunk" }) -vim.keymap.set("n", "(git-hunk-stage)", function() - require("git.hunks").stage_hunk() +vim.keymap.set("n", "(git-hunk-stage-toggle)", function() + require("git.hunks").toggle_stage() end, { silent = true, desc = "Stage or unstage the hunk under cursor" }) vim.keymap.set("n", "(git-hunk-reset)", function() require("git.hunks").reset_hunk() diff --git a/test/git/hunks_test.lua b/test/git/hunks_test.lua index 8334bed..e3d71e8 100644 --- a/test/git/hunks_test.lua +++ b/test/git/hunks_test.lua @@ -261,10 +261,10 @@ t.test("overlay: toggling swaps gutter signs for the overlay", function() ) end) -t.test("stage_hunk stages the change into the index", function() +t.test("toggle_stage stages the change into the index", function() local dir, buf = setup("a\nb\nc\n", "a\nB\nc\n") vim.api.nvim_win_set_cursor(0, { 2, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "stage to land in the index") @@ -276,27 +276,27 @@ t.test("stage_hunk stages the change into the index", function() ) end) -t.test("stage_hunk stages a pure addition", function() +t.test("toggle_stage stages a pure addition", function() local dir, buf = setup("a\nb\n", "a\nb\nc\n") vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "stage to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc") end) -t.test("stage_hunk stages a deletion", function() +t.test("toggle_stage stages a deletion", function() local dir, buf = setup("a\nb\nc\n", "a\nc\n") vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "stage to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nc") end) -t.test("stage_hunk stages only the hunk under the cursor", function() +t.test("toggle_stage stages only the hunk under the cursor", function() local committed = table.concat({ "local M = {}", "", @@ -330,7 +330,7 @@ t.test("stage_hunk stages only the hunk under the cursor", function() }, "\n") .. "\n" local dir, buf = setup(committed, worktree) vim.api.nvim_win_set_cursor(0, { 9, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the mid hunk to land in the index") @@ -357,50 +357,50 @@ t.test("stage_hunk stages only the hunk under the cursor", function() ) end) -t.test("stage_hunk stages a whole-file change with no context", function() +t.test("toggle_stage stages a whole-file change with no context", function() local dir, buf = setup("a\nb\nc\n", "x\ny\nz\n") vim.api.nvim_win_set_cursor(0, { 2, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the change to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "x\ny\nz") end) -t.test("stage_hunk stages a change at the start of the file", function() +t.test("toggle_stage stages a change at the start of the file", function() local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nc\nd\ne\n") vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the change to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "A\nb\nc\nd\ne") end) -t.test("stage_hunk stages a change at the end of the file", function() +t.test("toggle_stage stages a change at the end of the file", function() local dir, buf = setup("a\nb\nc\nd\ne\n", "a\nb\nc\nd\nE\n") vim.api.nvim_win_set_cursor(0, { 5, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the change to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc\nd\nE") end) -t.test("stage_hunk stages a deletion at the start of the file", function() +t.test("toggle_stage stages a deletion at the start of the file", function() local dir, buf = setup("a\nb\nc\nd\n", "b\nc\nd\n") vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the deletion to land in the index") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "b\nc\nd") end) -t.test("stage_hunk leaves an adjacent unstaged hunk in place", function() +t.test("toggle_stage leaves an adjacent unstaged hunk in place", function() local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n") vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" end, "the line-3 hunk to land in the index") @@ -411,22 +411,22 @@ t.test("stage_hunk leaves an adjacent unstaged hunk in place", function() ) end) -t.test("stage_hunk unstages one of two adjacent staged hunks", function() +t.test("toggle_stage unstages one of two adjacent staged hunks", function() local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n") vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).staged == 1 end, "the line-1 hunk to be staged") vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() local s = assert(hunks.state(buf)) return #s.hunks == 0 and #s.staged == 2 end, "both hunks to be staged") vim.api.nvim_win_set_cursor(0, { 3, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).staged == 1 end, "the line-3 hunk to be unstaged again") @@ -437,18 +437,18 @@ t.test("stage_hunk unstages one of two adjacent staged hunks", function() ) end) -t.test("stage_hunk refreshes the gutter when status stays modified", function() +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.stage_hunk(buf) + 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.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).hunks == 1 end, "gutter to drop the middle staged hunk") @@ -457,7 +457,7 @@ end) t.test("staged hunks show with the staged highlight", function() local _, buf = setup("a\nb\nc\n", "a\nB\nc\n") vim.api.nvim_win_set_cursor(0, { 2, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() local s = assert(hunks.state(buf)) return #s.hunks == 0 and #s.staged == 1 @@ -470,7 +470,7 @@ end) t.test("the gutter shows staged and unstaged hunks together", function() local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") vim.api.nvim_win_set_cursor(0, { 1, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).hunks == 2 end, "the first hunk to leave the unstaged set") @@ -481,15 +481,15 @@ t.test("the gutter shows staged and unstaged hunks together", function() }) end) -t.test("stage_hunk toggles a staged hunk back to unstaged", function() +t.test("toggle_stage toggles a staged hunk back to unstaged", function() local _, buf = setup("a\nb\nc\n", "a\nB\nc\n") vim.api.nvim_win_set_cursor(0, { 2, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() local s = assert(hunks.state(buf)) return #s.hunks == 0 and #s.staged == 1 end, "the hunk to become staged") - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() local s = assert(hunks.state(buf)) return #s.hunks == 1 and #s.staged == 0 @@ -499,10 +499,10 @@ t.test("stage_hunk toggles a staged hunk back to unstaged", function() }) end) -t.test("stage_hunk unstages correctly when buffer lines are shifted", function() +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.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).staged == 1 end, "the line-3 change to be staged") @@ -515,7 +515,7 @@ t.test("stage_hunk unstages correctly when buffer lines are shifted", function() end, "the unstaged add at the top to register") vim.api.nvim_win_set_cursor(0, { 4, 0 }) - hunks.stage_hunk(buf) + hunks.toggle_stage(buf) t.wait_for(function() return #assert(hunks.state(buf)).staged == 0 end, "the shifted staged hunk to be unstaged")