дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
fix(github): open a pull request's diff chain in the changes tree's order (#8219)
F7 / Shift+F7 in a pull request's diff walked the files in GitHub's API order (by path, root files first) while the changes tree lists folders before root files, so Show Diff from the tree could start mid-chain. The changes tree now passes its file order to openPullRequestFileDiff, which sorts the chain by it; other callers keep GitHub's order. Commit diffs have no chain and are unaffected. Co-authored-by: coding46 <[email protected]>
Цей коміт міститься в:
1 джерело
4c44beba35
коміт
46031d162b
4 files changed
+55
-8
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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<F extends { filename: string }>(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<void> {
|
||||
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),
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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(
|
||||
<ChangesTree
|
||||
@@ -95,7 +97,7 @@ describe("Show Diff in a pull request's changes", () => {
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Заблокувати користувача