Skip to content

Commit 48755d2

Browse files
committed
show hunk navigation
1 parent f9d1405 commit 48755d2

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

lua/codecompanion/diff/ui.lua

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local M = {}
99
---@class CodeCompanion.DiffUI
1010
---@field diff CC.Diff
1111
---@field diff_id number
12+
---@field hunks number The total number of hunks in the diff
13+
---@field current_hunk number The current hunk index (1-based)
1214
---@field bufnr number
1315
---@field winnr number
1416
---@field chat_bufnr? number If the diff has an associated chat buffer, pass in the chat buffer number
@@ -29,7 +31,7 @@ end
2931

3032
---Show instructions for diff interaction
3133
---@param bufnr number
32-
---@param opts {namespace: number, line?: number, overwrite?: boolean}
34+
---@param opts {current_hunk: number, hunks: number, namespace: number, line?: number, overwrite?: boolean}
3335
---@return nil
3436
local function show_keymaps(bufnr, opts)
3537
local namespace = "codecompanion_diff_ui_" .. tostring(opts.namespace)
@@ -46,7 +48,9 @@ local function show_keymaps(bufnr, opts)
4648

4749
ui_utils.show_buffer_notification(bufnr, {
4850
text = string.format(
49-
"[%s] Always Accept | [%s] Accept | [%s] Reject | [%s]/[%s] Next/Prev hunks | [q] Close",
51+
"(%d/%d) [%s] Always Accept | [%s] Accept | [%s] Reject | [%s]/[%s] Next/Prev hunks | [q] Close",
52+
opts.current_hunk or 1,
53+
opts.hunks or 1,
5054
always_accept,
5155
accept,
5256
reject,
@@ -62,18 +66,33 @@ end
6266
---Navigate to next hunk
6367
---@param line number
6468
function DiffUI:next_hunk(line)
65-
for _, hunk in ipairs(self.diff.hunks) do
69+
for index, hunk in ipairs(self.diff.hunks) do
6670
local hunk_line = hunk.pos[1] + 1
6771
if hunk_line > line then
68-
show_keymaps(self.bufnr, { overwrite = true, namespace = self.diff.namespace, line = hunk_line - 2 })
72+
self.current_hunk = index
73+
show_keymaps(self.bufnr, {
74+
current_hunk = self.current_hunk,
75+
hunks = self.hunks,
76+
overwrite = true,
77+
namespace = self.diff.namespace,
78+
line = hunk_line - 2,
79+
})
6980
return ui_utils.scroll_to_line(self.bufnr, hunk_line)
7081
end
7182
end
7283

84+
-- Wrap around to first hunk
7385
if #self.diff.hunks > 0 then
74-
line = self.diff.hunks[1].pos[1] + 1
75-
show_keymaps(self.bufnr, { overwrite = true, namespace = self.diff.namespace, line = line - 2 })
76-
ui_utils.scroll_to_line(self.bufnr, line)
86+
self.current_hunk = 1
87+
local hunk_line = self.diff.hunks[1].pos[1] + 1
88+
show_keymaps(self.bufnr, {
89+
current_hunk = self.current_hunk,
90+
hunks = self.hunks,
91+
overwrite = true,
92+
namespace = self.diff.namespace,
93+
line = hunk_line - 2,
94+
})
95+
ui_utils.scroll_to_line(self.bufnr, hunk_line)
7796
end
7897
end
7998

@@ -85,15 +104,30 @@ function DiffUI:previous_hunk(line)
85104
local hunk = self.diff.hunks[i]
86105
local hunk_line = hunk.pos[1] + 1
87106
if hunk_line < line then
88-
show_keymaps(self.bufnr, { overwrite = true, namespace = self.diff.namespace, line = hunk_line - 2 })
107+
self.current_hunk = i
108+
show_keymaps(self.bufnr, {
109+
current_hunk = self.current_hunk,
110+
hunks = self.hunks,
111+
overwrite = true,
112+
namespace = self.diff.namespace,
113+
line = hunk_line - 2,
114+
})
89115
return ui_utils.scroll_to_line(self.bufnr, hunk_line)
90116
end
91117
end
92118

119+
-- Wrap around to last hunk
93120
if #self.diff.hunks > 0 then
94-
line = self.diff.hunks[#self.diff.hunks].pos[1] + 1
95-
show_keymaps(self.bufnr, { overwrite = true, namespace = self.diff.namespace, line = line - 2 })
96-
ui_utils.scroll_to_line(self.bufnr, line)
121+
self.current_hunk = #self.diff.hunks
122+
local hunk_line = self.diff.hunks[#self.diff.hunks].pos[1] + 1
123+
show_keymaps(self.bufnr, {
124+
current_hunk = self.current_hunk,
125+
hunks = self.hunks,
126+
overwrite = true,
127+
namespace = self.diff.namespace,
128+
line = hunk_line - 2,
129+
})
130+
ui_utils.scroll_to_line(self.bufnr, hunk_line)
97131
end
98132
end
99133

@@ -158,25 +192,27 @@ function M.show(diff, opts)
158192
title = opts.title or get_buf_name(diff.bufnr),
159193
})
160194

161-
-- Apply diff extmarks
162-
local Diff = require("codecompanion.diff")
163-
Diff.apply(diff, bufnr)
164-
show_keymaps(bufnr, { namespace = diff.namespace })
165-
166-
-- Lock the buffer so the user can't make any changes
167-
vim.bo[bufnr].modified = false
168-
vim.bo[bufnr].modifiable = false
169-
170195
---@type CodeCompanion.DiffUI
171196
local diff_ui = setmetatable({
172197
diff = diff,
173198
diff_id = opts.diff_id or math.random(10000000),
199+
hunks = #diff.hunks,
200+
current_hunk = 1,
174201
bufnr = bufnr,
175202
winnr = winnr,
176203
chat_bufnr = opts.chat_bufnr,
177204
tool_name = opts.tool_name,
178205
}, DiffUI)
179206

207+
-- Apply diff extmarks
208+
local Diff = require("codecompanion.diff")
209+
Diff.apply(diff, bufnr)
210+
show_keymaps(bufnr, { current_hunk = 1, hunks = #diff.hunks, namespace = diff.namespace })
211+
212+
-- Lock the buffer so the user can't make any changes
213+
vim.bo[bufnr].modified = false
214+
vim.bo[bufnr].modifiable = false
215+
180216
diff_ui:setup_keymaps()
181217

182218
-- If the user closes a window prematurely then reject the diff

0 commit comments

Comments
 (0)