Skip to content

feat: reload edited buffer after Claude writes to disk#23

Open
kam-hak wants to merge 1 commit intoCannon07:mainfrom
kam-hak:feat/reload-edited-buffer
Open

feat: reload edited buffer after Claude writes to disk#23
kam-hak wants to merge 1 commit intoCannon07:mainfrom
kam-hak:feat/reload-edited-buffer

Conversation

@kam-hak
Copy link
Copy Markdown
Contributor

@kam-hak kam-hak commented Apr 4, 2026

Problem

After Claude edits a file that's open in nvim, the buffer shows stale content until the user manually focuses it. Nvim's autoread only checks on focus/CursorHold events, so edits made by an external process (Claude CLI) don't appear immediately.

Fix

After PostToolUse fires, send a targeted checktime call via RPC for just the edited file's buffer. Other open buffers are not affected.

Test plan

  • Have a file open in nvim, let Claude edit it — buffer updates without refocusing
  • Have other files open — they remain unchanged
  • Edit a file not open in nvim — no error, no effect

@kam-hak
Copy link
Copy Markdown
Contributor Author

kam-hak commented Apr 4, 2026

Self-review caught that vim.fn.bufnr(path) uses file-pattern matching (partial + regex), so paths with regex metacharacters like [, ], * could match the wrong buffer. Replaced with an explicit nvim_list_bufs() loop comparing canonical paths via fs_realpath, and scoped checktime to the matched buffer number. Pushed.

kam-hak added a commit to kam-hak/claude-preview.nvim that referenced this pull request Apr 4, 2026
Apply the four fixes from the PR review cycle:

- PR Cannon07#20: restore post-cleanup reveal inside is_open() guard so
  newly-created files appear in neo-tree after accept.
- PR Cannon07#21: replace jq '// true' with explicit conditional so
  reveal=false config is actually honored (// treats false like null).
- PR Cannon07#21/Cannon07#24: restore walk-up-to-parent logic for created files so
  neo-tree can expand paths that don't exist on disk yet.
- PR Cannon07#23: replace bufnr(path) with literal canonical-path comparison;
  bufnr does regex matching and mis-matches paths with metacharacters.
- PR Cannon07#24: drop the is_mac + :lower() hack; fs_realpath already returns
  the filesystem canonical form, and lowercasing produces false
  positives on case-sensitive filesystems.
@Cannon07
Copy link
Copy Markdown
Owner

Cannon07 commented Apr 5, 2026

Hey! Just merged PRs #20 and #21 — thanks for the quick updates on both. PR #23 and #24 now has merge conflicts from the recent merges. Could you rebase it on top of the latest main and resolve the conflicts? Let me know if you need any help. Thanks!

Fires checktime on the buffer matching the edited file so changes
appear without requiring focus/blur. Runs regardless of diff state
(visible_only mode, manually-closed tab, etc) — Claude wrote the
file whether or not a preview was shown.

Iterates nvim_list_bufs() and compares canonical paths (fs_realpath)
instead of vim.fn.bufnr(), which does partial+regex matching on
buffer names and mis-matches paths containing regex metacharacters
(e.g. /tmp/foo[1].md).
@kam-hak kam-hak force-pushed the feat/reload-edited-buffer branch from 8c539b9 to cc606ad Compare April 5, 2026 16:06
@kam-hak
Copy link
Copy Markdown
Contributor Author

kam-hak commented Apr 5, 2026

Rebased onto main, squashed to a single commit.

Moved the buffer reload outside the DIFF_OPEN guard so it runs whether or not a preview was shown. Otherwise it gets skipped under visible_only mode or with a manually-closed tab.

@Cannon07
Copy link
Copy Markdown
Owner

Cannon07 commented Apr 5, 2026

Thanks for the PR!

I tested this on my end but wasn't able to reproduce the stale buffer issue — the buffer seems to reload correctly after Claude edits, even without this change. Could you share the exact steps to reproduce the stale buffer (which file layout, split/tab setup, etc.)

Once I can reproduce the issue, I'd be happy to move forward with this. Thanks!

@crisecheverria
Copy link
Copy Markdown

This happened to me when having a buffer open and then opening claude code inside neovim using :vsplit | term and did an edit via claude code on the opened file. It only updated the buffer when I changed focus from neovim to another app and return into neovim, otherwise i had to run :e to see the change.

@Cannon07
Copy link
Copy Markdown
Owner

Cannon07 commented Apr 19, 2026

Thanks for the repro @crisecheverria!

Adding TermLeave to your checktime autocmd should fix it:

vim.o.autoread = true
vim.api.nvim_create_autocmd({ "FocusGained", "BufEnter", "CursorHold", "TermLeave" }, {
  command = "checktime",
})

I'll update the Recommended companion settings in the README to include TermLeave.

Adding TermLeave to your checktime autocmd should fix it as a workaround for now. We're also looking at integrating this into the plugin directly (this PR) so it works out of the box.

@Cannon07
Copy link
Copy Markdown
Owner

Hey @kam-hak, Happy to merge this in.

That said, the codebase has changed quite a bit since your last rebase. The buffer reload logic would need to go into core-post-tool.sh instead of claude-close-diff.sh.

Would you be up for rebasing again, or would you prefer I take over from here and integrate the change? Either way works — the nvim_list_bufs + fs_realpath approach is solid.

@kam-hak
Copy link
Copy Markdown
Contributor Author

kam-hak commented Apr 19, 2026

Hey @kam-hak, Happy to merge this in.

That said, the codebase has changed quite a bit since your last rebase. The buffer reload logic would need to go into core-post-tool.sh instead of claude-close-diff.sh.

Would you be up for rebasing again, or would you prefer I take over from here and integrate the change? Either way works — the nvim_list_bufs + fs_realpath approach is solid.

Hey! Yea sorry this is stale now. I wouldn't be able to do this for about a week so please feel free to integrate/merge, or i can do it around Friday if you end up being busy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants