Skip to content

Add diff viewing#91

Draft
chuan2984 wants to merge 11 commits intonickjvandyke:mainfrom
chuan2984:add_diff_viewing
Draft

Add diff viewing#91
chuan2984 wants to merge 11 commits intonickjvandyke:mainfrom
chuan2984:add_diff_viewing

Conversation

@chuan2984
Copy link
Contributor

@chuan2984 chuan2984 commented Nov 30, 2025

Summary

Add PR-style diff review UI for opencode changes.

This is my rough draft based on some discussion here. I'll be honest and that the session_diff.lua was mostly AI coded, but was verified and checked for accuracy.

Please pull it if you are interested, its been working well for me so far

Screenshots

image

Key Points

  • SSE: Uses message.updated event (contains only current cycle diffs, unlike session.diff which
    accumulates everything) for capturing the before, and after for only files touched in the current cycle.
  • Diff display:
    • Simple vim.diff() unified view
    • A more complex pr review style enhanced diff view that works with hunk staging

note: DiffView.nvim sadly does not really support what we want, since it only works with git rev not local temp files(we are not using git, we dont get much from DiffView.nvim other than the nice UI

Screenshot of enhanced diff image

Files

  • lua/opencode/diff.lua - Core review logic
  • plugin/events/session_diff.lua - Event listener
  • DIFF_FEATURE.md - Full documentation

TODOs

  • better abstraction/file organization
  • better user command
  • the ability to see the most recent diff
  • slightly better-looking UI
  • the ability to set keymaps
  • plan mode should show diff but non-editable
  • What should happen to diff if files outside of current dir are modified
  • Add toggle file panel keymap
  • Delay the events until idle or that do it without setting focus?
  • MORE testing~~~

Bugs

  • The highlighting on the current file can be off in certain scenarios, treesitter problem. Incorrect timing, re-open fixes it
  • Shows only new addition for my lua files, no idea why, this is because of symlink, This is a opencode problem.

Nice to have?

  • any permission event dont automatically switch the focus back to nvim

@@ -0,0 +1,14 @@
-- Debug helper: Add this to your Neovim config temporarily to see ALL opencode events
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: ignore this file, only for debugging, will be removed

@nickjvandyke
Copy link
Owner

sometimes closing neovim would not close the manage opencode session

I didn't test this super extensively, especially with new providers. lmk if you find a cause or fix along the way!

@froats
Copy link

froats commented Jan 8, 2026

Hi, how is this going?

@chuan2984
Copy link
Contributor Author

chuan2984 commented Jan 9, 2026

@froats , still in testing mode, i havent had too much chance to work on this. Theres something wrong with opening the current version, treesitter sometimes is not correctly attached. Other than that and some QoL stuff, i have been using it without a problem. Although i dont really use the diff features(accept hunk/reject hunk)that often(hunk definition is not that accurate). Please give it a try if you are interested. It is 80% there.

I also just attached a screenshot as well.

@naowalrahman
Copy link
Contributor

naowalrahman commented Jan 18, 2026

Opencode actually stores snapshots (git commits in a shadow repo) per-message in ~/.local/share/opencode/snapshot, so I wonder if you could use something like codediff.nvim plus a picker to let users pick the messages they want to diff between for the currently open session? (or even simpler, a session diff and last message diff).

@chuan2984
Copy link
Contributor Author

Opencode actually stores snapshots (git commits in a shadow repo) per-message in ~/.local/share/opencode/snapshot, so I wonder if you could use something like codediff.nvim plus a picker to let users pick the messages they want to diff between for the currently open session? (or even simpler, a session diff and last message diff).

thank you, didnt know about this. Will check it out tomorrow on MLK day.

@naowalrahman
Copy link
Contributor

Opencode actually stores snapshots (git commits in a shadow repo) per-message in ~/.local/share/opencode/snapshot, so I wonder if you could use something like codediff.nvim plus a picker to let users pick the messages they want to diff between for the currently open session? (or even simpler, a session diff and last message diff).

thank you, didnt know about this. Will check it out tomorrow on MLK day.

I made a PR #137!

When working with symlinked directories (e.g. ~/.config/nvim -> ~/dotfiles/.config/nvim),
Neovim can create separate buffers for the same file if accessed via different paths.
This causes issues with the diff view where the actual file might be loaded under
one path while the diff tries to open it under another.

This commit normalizes all file paths by resolving symlinks and converting to
absolute paths, ensuring consistent buffer identity.
When the actual file is already loaded in a buffer (from the user's editing session),
opening it with :diffsplit reuses the buffer without triggering FileType events.
For users who rely on FileType autocmds to attach Treesitter (via vim.treesitter.start),
this causes the diff view to show the file without syntax highlighting.

This commit detects if the buffer is already loaded and forces a reload with :edit!
before opening in diffsplit. This ensures FileType events fire properly and Treesitter
attaches to the buffer.

Fixes intermittent "no grammar for filetype found" errors in diff view.
…hment

The previous approach of reloading the buffer before diffsplit was not effective
because the reload happened in the wrong context and didn't trigger FileType events
when the buffer was subsequently opened in diffsplit.

This commit takes a direct approach:
1. Open the file with diffsplit
2. Explicitly run :filetype detect to ensure filetype is set
3. Directly call vim.treesitter.start() to attach Treesitter highlighting

This ensures syntax highlighting works regardless of whether the buffer was
previously loaded or how FileType autocmds are configured.

Fixes the issue where right pane has no filetype and no Treesitter highlighting.
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.

4 participants