Skip to content

Commit 33f9d5e

Browse files
committed
feat: Add first implementation of split diff view plugin for Claude Code
- Create marketplace.json and plugin.json for plugin metadata - Implement GitHub Actions workflow for testing - Add README.md with installation instructions and usage details - Develop hooks for rendering split diffs in two columns - Implement split-rows logic to handle diff data - Add tests for split-rows functionality - Configure TypeScript settings
1 parent b4d1452 commit 33f9d5e

9 files changed

Lines changed: 521 additions & 0 deletions

File tree

‎.claude-plugin/marketplace.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "neteye-platform",
3+
"description": "Claude Code plugins and mods by NetEye Platform developers",
4+
"owner": {
5+
"name": "NetEye Platform",
6+
"url": "https://github.com/neteye-platform"
7+
},
8+
"plugins": [
9+
{
10+
"name": "cc-split-diff-view",
11+
"source": "./",
12+
"description": "Edit and Write diffs in two side-by-side columns instead of +/- lines"
13+
}
14+
]
15+
}

‎.claude-plugin/plugin.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "cc-split-diff-view",
3+
"version": "0.3.0",
4+
"description": "Draws Edit and Write diffs in two side-by-side columns: removed lines on the left, added lines on the right, in Claude Code's own diff colors",
5+
"author": { "name": "oskarnrk", "url": "https://github.com/oskarnrk" },
6+
"repository": "https://github.com/neteye-platform/cc-split-diff-view"
7+
}

‎.github/workflows/test.yaml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "**"
7+
push:
8+
branches:
9+
- main
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
test:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
19+
20+
- name: Install Claude Code
21+
run: npm install -g @anthropic-ai/claude-code
22+
23+
- name: Run tests
24+
run: claude plugin test

‎README.md‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Split diff view for Claude Code
2+
3+
A [Claude Code mod](https://code.claude.com/docs/en/plugins/mods/overview) that draws the
4+
diff of every `Edit` and `Write` in **two side-by-side columns** instead of one stacked
5+
`-`/`+` list:
6+
7+
- removed lines on the **left**, added lines on the **right**
8+
- each side keeps its real line numbers
9+
- a changed line sits next to the line that replaced it
10+
- long runs between hunks collapse to `⋯ N unchanged lines`
11+
- colors are Claude Code's own diff colors (`diffRemoved` / `diffAdded` theme keys), so
12+
they follow your `/theme`
13+
- syntax highlighting comes from Claude Code's own highlighter (the `Code` element, the
14+
one the built-in `/diff` pane uses), with the language taken from the file's extension
15+
16+
Errors, edits held for review, brand-new files and anything without a diff keep Claude
17+
Code's normal drawing.
18+
19+
## Requirements
20+
21+
- Claude Code **v2.1.287 or later** (the version that introduced mods; they are on by
22+
default). Check with `claude --version`.
23+
- Mods must be allowed to load: not started with `--safe-mode` or `--bare`, and not blocked
24+
by your organization's managed settings.
25+
26+
## Install
27+
28+
From a terminal:
29+
30+
```bash
31+
claude plugin marketplace add neteye-platform/cc-split-diff-view
32+
claude plugin install cc-split-diff-view@neteye-mods
33+
```
34+
35+
Or from inside a Claude Code session:
36+
37+
```text
38+
/plugin marketplace add neteye-platform/cc-split-diff-view
39+
/plugin install cc-split-diff-view@neteye-mods
40+
```
41+
42+
No download or build step is needed: Claude Code fetches the repository and loads the
43+
TypeScript directly. Start a new session (or run `/reload-plugins`), then ask Claude to
44+
edit a file.
45+
46+
To check it loaded, run `/plugin` and look for `cc-split-diff-view` under
47+
**Installed**.
48+
49+
## Update
50+
51+
Claude Code caches an installed plugin by version, so updates arrive when the `version` in
52+
`.claude-plugin/plugin.json` is bumped:
53+
54+
```bash
55+
claude plugin marketplace update neteye-mods
56+
```
57+
58+
## Uninstall
59+
60+
```bash
61+
claude plugin uninstall cc-split-diff-view@neteye-mods
62+
```
63+
64+
You can also turn it off without removing it from the **Installed** tab of `/plugin`.
65+
66+
## Try it for one session without installing
67+
68+
```bash
69+
git clone https://github.com/neteye-platform/cc-split-diff-view.git
70+
claude --plugin-dir ./cc-split-diff-view
71+
```
72+
73+
The mod hot-reloads when you save a file in that directory.
74+
75+
## Limitations
76+
77+
- Only `Edit` and `Write` are redrawn. The permission prompt is not a render site, so a
78+
mod cannot change it.
79+
- Each line is highlighted on its own, so something that spans lines (a block comment or a
80+
template string) can be colored as plain code when only part of it is in the diff.
81+
- Hooks stay active in the VS Code extension, `claude -p` and cloud sessions, but the
82+
interface part only draws in the terminal and the Desktop app's Code tab.
83+
- Mod events and render-site props can change between Claude Code releases. If the diff
84+
suddenly looks like the default again, run `claude plugin validate .` and check the
85+
debug log.

