Skip to content

Commit 9b38615

Browse files
authored
fix(chat): watched buffer diffs should have context id (#2410)
Co-authored-by: Oli Morris <[email protected]>
1 parent 9cc2933 commit 9b38615

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lua/codecompanion/strategies/chat/watchers.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local log = require("codecompanion.utils.log")
88

99
local api = vim.api
1010
local fmt = string.format
11+
local diff = vim.text.diff or vim.diff
1112

1213
---@class CodeCompanion.Watchers
1314
local Watchers = {}
@@ -88,13 +89,17 @@ function Watchers:get_changes(bufnr)
8889
self:unwatch(bufnr)
8990
return true, nil
9091
end
92+
9193
local buffer = self.buffers[bufnr]
94+
9295
local current_content = api.nvim_buf_get_lines(bufnr, 0, -1, false)
9396
local current_tick = api.nvim_buf_get_changedtick(bufnr)
9497
if current_tick == buffer.changedtick then
9598
return false, nil
9699
end
100+
97101
local old_content = buffer.last_sent -- Store before updating
102+
98103
local changed = has_changes(old_content, current_content)
99104
if changed then
100105
buffer.content = current_content
@@ -114,14 +119,15 @@ local function format_changes_as_diff(old_content, new_content)
114119
-- Convert line arrays to strings for vim.diff
115120
local old_str = table.concat(old_content, "\n") .. "\n"
116121
local new_str = table.concat(new_content, "\n") .. "\n"
117-
-- Use vim.diff to generate clean unified diff
118-
local diff_result = vim.diff(old_str, new_str, {
122+
123+
local diff_result = diff(old_str, new_str, {
119124
result_type = "unified",
120-
ctxlen = 3, -- 3 lines of context
125+
ctxlen = 3,
121126
algorithm = "myers",
122127
})
128+
123129
if diff_result and diff_result ~= "" then
124-
return fmt("```diff\n%s```", diff_result)
130+
return fmt("````diff\n%s````", diff_result)
125131
end
126132

127133
return ""
@@ -149,13 +155,12 @@ function Watchers:check_for_changes(chat)
149155
item.bufnr,
150156
delta
151157
),
152-
}, { visible = false })
158+
}, { context = { id = item.id }, visible = false })
153159
end
154160
elseif has_changed then
155-
-- buffer is now invalid
156161
chat:add_message({
157162
role = config.constants.USER_ROLE,
158-
content = fmt([[buffer %d has been removed.]], item.bufnr),
163+
content = fmt([[Buffer %d has been removed.]], item.bufnr),
159164
}, { visible = false })
160165
end
161166
end

0 commit comments

Comments
 (0)