Skip to content

Commit 0d92723

Browse files
committed
Prefer additions when resolving hunk comments
1 parent 676e827 commit 0d92723

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/core/liveComments.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function findHunkIndexForLine(file: DiffFile, side: DiffSide, line: numbe
4343
export function firstCommentTargetForHunk(hunk: Hunk): Omit<ResolvedCommentTarget, "hunkIndex"> {
4444
let deletionLineNumber = hunk.deletionStart;
4545
let additionLineNumber = hunk.additionStart;
46+
let firstDeletionLine: number | undefined;
4647

4748
for (const content of hunk.hunkContent) {
4849
if (content.type === "context") {
@@ -58,12 +59,19 @@ export function firstCommentTargetForHunk(hunk: Hunk): Omit<ResolvedCommentTarge
5859
};
5960
}
6061

61-
if (content.deletions > 0) {
62-
return {
63-
side: "old",
64-
line: deletionLineNumber,
65-
};
62+
if (content.deletions > 0 && firstDeletionLine === undefined) {
63+
firstDeletionLine = deletionLineNumber;
6664
}
65+
66+
deletionLineNumber += content.deletions;
67+
additionLineNumber += content.additions;
68+
}
69+
70+
if (firstDeletionLine !== undefined) {
71+
return {
72+
side: "old",
73+
line: firstDeletionLine,
74+
};
6775
}
6876

6977
const fallbackRange = hunkLineRange(hunk);

test/live-comments.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
buildLiveComment,
55
findDiffFileByPath,
66
findHunkIndexForLine,
7+
firstCommentTargetForHunk,
78
hunkLineRange,
89
resolveCommentTarget,
910
} from "../src/core/liveComments";
@@ -125,6 +126,25 @@ describe("live comment helpers", () => {
125126
});
126127
});
127128

129+
test("prefers a later addition over an earlier deletion-only chunk", () => {
130+
const target = firstCommentTargetForHunk({
131+
additionStart: 20,
132+
additionLines: 1,
133+
deletionStart: 20,
134+
deletionLines: 1,
135+
hunkContent: [
136+
{ type: "change", deletions: 1, additions: 0 },
137+
{ type: "context", lines: 2 },
138+
{ type: "change", deletions: 0, additions: 1 },
139+
],
140+
} as Parameters<typeof firstCommentTargetForHunk>[0]);
141+
142+
expect(target).toEqual({
143+
side: "new",
144+
line: 22,
145+
});
146+
});
147+
128148
test("builds a live MCP comment annotation", () => {
129149
const comment = buildLiveComment(
130150
{

0 commit comments

Comments
 (0)