‎hooks/hooks.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"description": "Split diff view: Edit and Write diffs drawn in two columns",
3+
"modules": ["./register.ts"]
4+
}

‎hooks/register.ts‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import type { ElementTable, On } from 'claude-code'
2+
3+
import { splitRows } from './split-rows.js'
4+
import type { Cell, SplitRow } from './split-rows.js'
5+
6+
/** Tools whose result is a diff this mod redraws. */
7+
const TOOLS = ['Edit', 'Write']
8+
9+
/** Rows drawn before "… N more rows". */
10+
const MAX_ROWS = 200
11+
12+
type Kit = Pick<ElementTable<'terminal'>, 'Box' | 'Text' | 'Code'>
13+
14+
const isRecord = (value: unknown): value is Record<string, unknown> =>
15+
typeof value === 'object' && value !== null
16+
17+
/**
18+
* Draws the result block of an Edit or Write call as a split diff: removed
19+
* lines in the left column and added lines in the right, with line numbers.
20+
*
21+
* @param on the engine's registrar
22+
*/
23+
export function register(on: On) {
24+
on('ui.render', { component: 'ToolResult' }, async ($, e, next) => {
25+
const { tool, output, isErrored } = e.props
26+
if (!TOOLS.includes(tool) || isErrored || !isRecord(output)) return next(e)
27+
// An edit held for review wrote nothing, so there is no diff to show.
28+
if (output.staged === true) return next(e)
29+
30+
const diff = splitRows(output.structuredPatch)
31+
// No hunks (a new file, an identical write): leave Claude Code's drawing alone.
32+
if (diff === null) return next(e)
33+
34+
const kit: Kit = $.ui.resolve(e)
35+
const { Box, Text } = kit
36+
const path = typeof output.filePath === 'string' ? output.filePath : undefined
37+
38+
const shown = diff.rows.slice(0, MAX_ROWS)
39+
const hidden = diff.rows.length - shown.length
40+
41+
return Box({
42+
flexDirection: 'column',
43+
children: [
44+
// The built-in summary line, with its response marker.
45+
Box({
46+
flexDirection: 'row',
47+
children: [
48+
Text({ dimColor: true, children: [' ⎿ '] }),
49+
...summary(Text, diff.added, diff.removed),
50+
],
51+
}),
52+
// The rows sit in a frame under the summary text, as the built-in diff does.
53+
Box({
54+
flexDirection: 'column',
55+
marginLeft: 5,
56+
borderStyle: 'round',
57+
borderDimColor: true,
58+
children: [
59+
...shown.map(row => drawRow(kit, row, path)),
60+
hidden > 0 ? Text({ dimColor: true, children: ['… ' + hidden + ' more rows'] }) : null,
61+
],
62+
}),
63+
],
64+
})
65+
})
66+
}
67+
68+
function drawRow(kit: Kit, row: SplitRow, path: string | undefined) {
69+
const { Box, Text } = kit
70+
if (row.kind === 'gap') {
71+
// The label sits in both columns, so a wide screen doesn't leave the right one blank.
72+
const label = () => Text({ dimColor: true, children: ['⋯ ' + row.hidden + ' unchanged lines'] })
73+
// A blank line above and below sets the label apart; an empty 1-wide Box takes the separator's place so the columns line up.
74+
const column = { width: '50%', flexShrink: 1, paddingY: 1 } as const
75+
return Box({
76+
flexDirection: 'row',
77+
children: [Box({ ...column, children: [label()] }), Box({ width: 1, flexShrink: 0 }), Box({ ...column, children: [label()] })],
78+
})
79+
}
80+
if (row.changed && row.left !== null && row.right !== null) {
81+
return drawPair(kit, row.left, row.right, path)
82+
}
83+
return Box({
84+
flexDirection: 'row',
85+
alignItems: 'stretch',
86+
children: [
87+
drawCell(kit, row.left, row.changed ? 'removed' : 'same', path),
88+
separator(kit),
89+
drawCell(kit, row.right, row.changed ? 'added' : 'same', path),
90+
],
91+
})
92+
}
93+
94+
function drawCell(
95+
{ Box, Text, Code }: Kit,
96+
cell: Cell | null,
97+
kind: 'removed' | 'added' | 'same',
98+
path: string | undefined,
99+
) {
100+
const column = { width: '50%', flexShrink: 1, flexDirection: 'row' } as const
101+
102+
// Nothing on this side of the row: keep the column, draw nothing in it.
103+
if (cell === null) return Box({ ...column, children: [Text({ children: [' '] })] })
104+
105+
// One-line hunk through the engine's diff renderer, so gutter, marker and
106+
// add/remove colors are exactly the built-in diff's.
107+
const n = cell.no
108+
const header =
109+
kind === 'removed' ? `@@ -${n},1 +${n},0 @@` : kind === 'added' ? `@@ -${n},0 +${n},1 @@` : `@@ -${n},1 +${n},1 @@`
110+
const sign = kind === 'removed' ? '-' : kind === 'added' ? '+' : ' '
111+
112+
return Box({
113+
...column,
114+
children: [
115+
Code({
116+
source: header + '\n' + sign + cell.text,
117+
format: 'diff',
118+
wrap: 'wrap',
119+
...(path === undefined ? {} : { path }),
120+
}),
121+
],
122+
})
123+
}
124+
125+
/**
126+
* A removed line next to the line that replaced it. The engine draws the two as
127+
* one hunk, which is what makes it tint the words that changed; each column
128+
* then shows its own line of that hunk. Long lines are cut with an ellipsis, as
129+
* a column clipped to one row cannot wrap.
130+
*/
131+
function drawPair(kit: Kit, left: Cell, right: Cell, path: string | undefined) {
132+
const { Box, Code } = kit
133+
const hunk = () =>
134+
Code({
135+
source: `@@ -${left.no},1 +${right.no},1 @@\n-${left.text}\n+${right.text}`,
136+
format: 'diff',
137+
wrap: 'truncate-end',
138+
...(path === undefined ? {} : { path }),
139+
})
140+
const column = { width: '50%', flexShrink: 1, height: 1, overflow: 'hidden' } as const
141+
return Box({
142+
flexDirection: 'row',
143+
children: [
144+
Box({ ...column, children: [hunk()] }),
145+
separator(kit),
146+
Box({
147+
...column,
148+
children: [Box({ position: 'absolute', top: -1, left: 0, right: 0, children: [hunk()] })],
149+
}),
150+
],
151+
})
152+
}
153+
154+
/**
155+
* A dim vertical rule between the columns. It stretches to the row's height
156+
* (wrapped lines make rows taller), so it draws a tall line out of flow and
157+
* lets the clipped box cut it to the row.
158+
*/
159+
function separator({ Box, Text }: Kit) {
160+
return Box({
161+
width: 1,
162+
flexShrink: 0,
163+
overflow: 'hidden',
164+
children: [
165+
Box({
166+
position: 'absolute',
167+
top: 0,
168+
bottom: 0,
169+
flexDirection: 'column',
170+
children: Array.from({ length: 20 }, () => Text({ dimColor: true, children: ['│'] })),
171+
}),
172+
],
173+
})
174+
}
175+
176+
/** "Added 3 lines, removed 1 line", the counts bold, as the built-in diff words it. */
177+
function summary(Text: Kit['Text'], added: number, removed: number) {
178+
const noun = (n: number) => (n > 1 ? ' lines' : ' line')
179+
const parts: ReturnType<Kit['Text']>[] = []
180+
if (added > 0) {
181+
parts.push(Text({ children: ['Added '] }), Text({ bold: true, children: [String(added)] }))
182+
parts.push(Text({ children: [noun(added) + (removed > 0 ? ', ' : '')] }))
183+
}
184+
if (removed > 0) {
185+
parts.push(Text({ children: [added === 0 ? 'Removed ' : 'removed '] }))
186+
parts.push(Text({ bold: true, children: [String(removed)] }), Text({ children: [noun(removed)] }))
187+
}
188+
return parts
189+
}

0 commit comments

Comments
 (0)