pi-diff-review 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/extensions/review.ts +841 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Colton Madden
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<img width="1400" height="225" alt="pi-diff-review" src="https://github.com/user-attachments/assets/049b2df9-ec38-4a9a-a824-8d682e77dae1" />
|
|
2
|
+
|
|
3
|
+
# pi-diff-review
|
|
4
|
+
|
|
5
|
+
Easily provide code reviews directly within [pi](https://pi.dev/).
|
|
6
|
+
|
|
7
|
+
<img width="1947" height="1103" alt="image" src="https://github.com/user-attachments/assets/3b1e1c51-4c77-4430-8915-1f7d481b64cb" />
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pi install https://github.com/cmpadden/pi-diff-review
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- `/diff` reviews the current unstaged `git diff`
|
|
18
|
+
- `/diff <git-diff-args>` passes arguments through to `git diff` (for example `/diff main...HEAD`)
|
|
19
|
+
- `j/k` or arrow keys to move
|
|
20
|
+
- `J/K` to extend a highlighted selection into a comment range
|
|
21
|
+
- `esc` clears the active selection, or exits review when no selection is active
|
|
22
|
+
- `n/p` to jump hunks
|
|
23
|
+
- `c` to add or edit a comment for the current line or selected range
|
|
24
|
+
- `x` to delete a comment for the current line or selected range
|
|
25
|
+
- `R` to submit comments back to pi
|
|
26
|
+
- `q` to exit
|
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionCommandContext,
|
|
5
|
+
Theme,
|
|
6
|
+
} from "@mariozechner/pi-coding-agent";
|
|
7
|
+
import {
|
|
8
|
+
Editor,
|
|
9
|
+
matchesKey,
|
|
10
|
+
truncateToWidth,
|
|
11
|
+
visibleWidth,
|
|
12
|
+
wrapTextWithAnsi,
|
|
13
|
+
} from "@mariozechner/pi-tui";
|
|
14
|
+
|
|
15
|
+
type DiffLineKind = "meta" | "hunk" | "context" | "add" | "remove";
|
|
16
|
+
|
|
17
|
+
type ReviewComment = {
|
|
18
|
+
id: string;
|
|
19
|
+
filePath: string;
|
|
20
|
+
text: string;
|
|
21
|
+
startLineId: string;
|
|
22
|
+
endLineId: string;
|
|
23
|
+
startOldLineNumber?: number;
|
|
24
|
+
startNewLineNumber?: number;
|
|
25
|
+
endOldLineNumber?: number;
|
|
26
|
+
endNewLineNumber?: number;
|
|
27
|
+
lineText: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type ReviewLine = {
|
|
31
|
+
id: string;
|
|
32
|
+
kind: DiffLineKind;
|
|
33
|
+
text: string;
|
|
34
|
+
filePath?: string;
|
|
35
|
+
oldLineNumber?: number;
|
|
36
|
+
newLineNumber?: number;
|
|
37
|
+
commentable: boolean;
|
|
38
|
+
hunkLabel?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type ReviewResult =
|
|
42
|
+
| { action: "submit"; comments: ReviewComment[] }
|
|
43
|
+
| { action: "cancel" };
|
|
44
|
+
|
|
45
|
+
type SelectionBounds = {
|
|
46
|
+
start: number;
|
|
47
|
+
end: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type DiffSource = {
|
|
51
|
+
label: string;
|
|
52
|
+
promptLabel: string;
|
|
53
|
+
args: string[];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function parseDiffSource(args: string): DiffSource {
|
|
57
|
+
const trimmed = args.trim();
|
|
58
|
+
if (!trimmed) {
|
|
59
|
+
return {
|
|
60
|
+
label: "unstaged git diff",
|
|
61
|
+
promptLabel: "the current unstaged git diff",
|
|
62
|
+
args: [],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const gitArgs = trimmed.split(/\s+/).filter(Boolean);
|
|
67
|
+
return {
|
|
68
|
+
label: `git diff ${trimmed}`,
|
|
69
|
+
promptLabel: `\`git diff ${trimmed}\``,
|
|
70
|
+
args: gitArgs,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getDiff(cwd: string, source: DiffSource): string {
|
|
75
|
+
return execFileSync("git", ["diff", "--no-color", "--unified=3", ...source.args], {
|
|
76
|
+
cwd,
|
|
77
|
+
encoding: "utf8",
|
|
78
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseDiff(diffText: string): ReviewLine[] {
|
|
83
|
+
const lines = diffText.split("\n");
|
|
84
|
+
const parsed: ReviewLine[] = [];
|
|
85
|
+
|
|
86
|
+
let currentFile: string | undefined;
|
|
87
|
+
let previousFile: string | undefined;
|
|
88
|
+
let nextFile: string | undefined;
|
|
89
|
+
let currentHunk: string | undefined;
|
|
90
|
+
let oldLine = 0;
|
|
91
|
+
let newLine = 0;
|
|
92
|
+
let lineIndex = 0;
|
|
93
|
+
|
|
94
|
+
for (const raw of lines) {
|
|
95
|
+
if (raw.startsWith("diff --git ")) {
|
|
96
|
+
const match = raw.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
|
97
|
+
previousFile = match?.[1];
|
|
98
|
+
nextFile = match?.[2];
|
|
99
|
+
currentFile = nextFile ?? previousFile;
|
|
100
|
+
currentHunk = undefined;
|
|
101
|
+
parsed.push({
|
|
102
|
+
id: `line-${lineIndex++}`,
|
|
103
|
+
kind: "meta",
|
|
104
|
+
text: raw,
|
|
105
|
+
filePath: currentFile,
|
|
106
|
+
commentable: false,
|
|
107
|
+
});
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (raw.startsWith("--- ")) {
|
|
112
|
+
previousFile =
|
|
113
|
+
raw === "--- /dev/null"
|
|
114
|
+
? undefined
|
|
115
|
+
: raw.replace(/^--- a\//, "").replace(/^--- /, "");
|
|
116
|
+
parsed.push({
|
|
117
|
+
id: `line-${lineIndex++}`,
|
|
118
|
+
kind: "meta",
|
|
119
|
+
text: raw,
|
|
120
|
+
filePath: currentFile,
|
|
121
|
+
commentable: false,
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (raw.startsWith("+++ ")) {
|
|
127
|
+
nextFile =
|
|
128
|
+
raw === "+++ /dev/null"
|
|
129
|
+
? undefined
|
|
130
|
+
: raw.replace(/^\+\+\+ b\//, "").replace(/^\+\+\+ /, "");
|
|
131
|
+
currentFile = nextFile ?? previousFile;
|
|
132
|
+
parsed.push({
|
|
133
|
+
id: `line-${lineIndex++}`,
|
|
134
|
+
kind: "meta",
|
|
135
|
+
text: raw,
|
|
136
|
+
filePath: currentFile,
|
|
137
|
+
commentable: false,
|
|
138
|
+
});
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (raw.startsWith("@@")) {
|
|
143
|
+
const match = raw.match(
|
|
144
|
+
/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$/,
|
|
145
|
+
);
|
|
146
|
+
if (match) {
|
|
147
|
+
oldLine = Number(match[1]);
|
|
148
|
+
newLine = Number(match[3]);
|
|
149
|
+
}
|
|
150
|
+
currentHunk = raw;
|
|
151
|
+
parsed.push({
|
|
152
|
+
id: `line-${lineIndex++}`,
|
|
153
|
+
kind: "hunk",
|
|
154
|
+
text: raw,
|
|
155
|
+
filePath: currentFile,
|
|
156
|
+
commentable: false,
|
|
157
|
+
hunkLabel: currentHunk,
|
|
158
|
+
});
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (raw.startsWith("+") && !raw.startsWith("+++")) {
|
|
163
|
+
parsed.push({
|
|
164
|
+
id: `line-${lineIndex++}`,
|
|
165
|
+
kind: "add",
|
|
166
|
+
text: raw,
|
|
167
|
+
filePath: currentFile,
|
|
168
|
+
newLineNumber: newLine,
|
|
169
|
+
commentable: Boolean(currentFile),
|
|
170
|
+
hunkLabel: currentHunk,
|
|
171
|
+
});
|
|
172
|
+
newLine++;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (raw.startsWith("-") && !raw.startsWith("---")) {
|
|
177
|
+
parsed.push({
|
|
178
|
+
id: `line-${lineIndex++}`,
|
|
179
|
+
kind: "remove",
|
|
180
|
+
text: raw,
|
|
181
|
+
filePath: currentFile,
|
|
182
|
+
oldLineNumber: oldLine,
|
|
183
|
+
commentable: Boolean(currentFile),
|
|
184
|
+
hunkLabel: currentHunk,
|
|
185
|
+
});
|
|
186
|
+
oldLine++;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (raw.startsWith(" ")) {
|
|
191
|
+
parsed.push({
|
|
192
|
+
id: `line-${lineIndex++}`,
|
|
193
|
+
kind: "context",
|
|
194
|
+
text: raw,
|
|
195
|
+
filePath: currentFile,
|
|
196
|
+
oldLineNumber: oldLine,
|
|
197
|
+
newLineNumber: newLine,
|
|
198
|
+
commentable: Boolean(currentFile),
|
|
199
|
+
hunkLabel: currentHunk,
|
|
200
|
+
});
|
|
201
|
+
oldLine++;
|
|
202
|
+
newLine++;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
parsed.push({
|
|
207
|
+
id: `line-${lineIndex++}`,
|
|
208
|
+
kind: "meta",
|
|
209
|
+
text: raw,
|
|
210
|
+
filePath: currentFile,
|
|
211
|
+
commentable: false,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return parsed;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function formatLocation(line: {
|
|
219
|
+
filePath?: string;
|
|
220
|
+
oldLineNumber?: number;
|
|
221
|
+
newLineNumber?: number;
|
|
222
|
+
}): string {
|
|
223
|
+
const file = line.filePath ?? "(unknown file)";
|
|
224
|
+
if (line.oldLineNumber != null && line.newLineNumber != null) {
|
|
225
|
+
if (line.oldLineNumber === line.newLineNumber) {
|
|
226
|
+
return `${file}:${line.newLineNumber}`;
|
|
227
|
+
}
|
|
228
|
+
return `${file}:old:${line.oldLineNumber}/new:${line.newLineNumber}`;
|
|
229
|
+
}
|
|
230
|
+
if (line.newLineNumber != null) return `${file}:new:${line.newLineNumber}`;
|
|
231
|
+
if (line.oldLineNumber != null) return `${file}:old:${line.oldLineNumber}`;
|
|
232
|
+
return file;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function formatCommentLocation(comment: ReviewComment): string {
|
|
236
|
+
const start = formatLocation({
|
|
237
|
+
filePath: comment.filePath,
|
|
238
|
+
oldLineNumber: comment.startOldLineNumber,
|
|
239
|
+
newLineNumber: comment.startNewLineNumber,
|
|
240
|
+
});
|
|
241
|
+
const end = formatLocation({
|
|
242
|
+
filePath: comment.filePath,
|
|
243
|
+
oldLineNumber: comment.endOldLineNumber,
|
|
244
|
+
newLineNumber: comment.endNewLineNumber,
|
|
245
|
+
});
|
|
246
|
+
return start === end ? start : `${start} -> ${end}`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function buildReviewPrompt(
|
|
250
|
+
comments: ReviewComment[],
|
|
251
|
+
promptLabel: string,
|
|
252
|
+
): string {
|
|
253
|
+
const body = comments
|
|
254
|
+
.map((comment) => {
|
|
255
|
+
const location = formatCommentLocation(comment);
|
|
256
|
+
const excerpt = comment.lineText.trim()
|
|
257
|
+
? `\n Excerpt:\n\n\`\`\`diff\n${comment.lineText}\n\`\`\``
|
|
258
|
+
: "";
|
|
259
|
+
return `- \`${location}\` — ${comment.text}${excerpt}`;
|
|
260
|
+
})
|
|
261
|
+
.join("\n");
|
|
262
|
+
|
|
263
|
+
return `Address this local code review feedback for ${promptLabel}.\n\n## Review comments\n${body}\n\nPlease apply the feedback and summarize what changed.`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function padToWidth(text: string, width: number): string {
|
|
267
|
+
const visible = visibleWidth(text);
|
|
268
|
+
if (visible >= width) return truncateToWidth(text, width);
|
|
269
|
+
return text + " ".repeat(width - visible);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function lineNumberCell(value?: number): string {
|
|
273
|
+
return value == null ? " " : String(value).padStart(4, " ");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
class ReviewComponent {
|
|
277
|
+
private selected = 0;
|
|
278
|
+
private scrollTop = 0;
|
|
279
|
+
private editMode = false;
|
|
280
|
+
private editingCommentKey?: string;
|
|
281
|
+
private selectionAnchor?: number;
|
|
282
|
+
private editor: Editor;
|
|
283
|
+
|
|
284
|
+
constructor(
|
|
285
|
+
private tui: {
|
|
286
|
+
requestRender: (full?: boolean) => void;
|
|
287
|
+
terminal?: { rows: number; columns: number };
|
|
288
|
+
},
|
|
289
|
+
private theme: Theme,
|
|
290
|
+
private title: string,
|
|
291
|
+
private lines: ReviewLine[],
|
|
292
|
+
private comments: Map<string, ReviewComment>,
|
|
293
|
+
private done: (result: ReviewResult) => void,
|
|
294
|
+
) {
|
|
295
|
+
const firstCommentable = this.lines.findIndex((line) => line.commentable);
|
|
296
|
+
this.selected = firstCommentable >= 0 ? firstCommentable : 0;
|
|
297
|
+
|
|
298
|
+
this.editor = new Editor(tui as never, {
|
|
299
|
+
borderColor: (s) => theme.fg("accent", s),
|
|
300
|
+
selectList: {
|
|
301
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
302
|
+
selectedText: (t) => theme.fg("accent", t),
|
|
303
|
+
description: (t) => theme.fg("muted", t),
|
|
304
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
305
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
this.editor.onSubmit = (value) => {
|
|
310
|
+
const selection = this.getActiveCommentSelection();
|
|
311
|
+
if (!selection) {
|
|
312
|
+
this.exitEditMode();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const trimmed = value.trim();
|
|
317
|
+
const key = this.getSelectionKey(selection.start, selection.end);
|
|
318
|
+
if (!trimmed) {
|
|
319
|
+
this.comments.delete(key);
|
|
320
|
+
} else {
|
|
321
|
+
this.comments.set(key, this.buildCommentFromSelection(selection, trimmed));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
this.exitEditMode();
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
handleInput(data: string): void {
|
|
329
|
+
if (this.editMode) {
|
|
330
|
+
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
|
|
331
|
+
this.exitEditMode();
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
this.editor.handleInput(data);
|
|
335
|
+
this.tui.requestRender();
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (matchesKey(data, "escape")) {
|
|
340
|
+
if (this.hasSelection()) {
|
|
341
|
+
this.clearSelection();
|
|
342
|
+
} else {
|
|
343
|
+
this.done({ action: "cancel" });
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (data === "q") {
|
|
348
|
+
this.done({ action: "cancel" });
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (data === "j" || matchesKey(data, "down")) {
|
|
352
|
+
this.move(1);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (data === "k" || matchesKey(data, "up")) {
|
|
356
|
+
this.move(-1);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (data === "J") {
|
|
360
|
+
this.extendSelection(1);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (data === "K") {
|
|
364
|
+
this.extendSelection(-1);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (data === "n") {
|
|
368
|
+
this.jumpHunk(1);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (data === "p") {
|
|
372
|
+
this.jumpHunk(-1);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (data === "x") {
|
|
376
|
+
this.deleteComment();
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (data === "c") {
|
|
380
|
+
this.startEditMode();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (data === "R") {
|
|
384
|
+
const comments = [...this.comments.values()].sort((a, b) =>
|
|
385
|
+
a.id.localeCompare(b.id),
|
|
386
|
+
);
|
|
387
|
+
if (comments.length === 0) return;
|
|
388
|
+
this.done({ action: "submit", comments });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
render(width: number): string[] {
|
|
393
|
+
const terminalRows = this.tui.terminal?.rows ?? 24;
|
|
394
|
+
const headerHeight = 3;
|
|
395
|
+
const footerHeight = 2;
|
|
396
|
+
const viewportHeight = Math.max(
|
|
397
|
+
6,
|
|
398
|
+
terminalRows - headerHeight - footerHeight,
|
|
399
|
+
);
|
|
400
|
+
this.ensureScroll(viewportHeight);
|
|
401
|
+
|
|
402
|
+
const rightWidth = Math.max(28, Math.floor(width * 0.34));
|
|
403
|
+
const separatorWidth = 3;
|
|
404
|
+
const leftWidth = Math.max(30, width - rightWidth - separatorWidth);
|
|
405
|
+
|
|
406
|
+
const selectedLine = this.lines[this.selected];
|
|
407
|
+
const rightPane = this.renderRightPane(
|
|
408
|
+
rightWidth,
|
|
409
|
+
viewportHeight,
|
|
410
|
+
selectedLine,
|
|
411
|
+
);
|
|
412
|
+
const output: string[] = [];
|
|
413
|
+
|
|
414
|
+
output.push(
|
|
415
|
+
truncateToWidth(
|
|
416
|
+
this.theme.fg(
|
|
417
|
+
"accent",
|
|
418
|
+
this.theme.bold(`Local Review: ${this.title}`),
|
|
419
|
+
),
|
|
420
|
+
width,
|
|
421
|
+
),
|
|
422
|
+
);
|
|
423
|
+
output.push(
|
|
424
|
+
truncateToWidth(
|
|
425
|
+
this.theme.fg(
|
|
426
|
+
"dim",
|
|
427
|
+
this.editMode
|
|
428
|
+
? `${this.lines.length} lines • ${this.comments.size} comments • editing comment • Enter save • Esc/Ctrl+C cancel`
|
|
429
|
+
: this.hasSelection()
|
|
430
|
+
? `${this.lines.length} lines • ${this.comments.size} comments • J/K extend • Esc clear selection • c comment range • R submit`
|
|
431
|
+
: `${this.lines.length} lines • ${this.comments.size} comments • j/k move • J/K extend • c comment • x delete • n/p hunk • R submit • q quit`,
|
|
432
|
+
),
|
|
433
|
+
width,
|
|
434
|
+
),
|
|
435
|
+
);
|
|
436
|
+
output.push(this.theme.fg("border", "─".repeat(width)));
|
|
437
|
+
|
|
438
|
+
const selection = this.getSelectionBounds();
|
|
439
|
+
for (let row = 0; row < viewportHeight; row++) {
|
|
440
|
+
const index = this.scrollTop + row;
|
|
441
|
+
const line = this.lines[index];
|
|
442
|
+
const left = line
|
|
443
|
+
? this.renderDiffLine(
|
|
444
|
+
line,
|
|
445
|
+
index,
|
|
446
|
+
leftWidth,
|
|
447
|
+
index === this.selected,
|
|
448
|
+
selection,
|
|
449
|
+
)
|
|
450
|
+
: " ".repeat(leftWidth);
|
|
451
|
+
const right = rightPane[row] ?? " ".repeat(rightWidth);
|
|
452
|
+
const combined = `${padToWidth(left, leftWidth)}${this.theme.fg("borderMuted", " │ ")}${padToWidth(right, rightWidth)}`;
|
|
453
|
+
output.push(truncateToWidth(combined, width));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
output.push(this.theme.fg("border", "─".repeat(width)));
|
|
457
|
+
output.push(
|
|
458
|
+
truncateToWidth(
|
|
459
|
+
this.theme.fg("muted", this.getFooterText(selectedLine)),
|
|
460
|
+
width,
|
|
461
|
+
),
|
|
462
|
+
);
|
|
463
|
+
return output;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
invalidate(): void {}
|
|
467
|
+
|
|
468
|
+
private move(delta: number): void {
|
|
469
|
+
this.selected = Math.max(
|
|
470
|
+
0,
|
|
471
|
+
Math.min(this.lines.length - 1, this.selected + delta),
|
|
472
|
+
);
|
|
473
|
+
this.tui.requestRender();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private extendSelection(delta: number): void {
|
|
477
|
+
if (this.selectionAnchor == null) {
|
|
478
|
+
this.selectionAnchor = this.selected;
|
|
479
|
+
}
|
|
480
|
+
this.selected = Math.max(
|
|
481
|
+
0,
|
|
482
|
+
Math.min(this.lines.length - 1, this.selected + delta),
|
|
483
|
+
);
|
|
484
|
+
this.tui.requestRender();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private clearSelection(): void {
|
|
488
|
+
this.selectionAnchor = undefined;
|
|
489
|
+
this.tui.requestRender();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private hasSelection(): boolean {
|
|
493
|
+
return this.selectionAnchor != null && this.selectionAnchor !== this.selected;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
private getSelectionBounds(): SelectionBounds | undefined {
|
|
497
|
+
if (this.selectionAnchor == null) return undefined;
|
|
498
|
+
return {
|
|
499
|
+
start: Math.min(this.selectionAnchor, this.selected),
|
|
500
|
+
end: Math.max(this.selectionAnchor, this.selected),
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
private getActiveCommentSelection(): SelectionBounds | undefined {
|
|
505
|
+
const selection = this.getSelectionBounds();
|
|
506
|
+
if (selection) return selection;
|
|
507
|
+
const line = this.lines[this.selected];
|
|
508
|
+
if (!line?.commentable) return undefined;
|
|
509
|
+
return { start: this.selected, end: this.selected };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
private getSelectionKey(start: number, end: number): string {
|
|
513
|
+
return `${this.lines[start]?.id ?? start}:${this.lines[end]?.id ?? end}`;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
private getCommentForSelection(
|
|
517
|
+
selection: SelectionBounds | undefined,
|
|
518
|
+
): ReviewComment | undefined {
|
|
519
|
+
if (!selection) return undefined;
|
|
520
|
+
return this.comments.get(this.getSelectionKey(selection.start, selection.end));
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
private getCommentKeysForLine(index: number): string[] {
|
|
524
|
+
const line = this.lines[index];
|
|
525
|
+
if (!line) return [];
|
|
526
|
+
return [...this.comments.entries()]
|
|
527
|
+
.filter(([, comment]) => {
|
|
528
|
+
const start = this.lines.findIndex((item) => item.id === comment.startLineId);
|
|
529
|
+
const end = this.lines.findIndex((item) => item.id === comment.endLineId);
|
|
530
|
+
return start !== -1 && end !== -1 && index >= start && index <= end;
|
|
531
|
+
})
|
|
532
|
+
.map(([key]) => key);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
private buildCommentFromSelection(
|
|
536
|
+
selection: SelectionBounds,
|
|
537
|
+
text: string,
|
|
538
|
+
): ReviewComment {
|
|
539
|
+
const startLine = this.lines[selection.start]!;
|
|
540
|
+
const endLine = this.lines[selection.end]!;
|
|
541
|
+
const excerpt = this.lines
|
|
542
|
+
.slice(selection.start, selection.end + 1)
|
|
543
|
+
.map((line) => line.text)
|
|
544
|
+
.join("\n");
|
|
545
|
+
return {
|
|
546
|
+
id: this.getSelectionKey(selection.start, selection.end),
|
|
547
|
+
filePath: startLine.filePath ?? endLine.filePath ?? "(unknown file)",
|
|
548
|
+
text,
|
|
549
|
+
startLineId: startLine.id,
|
|
550
|
+
endLineId: endLine.id,
|
|
551
|
+
startOldLineNumber: startLine.oldLineNumber,
|
|
552
|
+
startNewLineNumber: startLine.newLineNumber,
|
|
553
|
+
endOldLineNumber: endLine.oldLineNumber,
|
|
554
|
+
endNewLineNumber: endLine.newLineNumber,
|
|
555
|
+
lineText: excerpt,
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
private getFooterText(selectedLine?: ReviewLine): string {
|
|
560
|
+
const selection = this.getSelectionBounds();
|
|
561
|
+
if (selection) {
|
|
562
|
+
const count = selection.end - selection.start + 1;
|
|
563
|
+
const startLine = this.lines[selection.start]!;
|
|
564
|
+
const endLine = this.lines[selection.end]!;
|
|
565
|
+
return `Selected ${count} lines: ${formatLocation(startLine)} -> ${formatLocation(endLine)}`;
|
|
566
|
+
}
|
|
567
|
+
return `Selected: ${selectedLine ? formatLocation(selectedLine) : "(no selection)"}`;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
private jumpHunk(direction: 1 | -1): void {
|
|
571
|
+
let index = this.selected + direction;
|
|
572
|
+
while (index >= 0 && index < this.lines.length) {
|
|
573
|
+
if (this.lines[index]?.kind === "hunk") {
|
|
574
|
+
this.selected = index;
|
|
575
|
+
this.tui.requestRender();
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
index += direction;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private deleteComment(): void {
|
|
583
|
+
const selection = this.getActiveCommentSelection();
|
|
584
|
+
if (!selection) return;
|
|
585
|
+
this.comments.delete(this.getSelectionKey(selection.start, selection.end));
|
|
586
|
+
this.tui.requestRender();
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
private startEditMode(): void {
|
|
590
|
+
const selection = this.getActiveCommentSelection();
|
|
591
|
+
if (!selection) return;
|
|
592
|
+
const startLine = this.lines[selection.start];
|
|
593
|
+
const endLine = this.lines[selection.end];
|
|
594
|
+
if (!startLine?.commentable || !endLine?.commentable) return;
|
|
595
|
+
if (startLine.filePath !== endLine.filePath) return;
|
|
596
|
+
|
|
597
|
+
const existing = this.getCommentForSelection(selection);
|
|
598
|
+
this.editMode = true;
|
|
599
|
+
this.editingCommentKey = this.getSelectionKey(selection.start, selection.end);
|
|
600
|
+
this.editor.setText(existing?.text ?? "");
|
|
601
|
+
this.tui.requestRender(true);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
private exitEditMode(): void {
|
|
605
|
+
this.editMode = false;
|
|
606
|
+
this.editingCommentKey = undefined;
|
|
607
|
+
this.editor.setText("");
|
|
608
|
+
this.tui.requestRender(true);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
private ensureScroll(viewportHeight: number): void {
|
|
612
|
+
if (this.selected < this.scrollTop) {
|
|
613
|
+
this.scrollTop = this.selected;
|
|
614
|
+
}
|
|
615
|
+
if (this.selected >= this.scrollTop + viewportHeight) {
|
|
616
|
+
this.scrollTop = this.selected - viewportHeight + 1;
|
|
617
|
+
}
|
|
618
|
+
this.scrollTop = Math.max(
|
|
619
|
+
0,
|
|
620
|
+
Math.min(this.scrollTop, Math.max(0, this.lines.length - viewportHeight)),
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
private renderDiffLine(
|
|
625
|
+
line: ReviewLine,
|
|
626
|
+
index: number,
|
|
627
|
+
width: number,
|
|
628
|
+
selected: boolean,
|
|
629
|
+
selection?: SelectionBounds,
|
|
630
|
+
): string {
|
|
631
|
+
const hasComment = this.getCommentKeysForLine(index).length > 0;
|
|
632
|
+
const commentMark = hasComment ? this.theme.fg("warning", "●") : " ";
|
|
633
|
+
const numbers = `${lineNumberCell(line.oldLineNumber)} ${lineNumberCell(line.newLineNumber)}`;
|
|
634
|
+
const raw = `${commentMark} ${numbers} ${line.text}`;
|
|
635
|
+
|
|
636
|
+
let styled: string;
|
|
637
|
+
switch (line.kind) {
|
|
638
|
+
case "add":
|
|
639
|
+
styled = this.theme.fg("toolDiffAdded", raw);
|
|
640
|
+
break;
|
|
641
|
+
case "remove":
|
|
642
|
+
styled = this.theme.fg("toolDiffRemoved", raw);
|
|
643
|
+
break;
|
|
644
|
+
case "context":
|
|
645
|
+
styled = this.theme.fg("toolDiffContext", raw);
|
|
646
|
+
break;
|
|
647
|
+
case "hunk":
|
|
648
|
+
styled = this.theme.fg("accent", raw);
|
|
649
|
+
break;
|
|
650
|
+
default:
|
|
651
|
+
styled = this.theme.fg("muted", raw);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
styled = truncateToWidth(styled, width);
|
|
655
|
+
const inSelection =
|
|
656
|
+
selection != null && index >= selection.start && index <= selection.end;
|
|
657
|
+
if (selected || inSelection) {
|
|
658
|
+
return this.theme.bg("selectedBg", padToWidth(styled, width));
|
|
659
|
+
}
|
|
660
|
+
return styled;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
private renderRightPane(
|
|
664
|
+
width: number,
|
|
665
|
+
height: number,
|
|
666
|
+
selectedLine?: ReviewLine,
|
|
667
|
+
): string[] {
|
|
668
|
+
const lines: string[] = [];
|
|
669
|
+
const title = this.theme.fg("accent", this.theme.bold("Comments"));
|
|
670
|
+
const selection = this.getActiveCommentSelection();
|
|
671
|
+
const currentComment = this.getCommentForSelection(selection);
|
|
672
|
+
|
|
673
|
+
lines.push(truncateToWidth(title, width));
|
|
674
|
+
lines.push(
|
|
675
|
+
truncateToWidth(
|
|
676
|
+
this.theme.fg(
|
|
677
|
+
"dim",
|
|
678
|
+
selection
|
|
679
|
+
? this.getFooterText(selectedLine)
|
|
680
|
+
: selectedLine
|
|
681
|
+
? formatLocation(selectedLine)
|
|
682
|
+
: "No selection",
|
|
683
|
+
),
|
|
684
|
+
width,
|
|
685
|
+
),
|
|
686
|
+
);
|
|
687
|
+
lines.push("");
|
|
688
|
+
|
|
689
|
+
if (!selectedLine) {
|
|
690
|
+
lines.push(
|
|
691
|
+
...wrapTextWithAnsi(
|
|
692
|
+
this.theme.fg("muted", "No diff lines available."),
|
|
693
|
+
width,
|
|
694
|
+
),
|
|
695
|
+
);
|
|
696
|
+
return lines.slice(0, height);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
if (!selection) {
|
|
700
|
+
lines.push(
|
|
701
|
+
...wrapTextWithAnsi(
|
|
702
|
+
this.theme.fg(
|
|
703
|
+
"muted",
|
|
704
|
+
"Move to a diff line and press c to add a comment.",
|
|
705
|
+
),
|
|
706
|
+
width,
|
|
707
|
+
),
|
|
708
|
+
);
|
|
709
|
+
return lines.slice(0, height);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (this.editMode && currentComment?.id === this.editingCommentKey) {
|
|
713
|
+
lines.push(
|
|
714
|
+
...wrapTextWithAnsi(
|
|
715
|
+
this.theme.fg(
|
|
716
|
+
"dim",
|
|
717
|
+
"Editing comment. Enter saves. Esc or Ctrl+C cancels.",
|
|
718
|
+
),
|
|
719
|
+
width,
|
|
720
|
+
),
|
|
721
|
+
);
|
|
722
|
+
lines.push("");
|
|
723
|
+
for (const line of this.editor.render(Math.max(10, width))) {
|
|
724
|
+
lines.push(truncateToWidth(line, width));
|
|
725
|
+
}
|
|
726
|
+
} else if (this.editMode && this.editingCommentKey) {
|
|
727
|
+
lines.push(
|
|
728
|
+
...wrapTextWithAnsi(
|
|
729
|
+
this.theme.fg(
|
|
730
|
+
"dim",
|
|
731
|
+
"Editing comment. Enter saves. Esc or Ctrl+C cancels.",
|
|
732
|
+
),
|
|
733
|
+
width,
|
|
734
|
+
),
|
|
735
|
+
);
|
|
736
|
+
lines.push("");
|
|
737
|
+
for (const line of this.editor.render(Math.max(10, width))) {
|
|
738
|
+
lines.push(truncateToWidth(line, width));
|
|
739
|
+
}
|
|
740
|
+
} else if (currentComment) {
|
|
741
|
+
lines.push(
|
|
742
|
+
...wrapTextWithAnsi(this.theme.fg("text", currentComment.text), width),
|
|
743
|
+
);
|
|
744
|
+
lines.push("");
|
|
745
|
+
lines.push(
|
|
746
|
+
...wrapTextWithAnsi(
|
|
747
|
+
this.theme.fg("dim", "x deletes this comment • c edits"),
|
|
748
|
+
width,
|
|
749
|
+
),
|
|
750
|
+
);
|
|
751
|
+
} else {
|
|
752
|
+
lines.push(
|
|
753
|
+
...wrapTextWithAnsi(
|
|
754
|
+
this.theme.fg(
|
|
755
|
+
"muted",
|
|
756
|
+
this.hasSelection()
|
|
757
|
+
? "No comment on this range."
|
|
758
|
+
: "No comment on this line.",
|
|
759
|
+
),
|
|
760
|
+
width,
|
|
761
|
+
),
|
|
762
|
+
);
|
|
763
|
+
lines.push("");
|
|
764
|
+
lines.push(
|
|
765
|
+
...wrapTextWithAnsi(
|
|
766
|
+
this.theme.fg(
|
|
767
|
+
"dim",
|
|
768
|
+
this.hasSelection()
|
|
769
|
+
? "Press c to add a range comment."
|
|
770
|
+
: "Press c to add one. Use J/K to extend a range.",
|
|
771
|
+
),
|
|
772
|
+
width,
|
|
773
|
+
),
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
lines.push("");
|
|
778
|
+
lines.push(
|
|
779
|
+
truncateToWidth(
|
|
780
|
+
this.theme.fg("accent", this.theme.bold("Excerpt")),
|
|
781
|
+
width,
|
|
782
|
+
),
|
|
783
|
+
);
|
|
784
|
+
const excerpt = this.lines
|
|
785
|
+
.slice(selection.start, selection.end + 1)
|
|
786
|
+
.map((line) => line.text)
|
|
787
|
+
.join("\n");
|
|
788
|
+
lines.push(
|
|
789
|
+
...wrapTextWithAnsi(
|
|
790
|
+
this.theme.fg("toolDiffContext", excerpt || "(blank line)"),
|
|
791
|
+
width,
|
|
792
|
+
),
|
|
793
|
+
);
|
|
794
|
+
return lines.slice(0, height);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
export default function (pi: ExtensionAPI) {
|
|
799
|
+
pi.registerCommand("diff", {
|
|
800
|
+
description: "Review a git diff in a custom TUI (/diff [git diff args])",
|
|
801
|
+
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
802
|
+
const source = parseDiffSource(args);
|
|
803
|
+
let diffText: string;
|
|
804
|
+
try {
|
|
805
|
+
diffText = getDiff(ctx.cwd, source);
|
|
806
|
+
} catch (error) {
|
|
807
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
808
|
+
ctx.ui.notify(`Unable to read ${source.label}: ${message}`, "error");
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (!diffText.trim()) {
|
|
813
|
+
ctx.ui.notify(`No changes to review for ${source.label}.`, "info");
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const reviewLines = parseDiff(diffText);
|
|
818
|
+
const result = await ctx.ui.custom<ReviewResult>(
|
|
819
|
+
(tui, theme, _keybindings, done) => {
|
|
820
|
+
const comments = new Map<string, ReviewComment>();
|
|
821
|
+
return new ReviewComponent(
|
|
822
|
+
tui,
|
|
823
|
+
theme,
|
|
824
|
+
source.label,
|
|
825
|
+
reviewLines,
|
|
826
|
+
comments,
|
|
827
|
+
done,
|
|
828
|
+
);
|
|
829
|
+
},
|
|
830
|
+
);
|
|
831
|
+
|
|
832
|
+
if (!result || result.action !== "submit") return;
|
|
833
|
+
if (result.comments.length === 0) {
|
|
834
|
+
ctx.ui.notify("No review comments to send.", "info");
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
pi.sendUserMessage(buildReviewPrompt(result.comments, source.promptLabel));
|
|
839
|
+
},
|
|
840
|
+
});
|
|
841
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-diff-review",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local diff review TUI extension for pi",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/cmpadden/pi-diff-review.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/cmpadden/pi-diff-review#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/cmpadden/pi-diff-review/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"code-review",
|
|
16
|
+
"git-diff",
|
|
17
|
+
"pi",
|
|
18
|
+
"pi-coding-agent",
|
|
19
|
+
"pi-extension",
|
|
20
|
+
"pi-package"
|
|
21
|
+
],
|
|
22
|
+
"files": [
|
|
23
|
+
"extensions",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"format": "prettier --write ."
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"prettier": "^3.5.3"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
35
|
+
"@mariozechner/pi-tui": "*"
|
|
36
|
+
},
|
|
37
|
+
"pi": {
|
|
38
|
+
"extensions": [
|
|
39
|
+
"./extensions"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|