From a101e671ff050124acee98944c8fb853e29e6add Mon Sep 17 00:00:00 2001 From: Pratik Tripathy Date: Mon, 13 Oct 2025 19:17:21 +0530 Subject: [PATCH] refactor(neovim): Git operation enhacements - Use gitsigns to do file diff against index - gitsigns: Use `[h` `]h` to traverse hunks instead of `[g` `]g` - gitsigns: `gQ`: Project hunks to quickfix list - gitsigns: `gs` acts as toggle to stage & unstage - diffview: `gD` Diff all files against index - Lazygit: Consistent keymap descriptions --- common/.config/nvim/lua/plugins/git.lua | 53 +++++++++---------- .../nvim/lua/plugins/utility-plugs.lua | 18 +++---- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/common/.config/nvim/lua/plugins/git.lua b/common/.config/nvim/lua/plugins/git.lua index 20ae917..9e622ba 100644 --- a/common/.config/nvim/lua/plugins/git.lua +++ b/common/.config/nvim/lua/plugins/git.lua @@ -18,8 +18,7 @@ return { "sindrets/diffview.nvim", keys = { - { "gd", "DiffviewOpen", desc = "Git: Open Diffview", mode = { "n" } }, - { "gD", "DiffviewOpen", desc = "Git: Open Diffview against master", mode = { "n" } }, + { "gD", "DiffviewOpen", desc = "Git: Diffview Project against index/staging", mode = { "n" } }, }, }, @@ -37,10 +36,14 @@ return { untracked = { text = "┆" }, }, attach_to_untracked = true, + current_line_blame = true, -- Show git line blame-line by default + current_line_blame_opts = { + virt_text_opt = "right_align", + ignore_whitespace = true, + }, + current_line_blame_formatter = ", , ", on_attach = function(bufnr) - local gs = package.loaded.gitsigns - - gs.toggle_current_line_blame() -- git blame line + local gs = require("gitsigns") local function map(mode, l, r, opts) opts = opts or {} @@ -48,34 +51,28 @@ return { vim.keymap.set(mode, l, r, opts) end - -- Navigation - map({ "n", "v" }, "]g", function() - if vim.wo.diff then - return "]g" - end - vim.schedule(function() - gs.next_hunk() - end) - return "" - end, { expr = true, desc = "Next Git hunk" }) - - map({ "n", "v" }, "[g", function() - if vim.wo.diff then - return "[g" - end - vim.schedule(function() - gs.prev_hunk() - end) - return "" - end, { expr = true, desc = "Previous Git hunk" }) - -- Text object map({ "o", "x" }, "ih", ":Gitsigns select_hunk", { desc = "Git: Visual select hunk" }) + map({ "n", "v" }, "]h", function() + gs.next_hunk() + end, { desc = "Next Git hunk" }) + + map({ "n", "v" }, "[h", function() + gs.prev_hunk() + end, { desc = "Previous Git hunk" }) + + map("n", "gQ", function() + gs.setqflist("all", { open = false }) + end, { desc = "Git: Project Hunks to quickfix list" }) + + map("n", "gd", function() + gs.diffthis("~1") + end, { desc = "git: Diff the file against last commit" }) + map("n", "gr", gs.reset_hunk, { desc = "Git: Reset hunk" }) map("n", "gp", gs.preview_hunk, { desc = "Git: Preview hunk" }) - map("n", "gs", gs.stage_hunk, { desc = "Git: Stage hunk under cursor" }) - map("n", "gu", gs.undo_stage_hunk, { desc = "Git: Unstage hunk under cursor" }) + map("n", "gs", gs.stage_hunk, { desc = "Git: Toggle Stage-Hunk" }) map("n", "gK", function() gs.blame_line({ full = true }) diff --git a/common/.config/nvim/lua/plugins/utility-plugs.lua b/common/.config/nvim/lua/plugins/utility-plugs.lua index 5e3734e..435980a 100644 --- a/common/.config/nvim/lua/plugins/utility-plugs.lua +++ b/common/.config/nvim/lua/plugins/utility-plugs.lua @@ -188,14 +188,21 @@ return { function() Snacks.lazygit.open(opts) end, - desc = "Git: Show LazyGit", + desc = "LazyGit: Show LazyGit", }, { "gl", function() Snacks.lazygit.log(opts) end, - desc = "LazyGit: List Git Log", + desc = "LazyGit: Git Log Graph", + }, + { + "gf", + function() + Snacks.lazygit.log_file(opts) + end, + desc = "LazyGit: Show File Log", }, { "gL", @@ -204,13 +211,6 @@ return { end, desc = "Git: Line Log", }, - { - "gf", - function() - Snacks.lazygit.log_file(opts) - end, - desc = "Git: Show File Log", - }, { "gO", function()