Файли
openidea/src/features/vcs/changesPreview.ts
T
l0ng-ai 6927b787b2 fix(vcs): move on to the next file from the changes diff preview (#7271)
The diff preview embedded in the Commit tool window and the Git window's Local Changes tab built a diff for the selected file without its sibling files, so F7 / the down arrow past the last difference did nothing there.

The changes tree now publishes its files in tree order (unversioned included, amend entries left out) and the preview uses them as the diff's chain. Past the last (first) difference the diff hints, then shows the next (previous) file in the preview itself and asks the tree to select it; the tree reveals it the same way it follows the active diff tab.

Fixes #7149
2026-12-02 00:15:32 +08:00

48 рядки
2.2 KiB
TypeScript

import { create } from "zustand";
import type { FileStatus } from "@/bindings";
import { type DiffData, withChain, workingDiffData } from "./diffInput";
/** The changes trees whose selection an embedded diff preview shows. */
export type PreviewTree = "commit" | "local";
interface ChangesPreviewState {
/** Each tree's files in tree order: the preview's F7 chain. */
files: Partial<Record<PreviewTree, readonly string[]>>;
/** A file the preview moved to past its first or last difference, for the tree to select. */
moved: { tree: PreviewTree; path: string; seq: number } | null;
}
export const useChangesPreview = create<ChangesPreviewState>(() => ({ files: {}, moved: null }));
/** Called by a changes tree when its files or their order change. */
export function publishPreviewFiles(tree: PreviewTree, paths: readonly string[] | undefined) {
const current = useChangesPreview.getState().files[tree];
const same = current && paths && current.length === paths.length && current.every((p, i) => p === paths[i]);
if (same || (!current && !paths)) return;
useChangesPreview.setState((s) => ({ files: { ...s.files, [tree]: paths } }));
}
/** Asks `tree` to select `path`, the file the preview moved to. */
export function selectPreviewFile(tree: PreviewTree, path: string) {
useChangesPreview.setState((s) => ({ moved: { tree, path, seq: (s.moved?.seq ?? 0) + 1 } }));
}
/**
* The preview of the working file `path`, with the tree's other files as its chain: as in
* IDEA's changes view preview, F7 (Shift+F7) past the last (first) difference goes on to the
* next (previous) file and selects it in the tree. `moved` is the neighbour the diff moved
* to, which carries the difference to reveal.
*/
export function previewDiffData(
path: string,
files: readonly string[] | undefined,
moved: DiffData | null,
status: readonly FileStatus[],
): DiffData {
const byPath = new Map(status.map((st) => [st.path, st]));
const diff = (p: string) => workingDiffData(p, undefined, null, byPath.get(p));
const index = files ? files.indexOf(path) : -1;
const data = !files || index < 0 || files.length < 2 ? diff(path) : withChain(files.map(diff), index);
return moved?.path === path && moved.reveal ? { ...data, reveal: moved.reveal } : data;
}