Skip to content

Commit 06da997

Browse files
authored
Fix extra blank lines in copied review comment diffs (#44)
1 parent 5a42b71 commit 06da997

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/__tests__/App.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ test('review comment markdown includes multi-line ranges', () => {
586586
expect(markdown).toContain('1. **src/range.ts** (New lines 1-2)');
587587
expect(markdown).toContain('+ 1 | const first = true;');
588588
expect(markdown).toContain('+ 2 | const second = true;');
589+
expect(markdown).toContain('+ 1 | const first = true;\n + 2 | const second = true;');
590+
expect(markdown).not.toContain('const first = true;\n\n +');
589591
});
590592

591593
test('escape discards empty review comments without confirmation', () => {

src/lib/review-comments.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export const getReviewCommentLineSelection = (comment: ReviewComment): CodeViewL
2727
type ReviewPatchRow = {
2828
additionLineNumber?: number;
2929
deletionLineNumber?: number;
30+
patchLineIndex: number;
31+
patchLines: ReadonlyArray<string>;
3032
prefix: '+' | '-' | ' ';
3133
side?: ReviewComment['side'];
32-
text: string;
3334
};
3435

3536
const matchesReviewPatchLine = (
@@ -140,6 +141,12 @@ const indentMarkdown = (value: string) =>
140141
.join('\n');
141142

142143
const formatReviewLineNumber = (lineNumber: number | string) => String(lineNumber).padStart(4);
144+
// @pierre/diffs keeps source line terminators; copied Markdown rows add their own separators.
145+
const trimReviewPatchLineTerminator = (line: string) =>
146+
line.endsWith('\r\n') ? line.slice(0, -2) : line.endsWith('\n') ? line.slice(0, -1) : line;
147+
148+
const getReviewPatchText = (lines: ReadonlyArray<string>, index: number) =>
149+
trimReviewPatchLineTerminator(lines[index] ?? '');
143150

144151
export const getReviewCommentPatchContext = (
145152
file: ChangedFile,
@@ -160,8 +167,9 @@ export const getReviewCommentPatchContext = (
160167
rows.push({
161168
additionLineNumber: additionLineNumber + index,
162169
deletionLineNumber: deletionLineNumber + index,
170+
patchLineIndex: content.additionLineIndex + index,
171+
patchLines: fileDiff.additionLines,
163172
prefix: ' ',
164-
text: fileDiff.additionLines[content.additionLineIndex + index] ?? '',
165173
});
166174
}
167175
deletionLineNumber += content.lines;
@@ -172,18 +180,20 @@ export const getReviewCommentPatchContext = (
172180
for (let index = 0; index < content.deletions; index += 1) {
173181
rows.push({
174182
deletionLineNumber: deletionLineNumber + index,
183+
patchLineIndex: content.deletionLineIndex + index,
184+
patchLines: fileDiff.deletionLines,
175185
prefix: '-',
176186
side: 'deletions',
177-
text: fileDiff.deletionLines[content.deletionLineIndex + index] ?? '',
178187
});
179188
}
180189

181190
for (let index = 0; index < content.additions; index += 1) {
182191
rows.push({
183192
additionLineNumber: additionLineNumber + index,
193+
patchLineIndex: content.additionLineIndex + index,
194+
patchLines: fileDiff.additionLines,
184195
prefix: '+',
185196
side: 'additions',
186-
text: fileDiff.additionLines[content.additionLineIndex + index] ?? '',
187197
});
188198
}
189199

@@ -213,7 +223,10 @@ export const getReviewCommentPatchContext = (
213223
: row.prefix === '-'
214224
? row.deletionLineNumber
215225
: `${row.deletionLineNumber ?? ''}/${row.additionLineNumber ?? ''}`;
216-
return `${row.prefix}${formatReviewLineNumber(lineNumber ?? '')} | ${row.text}`;
226+
return `${row.prefix}${formatReviewLineNumber(lineNumber ?? '')} | ${getReviewPatchText(
227+
row.patchLines,
228+
row.patchLineIndex,
229+
)}`;
217230
});
218231

219232
return [hunk.hunkSpecs?.trim(), ...context].filter(Boolean).join('\n');

0 commit comments

Comments
 (0)