diff --git a/src/features/github/data/local.test.ts b/src/features/github/data/local.test.ts new file mode 100644 index 0000000000..461cfed39d --- /dev/null +++ b/src/features/github/data/local.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import { inOrder } from "./local"; + +const f = (filename: string) => ({ filename }); + +describe("inOrder", () => { + // GitHub lists a pull request's files by path, dotfiles first; the changes tree lists folders + // before root files. The diff chain (F7) follows the tree. + const api = [f(".env.example"), f(".gitignore"), f("README.md"), f("src/a.ts"), f("src/b.ts")]; + const tree = ["src/a.ts", "src/b.ts", ".env.example", ".gitignore", "README.md"]; + + it("sorts the files like the given order", () => { + expect(inOrder(api, tree).map((x) => x.filename)).toEqual(tree); + }); + + it("keeps the files' own order without one, and puts unlisted files last", () => { + expect(inOrder(api).map((x) => x.filename)).toEqual(api.map((x) => x.filename)); + expect(inOrder(api, ["src/b.ts"]).map((x) => x.filename)).toEqual([ + "src/b.ts", + ".env.example", + ".gitignore", + "README.md", + "src/a.ts", + ]); + }); + + it("doesn't change its input", () => { + const input = [...api]; + inOrder(input, tree); + expect(input).toEqual(api); + }); +}); diff --git a/src/features/github/data/local.ts b/src/features/github/data/local.ts index 9ab53f0d10..1a50056022 100644 --- a/src/features/github/data/local.ts +++ b/src/features/github/data/local.ts @@ -143,15 +143,23 @@ export function fileDiffData( }; } +/** `files` sorted by their position in `order` (paths); files not listed keep their order, last. */ +export function inOrder(files: readonly F[], order: readonly string[] = []): F[] { + if (!order.length) return [...files]; + const rank = new Map(order.map((p, i) => [p, i])); + return [...files].sort((a, b) => (rank.get(a.filename) ?? rank.size) - (rank.get(b.filename) ?? rank.size)); +} + /** * Opens `path` of pull request `number` in the diff viewer (fetching what it needs first), * revealing the 0-based `line` of `side` (default right) when given (a review thread's line). - * The pull request's other files follow in the chain (F7 past the last change). + * The pull request's other files follow in the chain (F7 past the last change), in `opts.order`'s + * order (paths, e.g. the changes tree's) when given. */ export async function openPullRequestFileDiff( number: number, path: string, - opts: { line?: number; side?: "left" | "right" } = {}, + opts: { line?: number; side?: "left" | "right"; order?: readonly string[] } = {}, ): Promise { const [pr, files] = await Promise.all([ loadPullRequestData(number, "details"), @@ -164,7 +172,8 @@ export async function openPullRequestFileDiff( status: "modified" as const, previousFilename: null, }; - const all = files.some((f) => f.filename === path) ? files : [...files, file]; + const known = files.some((f) => f.filename === path) ? files : [...files, file]; + const all = inOrder(known, opts.order); const data = withChain( all.map((f) => fileDiffData(pr, f, revs)), all.findIndex((f) => f.filename === path), diff --git a/src/features/github/details/ChangesTree.tsx b/src/features/github/details/ChangesTree.tsx index 287037b5f0..b2ce50b488 100644 --- a/src/features/github/details/ChangesTree.tsx +++ b/src/features/github/details/ChangesTree.tsx @@ -83,8 +83,12 @@ export function ChangesTree({ number, files, commitSha, viewedState, threads, ch // Until the threads and the viewed state load, no state is shown (`isLoading`). const loading = threads === undefined || (hasViewedState && viewedState === undefined); + // F7 / Shift+F7 walk the diff chain; it follows the tree's order, not the API's. + const order = useMemo(() => filesUnder(roots).map((f) => f.filename), [roots]); const openDiff = (f: GHCommitFile) => - void (commitSha ? openCommitFileDiff(number, commitSha, f) : openPullRequestFileDiff(number, f.filename)); + void (commitSha + ? openCommitFileDiff(number, commitSha, f) + : openPullRequestFileDiff(number, f.filename, { order })); const jumpToSource = (f: GHCommitFile) => { const root = reviewRoot(); if (root) void editorService().openFile(`${root.replace(/[\\/]+$/, "")}/${f.filename}`, { focus: true }); diff --git a/src/features/github/showDiff.test.tsx b/src/features/github/showDiff.test.tsx index c835ad2c09..d498658413 100644 --- a/src/features/github/showDiff.test.tsx +++ b/src/features/github/showDiff.test.tsx @@ -80,6 +80,8 @@ describe("Show Diff in a pull request's changes", () => { previousFilename: null, }); const files = [file("src/a.ts"), file("src/b.ts")]; + // The chain (F7) follows the tree's order. + const chain = { order: ["src/a.ts", "src/b.ts"] }; const show = (commitSha: string | null) => render( { it("opens the pull request's diff of the selected file", async () => { show(null); press("b.ts"); - await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts")); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts", chain)); }); it("opens the selected file's diff in the chosen commit", async () => { @@ -107,20 +109,20 @@ describe("Show Diff in a pull request's changes", () => { it("opens the first file of a selected directory", async () => { show(null); press("src"); - await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts")); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts", chain)); }); it("opens the first file when nothing is selected", async () => { show(null); press(); - await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts")); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts", chain)); }); it("opens the same diff from Show Diff in New Window", async () => { bind("vcs.showDiffInNewWindow"); show(null); press("b.ts"); - await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts")); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts", chain)); }); });