|
| 1 | +# Change-By-Change Review |
| 2 | + |
| 3 | +Review changes Opencode made using [diffview-plus](https://github.com/dlyongemallo/diffview-plus.nvim) |
| 4 | +and (optionally) [gitsigns](https://github.com/lewis6991/gitsigns.nvim) |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## Problem |
| 9 | + |
| 10 | +Opencode.nvim already provides a way to see changes after running a prompt. |
| 11 | +It can be opened using `:Opencode diff_open`, `:Opencode diff open`, hovering the `Created Snapshot` output |
| 12 | +and pressing `D`, or the default keymap `<leader>od`. |
| 13 | + |
| 14 | +Diffview+ gives the user a few more features: layout customization, a tree to navigate changed files from, |
| 15 | +a way to mark what you've already reviewed, accepting/rejecting changes, |
| 16 | +familiar keymaps if you're already using it, and a bunch more configuration options. |
| 17 | + |
| 18 | +Gitsigns can be used to fill context when asking the AI to revise an edit. |
| 19 | + |
| 20 | +## Solution |
| 21 | + |
| 22 | +This integration can be made by adding a few lines to diffview+'s and gitsigns' configs. Really just adding |
| 23 | +a couple of keymaps. |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | + |
| 29 | +- [diffview+](https://github.com/dlyongemallo/diffview-plus.nvim) |
| 30 | +- [gitsigns](https://github.com/lewis6991/gitsigns.nvim) (optional) |
| 31 | + |
| 32 | +### Setup |
| 33 | + |
| 34 | +#### Diffview/Diffview-plus |
| 35 | + |
| 36 | +**`Lazy keys spec (if you lazy load opencode.nvim):`** |
| 37 | +```lua |
| 38 | +keys = { |
| 39 | + { "<leader>odv", function () |
| 40 | + local path = require("opencode.config_file").get_workspace_snapshot_path():wait() |
| 41 | + local first_snapshot = require("opencode.git_review").get_first_snapshot() |
| 42 | + vim.cmd("DiffviewOpen \"-C=" .. path .. "\"" .. " " .. first_snapshot) |
| 43 | + end, desc = "diff against start of session"}, |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +**`Or vim.keymap.set:`** |
| 48 | +```lua |
| 49 | +vim.keymap.set("n", "<leader>odv", function() |
| 50 | + local path = require("opencode.config_file").get_workspace_snapshot_path():wait() |
| 51 | + local first_snapshot = require("opencode.git_review").get_first_snapshot() |
| 52 | + vim.cmd("DiffviewOpen \"-C=" .. path .. "\"" .. " " .. first_snapshot) |
| 53 | +end, { desc = "open session changes in diffview"}) |
| 54 | +``` |
| 55 | + |
| 56 | +#### Gitsigns |
| 57 | + |
| 58 | +This is from the [gitsigns recommended keymaps](https://github.com/lewis6991/gitsigns.nvim#-keymaps). |
| 59 | +This will give you the "hunk x of y" output, but is not necessary. `]c` and `[c` are defined in a diff |
| 60 | +buffer by NeoVim, you'd just have to select them manually without the gitsigns shorthand. |
| 61 | + |
| 62 | +**`gitsigns configuration`** (straight from `gitsigns`'s recommended setup) |
| 63 | +```lua |
| 64 | +opts = { |
| 65 | + on_attach = { |
| 66 | + local gs = require("gitsigns") |
| 67 | + map('n', ']c', function() |
| 68 | + if vim.wo.diff then |
| 69 | + vim.cmd.normal({']c', bang = true}) |
| 70 | + else |
| 71 | + gs.nav_hunk('next') |
| 72 | + end |
| 73 | + end, "next change") |
| 74 | + map('n', '[c', function() |
| 75 | + if vim.wo.diff then |
| 76 | + vim.cmd.normal({'[c', bang = true}) |
| 77 | + else |
| 78 | + gs.nav_hunk('prev') |
| 79 | + end |
| 80 | + end, "prev change") |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Add this new mapping in the `on_attach` after your other mappings to send a hunk to context: |
| 86 | + |
| 87 | +**`gitsigns.opts.on_attach`** |
| 88 | +```lua |
| 89 | + map("n", "<leader>oyh", function() |
| 90 | + vim.cmd("Gitsigns select_hunk") |
| 91 | + require("opencode.context").add_visual_selection() |
| 92 | + end, "send hunk to opencode context") |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +### Usage |
| 97 | + |
| 98 | +Once Opencode has created a snapshot and made some edits, you can review the edits since the start of |
| 99 | +the session by pressing `<leader>odv`. |
| 100 | + |
| 101 | +Inside diffview+: |
| 102 | + |
| 103 | +- `<tab>` and `<s-tab>` jump between changed files |
| 104 | +- `]c` and `[c` jump between changes |
| 105 | +- use `do` to "use other diff" (delete a change) and `dp` if you're in the `old` buffer to "put diff" |
| 106 | +into the `LOCAL` buffer |
| 107 | +- if you're in the `LOCAL` buffer, `<leader>oyh` will send the hunk to Opencode context. |
| 108 | +- `:tabc` to exit diffview+ |
| 109 | + |
| 110 | +## How It Works |
| 111 | + |
| 112 | +Diffview+ can start with a different path to git. That's specified with the `-C` argument. |
| 113 | +Git itself can give you a diff against these snapshots. This causes Diffview+ to use the path |
| 114 | +provided by `-C` as its git command execution base. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +Contributed by @[Kortantic](https://github.com/kortantic) |
0 commit comments