Skip to content

Commit d52cd61

Browse files
feat: add ClaudeCodeDiffAccept and ClaudeCodeDiffDeny user commands
- Add ClaudeCodeDiffAccept and ClaudeCodeDiffDeny user commands to init.lua - Implement accept_current_diff() and deny_current_diff() functions in diff.lua - Refactor keymaps to use new commands instead of inline functions - Store diff context in buffer variables for command access - Update README.md with new commands and customization examples - Maintain backward compatibility with existing <leader>da and <leader>dq keymaps Resolves issue #44 by allowing users to customize diff keymaps without conflicts Co-authored-by: ThomasK33 <[email protected]> Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent 030fa7c commit d52cd61

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ That's it! For more configuration options, see [Advanced Setup](#advanced-setup)
8888
- `:ClaudeCodeSend` - Send current visual selection to Claude, or add files from tree explorer
8989
- `:ClaudeCodeTreeAdd` - Add selected file(s) from tree explorer to Claude context (also available via ClaudeCodeSend)
9090
- `:ClaudeCodeAdd <file-path> [start-line] [end-line]` - Add a specific file or directory to Claude context by path with optional line range
91+
- `:ClaudeCodeDiffAccept` - Accept the current diff changes (equivalent to `<leader>da`)
92+
- `:ClaudeCodeDiffDeny` - Deny/reject the current diff changes (equivalent to `<leader>dq`)
9193

9294
### Toggle Behavior
9395

@@ -155,6 +157,18 @@ When you reject changes, the diff view closes and the original file remains unch
155157

156158
You can also navigate to the Claude Code terminal window and accept or reject diffs directly from within Claude's interface. This provides an alternative way to manage diffs without using the Neovim-specific keymaps.
157159

160+
### Customizing Diff Keymaps
161+
162+
The default keymaps (`<leader>da` and `<leader>dq`) can be customized by remapping them to the underlying commands:
163+
164+
```lua
165+
-- Example: Use different keymaps for diff handling
166+
vim.keymap.set('n', '<leader>ya', '<cmd>ClaudeCodeDiffAccept<cr>', { desc = 'Accept diff' })
167+
vim.keymap.set('n', '<leader>yn', '<cmd>ClaudeCodeDiffDeny<cr>', { desc = 'Deny diff' })
168+
```
169+
170+
The commands `ClaudeCodeDiffAccept` and `ClaudeCodeDiffDeny` work only in diff buffers created by the plugin and will show a warning if used elsewhere.
171+
158172
### How It Works
159173

160174
The plugin uses a signal-based approach where accepting or rejecting a diff sends a message to Claude Code rather than directly modifying files. This ensures consistency and allows Claude Code to handle the actual file operations while the plugin manages the user interface and buffer reloading.

lua/claudecode/diff.lua

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,23 +578,16 @@ function M._create_diff_view_from_window(target_window, old_file_path, new_buffe
578578
vim.cmd("wincmd =")
579579
vim.api.nvim_set_current_win(new_win)
580580

581-
local keymap_opts = { buffer = new_buffer, silent = true }
582-
583-
vim.keymap.set("n", "<leader>da", function()
584-
M._resolve_diff_as_saved(tab_name, new_buffer)
585-
end, keymap_opts)
581+
-- Store diff context in buffer variables for user commands
582+
vim.b[new_buffer].claudecode_diff_tab_name = tab_name
583+
vim.b[new_buffer].claudecode_diff_new_win = new_win
584+
vim.b[new_buffer].claudecode_diff_target_win = target_window
586585

587-
vim.keymap.set("n", "<leader>dq", function()
588-
if vim.api.nvim_win_is_valid(new_win) then
589-
vim.api.nvim_win_close(new_win, true)
590-
end
591-
if vim.api.nvim_win_is_valid(target_window) then
592-
vim.api.nvim_set_current_win(target_window)
593-
vim.cmd("diffoff")
594-
end
586+
local keymap_opts = { buffer = new_buffer, silent = true }
595587

596-
M._resolve_diff_as_rejected(tab_name)
597-
end, keymap_opts)
588+
-- Use the new user commands instead of inline functions
589+
vim.keymap.set("n", "<leader>da", "<cmd>ClaudeCodeDiffAccept<cr>", keymap_opts)
590+
vim.keymap.set("n", "<leader>dq", "<cmd>ClaudeCodeDiffDeny<cr>", keymap_opts)
598591

599592
-- Return window information for later storage
600593
return {
@@ -899,4 +892,43 @@ function M.reload_file_buffers_manual(file_path, original_cursor_pos)
899892
return reload_file_buffers(file_path, original_cursor_pos)
900893
end
901894

895+
--- Accept the current diff (user command version)
896+
-- This function reads the diff context from buffer variables
897+
function M.accept_current_diff()
898+
local current_buffer = vim.api.nvim_get_current_buf()
899+
local tab_name = vim.b[current_buffer].claudecode_diff_tab_name
900+
901+
if not tab_name then
902+
vim.notify("No active diff found in current buffer", vim.log.levels.WARN)
903+
return
904+
end
905+
906+
M._resolve_diff_as_saved(tab_name, current_buffer)
907+
end
908+
909+
--- Deny/reject the current diff (user command version)
910+
-- This function reads the diff context from buffer variables
911+
function M.deny_current_diff()
912+
local current_buffer = vim.api.nvim_get_current_buf()
913+
local tab_name = vim.b[current_buffer].claudecode_diff_tab_name
914+
local new_win = vim.b[current_buffer].claudecode_diff_new_win
915+
local target_window = vim.b[current_buffer].claudecode_diff_target_win
916+
917+
if not tab_name then
918+
vim.notify("No active diff found in current buffer", vim.log.levels.WARN)
919+
return
920+
end
921+
922+
-- Close windows and clean up (same logic as the original keymap)
923+
if new_win and vim.api.nvim_win_is_valid(new_win) then
924+
vim.api.nvim_win_close(new_win, true)
925+
end
926+
if target_window and vim.api.nvim_win_is_valid(target_window) then
927+
vim.api.nvim_set_current_win(target_window)
928+
vim.cmd("diffoff")
929+
end
930+
931+
M._resolve_diff_as_rejected(tab_name)
932+
end
933+
902934
return M

lua/claudecode/init.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,21 @@ function M._create_commands()
880880
"Terminal module not found. Terminal commands (ClaudeCode, ClaudeCodeOpen, ClaudeCodeClose) not registered."
881881
)
882882
end
883+
884+
-- Diff management commands
885+
vim.api.nvim_create_user_command("ClaudeCodeDiffAccept", function()
886+
local diff = require("claudecode.diff")
887+
diff.accept_current_diff()
888+
end, {
889+
desc = "Accept the current diff changes",
890+
})
891+
892+
vim.api.nvim_create_user_command("ClaudeCodeDiffDeny", function()
893+
local diff = require("claudecode.diff")
894+
diff.deny_current_diff()
895+
end, {
896+
desc = "Deny/reject the current diff changes",
897+
})
883898
end
884899

885900
--- Get version information

0 commit comments

Comments
 (0)