My nvim configuration
feat(claude): show changes in chat window
2 files changed, +59 -0
+30-0claude-nvim/lua/claude/runner.lua
| @@ -154,6 +154,36 @@ local function handle_event(sid, evt) | ||
| 154 | 154 | summary = (summary or ""):gsub("\n", " ") |
| 155 | 155 | if #summary > 200 then summary = summary:sub(1, 200) .. "…" end |
| 156 | 156 | ui.append_tool_use(sid, name, summary) |
| 157 | + | |
| 158 | + local diff_lines | |
| 159 | + if name == "Edit" and input.old_string and input.new_string then | |
| 160 | + diff_lines = {} | |
| 161 | + for _, l in ipairs(vim.split(input.old_string, "\n", { plain = true })) do | |
| 162 | + table.insert(diff_lines, "- " .. l) | |
| 163 | + end | |
| 164 | + for _, l in ipairs(vim.split(input.new_string, "\n", { plain = true })) do | |
| 165 | + table.insert(diff_lines, "+ " .. l) | |
| 166 | + end | |
| 167 | + elseif name == "MultiEdit" and type(input.edits) == "table" then | |
| 168 | + diff_lines = {} | |
| 169 | + for i, e in ipairs(input.edits) do | |
| 170 | + if i > 1 then table.insert(diff_lines, "@@") end | |
| 171 | + for _, l in ipairs(vim.split(e.old_string or "", "\n", { plain = true })) do | |
| 172 | + table.insert(diff_lines, "- " .. l) | |
| 173 | + end | |
| 174 | + for _, l in ipairs(vim.split(e.new_string or "", "\n", { plain = true })) do | |
| 175 | + table.insert(diff_lines, "+ " .. l) | |
| 176 | + end | |
| 177 | + end | |
| 178 | + elseif name == "Write" and input.content then | |
| 179 | + diff_lines = {} | |
| 180 | + for _, l in ipairs(vim.split(input.content, "\n", { plain = true })) do | |
| 181 | + table.insert(diff_lines, "+ " .. l) | |
| 182 | + end | |
| 183 | + end | |
| 184 | + if diff_lines and #diff_lines > 0 then | |
| 185 | + ui.append_diff(sid, diff_lines) | |
| 186 | + end | |
| 157 | 187 | end |
| 158 | 188 | end |
| 159 | 189 | end |
+29-0claude-nvim/lua/claude/ui.lua
| @@ -512,6 +512,35 @@ function M.append_assistant_text(sid, text) | ||
| 512 | 512 | end) |
| 513 | 513 | end |
| 514 | 514 | |
| 515 | +local ns_diff = vim.api.nvim_create_namespace("claude_panel_diff") | |
| 516 | + | |
| 517 | +function M.append_diff(sid, lines) | |
| 518 | + if sid ~= panel.sid then return end | |
| 519 | + vim.schedule(function() | |
| 520 | + local buf = panel.buf | |
| 521 | + if not buf or not vim.api.nvim_buf_is_valid(buf) then return end | |
| 522 | + spinner_remove_if_present() | |
| 523 | + vim.bo[buf].modifiable = true | |
| 524 | + local start_row = vim.api.nvim_buf_line_count(buf) | |
| 525 | + vim.api.nvim_buf_set_lines(buf, start_row, start_row, false, lines) | |
| 526 | + vim.api.nvim_buf_set_lines(buf, -1, -1, false, { "" }) | |
| 527 | + vim.bo[buf].modifiable = false | |
| 528 | + for i, l in ipairs(lines) do | |
| 529 | + local row = start_row + i - 1 | |
| 530 | + local hl | |
| 531 | + local c = l:sub(1, 1) | |
| 532 | + if c == "+" then hl = "DiffAdd" | |
| 533 | + elseif c == "-" then hl = "DiffDelete" | |
| 534 | + elseif l:sub(1, 2) == "@@" then hl = "DiffChange" end | |
| 535 | + if hl then | |
| 536 | + pcall(vim.api.nvim_buf_set_extmark, buf, ns_diff, row, 0, { line_hl_group = hl }) | |
| 537 | + end | |
| 538 | + end | |
| 539 | + spinner_render() | |
| 540 | + scroll_to_bottom_now() | |
| 541 | + end) | |
| 542 | +end | |
| 543 | + | |
| 515 | 544 | function M.append_tool_use(sid, name, summary) |
| 516 | 545 | if sid ~= panel.sid then return end |
| 517 | 546 | vim.schedule(function() |