Skip to content

Commit e5a2031

Browse files
only21milclaudemaria-rcks
authored
fix(server): review guide stops no longer show the same lines twice (pingdotgg#23725)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent b0aa468 commit e5a2031

2 files changed

Lines changed: 60 additions & 15 deletions

File tree

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,48 @@ describe("normalizeReviewGuide", () => {
152152
expect(result.stops).toHaveLength(1);
153153
});
154154

155+
it("merges excerpts that a later excerpt bridges into one", () => {
156+
const result = normalizeReviewGuide(
157+
guide({
158+
stops: [
159+
stop({
160+
excerpts: [
161+
excerpt("src/a.ts", 40, 41),
162+
{ ...excerpt("src/a.ts", 50, 51), caption: "Second" },
163+
excerpt("src/a.ts", 41, 50),
164+
],
165+
}),
166+
],
167+
}),
168+
change,
169+
);
170+
expect(result.stops[0]?.excerpts).toEqual([
171+
{ ...excerpt("src/a.ts", 40, 51), caption: "Second" },
172+
]);
173+
174+
const tailNote = { path: "src/a.ts", line: 40, title: "Tail", body: "Keep this note." };
175+
const wide = normalizeReviewGuide(
176+
guide({
177+
stops: [
178+
stop({
179+
excerpts: [
180+
excerpt("src/a.ts", 1, 25),
181+
{ ...excerpt("src/a.ts", 30, 45), caption: "Second" },
182+
excerpt("src/a.ts", 24, 32),
183+
],
184+
notes: [tailNote],
185+
}),
186+
],
187+
}),
188+
parseReviewGuideChange(
189+
"src/a.ts\0",
190+
"diff --git a/src/a.ts b/src/a.ts\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -0,0 +1,60 @@\n+added",
191+
),
192+
);
193+
expect(wide.stops[0]?.excerpts).toEqual([{ ...excerpt("src/a.ts", 1, 25), caption: "Second" }]);
194+
expect(wide.stops[0]?.notes).toEqual([]);
195+
});
196+
155197
it("drops notes outside the stop's remaining excerpts", () => {
156198
const result = normalizeReviewGuide(
157199
guide({

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,29 @@ function clampExcerpt(
8787
function mergeExcerpts(
8888
excerpts: ReadonlyArray<PullRequestReviewGuideExcerpt>,
8989
): Array<PullRequestReviewGuideExcerpt> {
90-
const merged: Array<PullRequestReviewGuideExcerpt> = [];
90+
let merged: Array<PullRequestReviewGuideExcerpt> = [];
9191
for (const excerpt of excerpts) {
92-
const index = merged.findIndex(
93-
(other) =>
94-
other.path === excerpt.path &&
95-
other.side === excerpt.side &&
96-
other.start <= excerpt.end + 1 &&
97-
excerpt.start <= other.end + 1,
98-
);
99-
const other = merged[index];
100-
if (other === undefined) {
92+
const touches = (other: PullRequestReviewGuideExcerpt) =>
93+
other.path === excerpt.path &&
94+
other.side === excerpt.side &&
95+
other.start <= excerpt.end + 1 &&
96+
excerpt.start <= other.end + 1;
97+
// An excerpt can bridge several earlier ones; fold them all into the first.
98+
const group = merged.filter(touches);
99+
const first = group[0];
100+
if (first === undefined) {
101101
merged.push(excerpt);
102102
continue;
103103
}
104-
merged[index] = {
105-
...other,
106-
start: Math.min(other.start, excerpt.start),
107-
end: Math.max(other.end, excerpt.end),
108-
caption: other.caption || excerpt.caption,
104+
const combined = {
105+
...first,
106+
start: Math.min(excerpt.start, ...group.map((other) => other.start)),
107+
end: Math.max(excerpt.end, ...group.map((other) => other.end)),
108+
caption: group.find((other) => other.caption)?.caption ?? excerpt.caption,
109109
};
110+
merged = merged.flatMap((other) =>
111+
other === first ? [combined] : touches(other) ? [] : [other],
112+
);
110113
}
111114
return merged.map((excerpt) => ({
112115
...excerpt,

0 commit comments

Comments
 (0)