mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
Move comment and commit stepping into lib
The order the stepper walks comments in and its wraparound index math were untested, and ReviewPage ordered its file groups with its own copy.
This commit is contained in:
@@ -9,6 +9,11 @@
|
||||
import { tick, untrack } from "svelte";
|
||||
|
||||
import type { CodeComments } from "@app/lib/codeComments";
|
||||
import {
|
||||
orderComments,
|
||||
stepCommitIndex,
|
||||
stepIndex,
|
||||
} from "@app/lib/commentNavigation";
|
||||
import { diffOptions } from "@app/lib/diffOptions.svelte";
|
||||
import { fileDiffPath, fileMetaOf, fullFileLoader } from "@app/lib/diffText";
|
||||
import { draftReviewStorage } from "@app/lib/draftReviewStorage";
|
||||
@@ -19,11 +24,7 @@
|
||||
cachedListCommits,
|
||||
getDiffText,
|
||||
} from "@app/lib/invoke";
|
||||
import {
|
||||
anchorOf,
|
||||
commentCountsByPath,
|
||||
isCommentableStatus,
|
||||
} from "@app/lib/pierreComments";
|
||||
import { anchorOf, commentCountsByPath } from "@app/lib/pierreComments";
|
||||
import { useShortcuts } from "@app/lib/shortcuts.svelte";
|
||||
import { pluralize } from "@app/lib/utils";
|
||||
|
||||
@@ -237,46 +238,16 @@
|
||||
);
|
||||
});
|
||||
|
||||
// Every comment on the diff in the order it is rendered — by file, in the
|
||||
// order the files appear, then down the lines of each. What the tab bar's
|
||||
// stepper walks.
|
||||
const orderedComments = $derived.by(() => {
|
||||
const comments = diffCodeComments;
|
||||
if (!comments) return [];
|
||||
const fileOrder = new Map(
|
||||
diffFiles.map((file, index) => [fileDiffPath(file), index] as const),
|
||||
);
|
||||
return comments.threads
|
||||
.flatMap(thread => {
|
||||
const anchor = anchorOf(thread.root.location);
|
||||
// Dropped for the same two reasons the diff itself drops them: the file
|
||||
// is not in this diff, or its content moved and an anchor in it is
|
||||
// ambiguous. Either way there is nothing on screen to step to.
|
||||
if (!anchor) return [];
|
||||
const order = fileOrder.get(anchor.path);
|
||||
if (order === undefined) return [];
|
||||
if (!isCommentableStatus(fileMeta.statuses.get(anchor.path))) return [];
|
||||
return [
|
||||
{
|
||||
id: thread.root.id,
|
||||
anchor,
|
||||
order,
|
||||
// A deletion is rendered above an addition on the same line, and
|
||||
// two comments on one line are ordered oldest first — the same rule
|
||||
// the annotations themselves are built with.
|
||||
side: anchor.side === "deletions" ? 0 : 1,
|
||||
timestamp: thread.root.edits[0].timestamp,
|
||||
},
|
||||
];
|
||||
})
|
||||
.sort(
|
||||
(a, b) =>
|
||||
a.order - b.order ||
|
||||
a.anchor.line - b.anchor.line ||
|
||||
a.side - b.side ||
|
||||
a.timestamp - b.timestamp,
|
||||
);
|
||||
});
|
||||
// What the tab bar's stepper walks.
|
||||
const orderedComments = $derived(
|
||||
diffCodeComments
|
||||
? orderComments(
|
||||
diffCodeComments.threads,
|
||||
diffFiles.map(fileDiffPath),
|
||||
path => fileMeta.statuses.get(path),
|
||||
)
|
||||
: [],
|
||||
);
|
||||
|
||||
// Which comment the stepper is on, held by id rather than by position: the
|
||||
// list shifts as comments are written, deleted or filtered out, and a position
|
||||
@@ -305,13 +276,9 @@
|
||||
/// Walk to the next (`1`) or previous (`-1`) comment on the diff. Called from
|
||||
/// the tab bar, which sits outside this component.
|
||||
export function stepComment(delta: number) {
|
||||
const total = orderedComments.length;
|
||||
if (total === 0) return;
|
||||
// From nothing, a step down starts at the first comment and a step up at the
|
||||
// last. From somewhere, both wrap, so a walk never dead-ends at either end
|
||||
// of a long diff.
|
||||
const from = commentIndex >= 0 ? commentIndex : delta > 0 ? -1 : 0;
|
||||
const target = orderedComments[(from + delta + total) % total];
|
||||
const index = stepIndex(commentIndex, orderedComments.length, delta);
|
||||
if (index === undefined) return;
|
||||
const target = orderedComments[index];
|
||||
activeCommentId = target.id;
|
||||
clearTimeout(highlightTimer);
|
||||
highlightedCommentId = target.id;
|
||||
@@ -457,8 +424,7 @@
|
||||
|
||||
function selectCommitAt(index: number) {
|
||||
if (commitList.length <= 1) return;
|
||||
const clamped = Math.max(0, Math.min(index, commitList.length - 1));
|
||||
const commit = commitList[clamped];
|
||||
const commit = commitList[index];
|
||||
selectRevision({
|
||||
headId: commit.id,
|
||||
baseId: commit.parents[0],
|
||||
@@ -475,7 +441,7 @@
|
||||
enabled: () => commitList.length > 1,
|
||||
run: () => {
|
||||
const current = commitList.findIndex(c => c.id === selectedCommit);
|
||||
selectCommitAt(current === -1 ? commitList.length - 1 : current - 1);
|
||||
selectCommitAt(stepCommitIndex(current, commitList.length, -1));
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -483,7 +449,7 @@
|
||||
enabled: () => commitList.length > 1,
|
||||
run: () => {
|
||||
const current = commitList.findIndex(c => c.id === selectedCommit);
|
||||
selectCommitAt(current === -1 ? 0 : current + 1);
|
||||
selectCommitAt(stepCommitIndex(current, commitList.length, 1));
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import type { CommentOwner } from "@app/lib/codeCommentActions";
|
||||
import { commentActions } from "@app/lib/codeCommentActions";
|
||||
import type { CodeComments } from "@app/lib/codeComments";
|
||||
import { orderFileGroups } from "@app/lib/commentNavigation";
|
||||
import { resolutionsByComment } from "@app/lib/commentResolutions";
|
||||
import { diffOptions } from "@app/lib/diffOptions.svelte";
|
||||
import {
|
||||
@@ -36,7 +37,6 @@
|
||||
invoke,
|
||||
} from "@app/lib/invoke";
|
||||
import type { CommentAnchor } from "@app/lib/pierreComments";
|
||||
import { anchorOf } from "@app/lib/pierreComments";
|
||||
import * as roles from "@app/lib/roles";
|
||||
import { push } from "@app/lib/router";
|
||||
import {
|
||||
@@ -522,22 +522,9 @@
|
||||
// The sidebar reads top to bottom alongside the file column, so the files come
|
||||
// in diff order and each file's threads in line order. `fileGroups` is built
|
||||
// from the review's comments, which are in neither.
|
||||
const orderedGroups = $derived.by(() => {
|
||||
const order = new Map(
|
||||
diffFiles.map((file, index) => [fileDiffPath(file), index]),
|
||||
);
|
||||
return fileGroups
|
||||
.filter(group => order.has(group.path))
|
||||
.sort((a, b) => (order.get(a.path) ?? 0) - (order.get(b.path) ?? 0))
|
||||
.map(group => ({
|
||||
path: group.path,
|
||||
threads: [...group.threads].sort(
|
||||
(a, b) =>
|
||||
(anchorOf(a.root.location)?.line ?? 0) -
|
||||
(anchorOf(b.root.location)?.line ?? 0),
|
||||
),
|
||||
}));
|
||||
});
|
||||
const orderedGroups = $derived(
|
||||
orderFileGroups(fileGroups, diffFiles.map(fileDiffPath)),
|
||||
);
|
||||
|
||||
const verdict = $derived(review.verdict);
|
||||
const timestamp = $derived(review.timestamp);
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import type { FileStatus } from "@app/components/diffFileHeaderState.svelte";
|
||||
import type { CodeLocation } from "@bindings/cob/thread/CodeLocation";
|
||||
import type { Thread } from "@bindings/cob/thread/Thread";
|
||||
|
||||
import type { CommentAnchor } from "@app/lib/pierreComments";
|
||||
import { anchorOf, isCommentableStatus } from "@app/lib/pierreComments";
|
||||
|
||||
export interface OrderedComment {
|
||||
id: string;
|
||||
anchor: CommentAnchor;
|
||||
}
|
||||
|
||||
/// Every comment on a diff in the order it is rendered: by file, in the order
|
||||
/// the files appear, then down the lines of each. A deletion renders above an
|
||||
/// addition on the same line, and two comments on one line oldest first.
|
||||
///
|
||||
/// Comments are left out for the same reasons the diff leaves them out: the file
|
||||
/// isn't in this diff, or its content moved and an anchor in it is ambiguous.
|
||||
export function orderComments(
|
||||
threads: Thread<CodeLocation>[],
|
||||
filePaths: string[],
|
||||
statusOf: (path: string) => FileStatus | undefined,
|
||||
): OrderedComment[] {
|
||||
const fileOrder = new Map(filePaths.map((path, index) => [path, index]));
|
||||
return threads
|
||||
.flatMap(thread => {
|
||||
const anchor = anchorOf(thread.root.location);
|
||||
if (!anchor) return [];
|
||||
const order = fileOrder.get(anchor.path);
|
||||
if (order === undefined) return [];
|
||||
if (!isCommentableStatus(statusOf(anchor.path))) return [];
|
||||
return [
|
||||
{
|
||||
id: thread.root.id,
|
||||
anchor,
|
||||
order,
|
||||
side: anchor.side === "deletions" ? 0 : 1,
|
||||
timestamp: thread.root.edits[0].timestamp,
|
||||
},
|
||||
];
|
||||
})
|
||||
.sort(
|
||||
(a, b) =>
|
||||
a.order - b.order ||
|
||||
a.anchor.line - b.anchor.line ||
|
||||
a.side - b.side ||
|
||||
a.timestamp - b.timestamp,
|
||||
)
|
||||
.map(({ id, anchor }) => ({ id, anchor }));
|
||||
}
|
||||
|
||||
/// The index a step of `delta` (`1` or `-1`) lands on in a list of `total`.
|
||||
/// From nothing (`current` is `-1`), a step down starts at the first item and a
|
||||
/// step up at the last. From somewhere, both wrap, so a walk never dead-ends.
|
||||
export function stepIndex(
|
||||
current: number,
|
||||
total: number,
|
||||
delta: number,
|
||||
): number | undefined {
|
||||
if (total === 0) return undefined;
|
||||
const from = current >= 0 ? current : delta > 0 ? -1 : 0;
|
||||
return (from + delta + total) % total;
|
||||
}
|
||||
|
||||
/// The commit a step of `delta` selects, clamped to the list. From no
|
||||
/// selection, stepping back selects the last commit and forward the first.
|
||||
export function stepCommitIndex(
|
||||
current: number,
|
||||
total: number,
|
||||
delta: number,
|
||||
): number {
|
||||
const next = current === -1 ? (delta > 0 ? 0 : total - 1) : current + delta;
|
||||
return Math.max(0, Math.min(next, total - 1));
|
||||
}
|
||||
|
||||
/// File groups in diff order, each file's threads in line order. Groups for
|
||||
/// files that aren't in the diff are dropped.
|
||||
export function orderFileGroups(
|
||||
groups: { path: string; threads: Thread<CodeLocation>[] }[],
|
||||
filePaths: string[],
|
||||
): { path: string; threads: Thread<CodeLocation>[] }[] {
|
||||
const order = new Map(filePaths.map((path, index) => [path, index]));
|
||||
return groups
|
||||
.filter(group => order.has(group.path))
|
||||
.sort((a, b) => (order.get(a.path) ?? 0) - (order.get(b.path) ?? 0))
|
||||
.map(group => ({
|
||||
path: group.path,
|
||||
threads: [...group.threads].sort(
|
||||
(a, b) =>
|
||||
(anchorOf(a.root.location)?.line ?? 0) -
|
||||
(anchorOf(b.root.location)?.line ?? 0),
|
||||
),
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
orderComments,
|
||||
orderFileGroups,
|
||||
stepCommitIndex,
|
||||
stepIndex,
|
||||
} from "@app/lib/commentNavigation";
|
||||
|
||||
import { comment, location, thread } from "./support/cobs";
|
||||
|
||||
function at(
|
||||
id: string,
|
||||
path: string,
|
||||
side: "old" | "new",
|
||||
line: number,
|
||||
timestamp = 0,
|
||||
) {
|
||||
return thread(
|
||||
comment({ id, location: location(path, side, line), timestamp }),
|
||||
);
|
||||
}
|
||||
|
||||
describe("orderComments", () => {
|
||||
test("orders by file order, line, side, then age", () => {
|
||||
const ordered = orderComments(
|
||||
[
|
||||
at("b-5", "b.ts", "new", 5),
|
||||
at("a-9", "a.ts", "new", 9),
|
||||
at("a-2-new", "a.ts", "new", 2),
|
||||
at("a-2-old", "a.ts", "old", 2),
|
||||
at("a-2-new-older", "a.ts", "new", 2, -1),
|
||||
],
|
||||
["a.ts", "b.ts"],
|
||||
() => "modified",
|
||||
);
|
||||
|
||||
expect(ordered.map(c => c.id)).toEqual([
|
||||
"a-2-old",
|
||||
"a-2-new-older",
|
||||
"a-2-new",
|
||||
"a-9",
|
||||
"b-5",
|
||||
]);
|
||||
expect(ordered[0].anchor).toEqual({
|
||||
path: "a.ts",
|
||||
side: "deletions",
|
||||
line: 2,
|
||||
});
|
||||
});
|
||||
|
||||
test("leaves out comments the diff can't show", () => {
|
||||
const ordered = orderComments(
|
||||
[
|
||||
at("elsewhere", "gone.ts", "new", 1),
|
||||
at("moved", "moved.ts", "new", 1),
|
||||
thread(comment({ id: "plain", location: null })),
|
||||
at("shown", "a.ts", "new", 1),
|
||||
],
|
||||
["a.ts", "moved.ts"],
|
||||
path => (path === "moved.ts" ? "moved" : "modified"),
|
||||
);
|
||||
|
||||
expect(ordered.map(c => c.id)).toEqual(["shown"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("stepIndex", () => {
|
||||
test.each([
|
||||
[-1, 1, 0],
|
||||
[-1, -1, 2],
|
||||
[0, 1, 1],
|
||||
[2, 1, 0],
|
||||
[0, -1, 2],
|
||||
])("from %j by %j lands on %j of three", (current, delta, expected) => {
|
||||
expect(stepIndex(current, 3, delta)).toBe(expected);
|
||||
});
|
||||
|
||||
test("has nowhere to go in an empty list", () => {
|
||||
expect(stepIndex(-1, 0, 1)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("stepCommitIndex", () => {
|
||||
test.each([
|
||||
[-1, -1, 2],
|
||||
[-1, 1, 0],
|
||||
[1, 1, 2],
|
||||
[1, -1, 0],
|
||||
[2, 1, 2],
|
||||
[0, -1, 0],
|
||||
])("from %j by %j selects %j of three", (current, delta, expected) => {
|
||||
expect(stepCommitIndex(current, 3, delta)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
test("orderFileGroups follows the diff's file order and line order", () => {
|
||||
const groups = orderFileGroups(
|
||||
[
|
||||
{
|
||||
path: "b.ts",
|
||||
threads: [at("b-9", "b.ts", "new", 9), at("b-1", "b.ts", "new", 1)],
|
||||
},
|
||||
{ path: "gone.ts", threads: [at("g", "gone.ts", "new", 1)] },
|
||||
{ path: "a.ts", threads: [at("a-3", "a.ts", "old", 3)] },
|
||||
],
|
||||
["a.ts", "b.ts"],
|
||||
);
|
||||
|
||||
expect(groups.map(g => [g.path, ...g.threads.map(t => t.root.id)])).toEqual([
|
||||
["a.ts", "a-3"],
|
||||
["b.ts", "b-1", "b-9"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("orderFileGroups leaves the caller's groups as they were", () => {
|
||||
const threads = [at("b-9", "b.ts", "new", 9), at("b-1", "b.ts", "new", 1)];
|
||||
orderFileGroups([{ path: "b.ts", threads }], ["b.ts"]);
|
||||
|
||||
expect(threads.map(t => t.root.id)).toEqual(["b-9", "b-1"]);
|
||||
});
|
||||
Reference in New Issue
Block a user