Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,20 @@ class DiffViewController extends Disposable implements ReviewDiffViewHandle {
const rendered = new Set<string>();
const renderCurrentState = (change?: StructuralSessionChange) => {
if (this.disposed) return;
for (const entry of entries) {
const path = entry.file.path;
if (change && !change.files.has(path)) continue;
const result = entry.file.status === "unchanged" ? {} : session.getFileResult(path);
if (!result) continue;
if (!rendered.has(path) && result.hidden !== undefined) view.hideFile(path, result.hidden);
rendered.add(path);
if (result.diff) view.fileCounts(path, reviewFileCounts(result.diff));
view.fileLoaded(path, result.error);
}
if (session.error) view.loadingFailed(session.error);
else if (session.complete) view.loadingFailed("diffr did not supply a result for this file.");
view.batch(() => {
for (const entry of entries) {
const path = entry.file.path;
if (change && !change.files.has(path)) continue;
const result = entry.file.status === "unchanged" ? {} : session.getFileResult(path);
if (!result) continue;
if (!rendered.has(path) && result.hidden !== undefined) view.hideFile(path, result.hidden);
rendered.add(path);
if (result.diff) view.fileCounts(path, reviewFileCounts(result.diff));
view.fileLoaded(path, result.error);
}
if (session.error) view.loadingFailed(session.error);
else if (session.complete) view.loadingFailed("diffr did not supply a result for this file.");
});
};
store.add(session.onDidChange(renderCurrentState));
renderCurrentState();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { ReviewDiffProgressState, ReviewDiffSection } from "../common/revie
*--------------------------------------------------------------------------------------------*/

import { Button } from "../../base/browser/ui/button/button.js";
import { isEqual } from "../../base/common/resources.js";
import { URI } from "../../base/common/uri.js";
import type {
IResourceLabel,
Expand Down Expand Up @@ -50,6 +49,12 @@ export interface ReviewMultiDiffHeaderEntry {
readonly onToggleViewed?: () => void;
}

/** Answers for one resource at a time: a header refresh must not walk every file. */
export interface ReviewMultiDiffHeaderSource {
entry(uris: { readonly original: URI | undefined; readonly modified: URI | undefined }): ReviewMultiDiffHeaderEntry | undefined;
section(id: string): ReviewDiffSection | undefined;
}

export class ReviewMultiDiffUIElementFactory
implements IWorkbenchUIElementFactory
{
Expand All @@ -70,7 +75,7 @@ export class ReviewMultiDiffUIElementFactory
}

constructor(
private readonly entries: () => readonly ReviewMultiDiffHeaderEntry[],
private readonly source: ReviewMultiDiffHeaderSource,
readonly horizontalScrollbar: "auto" | "hidden",
readonly overflowWidgetsDomNode: HTMLElement | undefined,
readonly hideResourceHeader = false,
Expand All @@ -81,7 +86,7 @@ export class ReviewMultiDiffUIElementFactory
) {}

isResourceCollapseLocked(uris: Parameters<IResourceHeaderMetadata["setUris"]>[0]): boolean {
return !!uris && !!this.entries().find(entry => sameResource(entry.original, uris.original) && sameResource(entry.modified, uris.modified))?.collapseLocked;
return !!uris && !!this.source.entry(uris)?.collapseLocked;
}

getResourceSectionId(uris: Parameters<IResourceHeaderMetadata["setUris"]>[0]): string | undefined {
Expand All @@ -98,10 +103,8 @@ export class ReviewMultiDiffUIElementFactory
const countsTooltip = new ReviewTooltip(this.hoverService, counts);
const viewed = new ReviewViewedCheckbox(this.hoverService, element.ownerDocument, () => current?.onToggleSection?.());
const refresh = () => {
const entry = current = uris && this.entries().find(entry => sameResource(entry.original, uris!.original) && sameResource(entry.modified, uris!.modified));
const section = sticky && entry?.sectionId
? this.entries().find(candidate => candidate.sectionId === entry.sectionId && candidate.section)?.section
: entry?.section;
const entry = current = uris && this.source.entry(uris);
const section = sticky && entry?.sectionId ? this.source.section(entry.sectionId) : entry?.section;
bodyHidden.set(entry?.sectionCollapsed ?? false, undefined);
element.replaceChildren(); element.className = 'review-diff-group'; element.hidden = !section;
height.set(section ? 48 : 0, undefined);
Expand Down Expand Up @@ -208,13 +211,7 @@ export class ReviewMultiDiffUIElementFactory

const setUris: IResourceHeaderMetadata["setUris"] = (uris) => {
lastUris = uris;
current = uris
? this.entries().find(
(entry) =>
sameResource(entry.original, uris.original) &&
sameResource(entry.modified, uris.modified),
)
: undefined;
current = uris ? this.source.entry(uris) : undefined;
element.hidden = !current;
if (!current) return;
const hasCounts =
Expand Down Expand Up @@ -272,12 +269,6 @@ export class ReviewMultiDiffUIElementFactory
}
}

function sameResource(left: URI | undefined, right: URI | undefined): boolean {
return left === undefined
? right === undefined
: !!right && isEqual(left, right);
}

export function reviewMultiDiffLabelUris(file: ReviewDiffFileWire): {
readonly original: URI | undefined;
readonly modified: URI | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function createStructuralDiffEditors(
const languages = instantiation.invokeFunction((a) => a.get(ILanguageService));
const viewId = ++structuralViewId;
const sources = new Map<string, { path: string; side: "lhs" | "rhs"; unchangedBase?: URI }>();
const snapshots = new Map<string, URI>();
const snapshots = new Map<string, { uri: URI; path: string; side: "lhs" | "rhs" }>();
const unchangedTexts = new Map<string, Promise<string>>();
const snapshotUri = (uri: URI, path: string, side: "lhs" | "rhs", unchangedBase?: URI) => {
const query = new URLSearchParams(uri.query);
Expand Down Expand Up @@ -85,7 +85,7 @@ export function createStructuralDiffEditors(
// diffr's binary check is stricter than git's.
text = result.diff?.type === "text" ? result.diff[source.side]?.text ?? "" : "";
}
if (!source.unchangedBase) snapshots.set(uri.toString(), uri);
if (!source.unchangedBase) snapshots.set(uri.toString(), { uri, path: source.path, side: source.side });
return modelService.getModel(uri) ?? modelService.createModel(text, languages.createByFilepathOrFirstLine(uri, text.split("\n", 1)[0]), uri);
},
}));
Expand All @@ -104,11 +104,10 @@ export function createStructuralDiffEditors(
const willEdit = lifetime.add(new Emitter<void>());
lifetime.add(session.onDidChange(change => {
// A refreshed diff edits its snapshots before any diff is recomputed against them.
const edits = [...snapshots.values()].flatMap(uri => {
const source = sources.get(uri.with({ fragment: "" }).toString())!;
const diff = change.files.has(source.path) ? session.getFileResult(source.path)?.diff : undefined;
const edits = [...snapshots.values()].flatMap(({ uri, path, side }) => {
const diff = change.files.has(path) ? session.getFileResult(path)?.diff : undefined;
const model = diff && modelService.getModel(uri);
return model ? [{ model, text: diff.type === "text" ? diff[source.side]?.text ?? "" : "" }] : [];
return model ? [{ model, text: diff.type === "text" ? diff[side]?.text ?? "" : "" }] : [];
});
if (edits.length) willEdit.fire();
for (const { model, text } of edits) replaceText(model, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class StructuralViewedState extends Disposable {
readonly onDidChange = this.changed.event;
private readonly scopes = new Map<string, ReturnType<typeof structuralViewedScopes>>();
private readonly states = new Map<string, string>();
private memo: { files: Map<string, ReviewDiffProgressFile>; values: Map<string, ReturnType<typeof structuralViewedProgress>> } | undefined;
private memo: { files: Map<string, ReviewDiffProgressFile>; values: Map<string, Map<number, ReturnType<typeof structuralViewedProgress>>> } | undefined;

constructor(
private readonly session: StructuralDiffSession,
Expand All @@ -28,11 +28,11 @@ export class StructuralViewedState extends Disposable {
onProgress: Event<void>,
) {
super();
const reset = () => { this.memo = undefined; this.sync(); };
this._register(onProgress(reset));
this._register(onProgress(() => { this.memo = undefined; this.sync(); }));
// A streamed batch changes only its own files; the rest keep their scopes and progress.
this._register(session.onDidChange(change => {
for (const path of change.files) this.scopes.delete(path);
reset();
for (const path of change.files) { this.scopes.delete(path); this.memo?.values.delete(path); }
this.sync(change.files);
}));
this.sync();
}
Expand All @@ -49,9 +49,10 @@ export class StructuralViewedState extends Disposable {

get(path: string, id: number) {
const { files, values } = this.current();
const key = `${path}:${id}`;
let value = values.get(key);
if (!value) values.set(key, value = structuralViewedProgress(this.getScopes(path)?.get(id) ?? [], files.get(path)));
let ofFile = values.get(path);
if (!ofFile) values.set(path, ofFile = new Map());
let value = ofFile.get(id);
if (!value) ofFile.set(id, value = structuralViewedProgress(this.getScopes(path)?.get(id) ?? [], files.get(path)));
return value;
}

Expand All @@ -63,11 +64,12 @@ export class StructuralViewedState extends Disposable {
return this.memo ??= { files: new Map(this.progress()?.files.map(file => [file.path, file])), values: new Map() };
}

private sync(): void {
private sync(paths?: ReadonlySet<string>): void {
const initialized = initializedScopes.get(this.session) ?? new Set<string>();
initializedScopes.set(this.session, initialized);
for (const entry of this.entries) {
const path = entry.file.path;
if (paths && !paths.has(path)) continue;
if (!this.current().files.has(path)) continue;
const scopes = this.getScopes(path);
const viewed = [...scopes ?? []].filter(([id]) => this.get(path, id).state === 'viewed');
Expand Down
131 changes: 71 additions & 60 deletions packages/review/app/src/DiffView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,65 +128,46 @@ export function ReviewDiffView({
const [lensTree, setLensTree] = useState<HTMLDivElement | null>(null);
const [scopeTree, setScopeTree] = useState<HTMLDivElement | null>(null);

const fullProgress = useMemo(
() =>
lenses?.progress
? {
files: lenses.progress.files.map((file) => ({
path: file.path,
...coverageProgress([file]),
viewedRanges: coverageSources(file),
changedRanges: coverageSources(file, file.changed),
unfoldRanges: lenses.unfoldRanges.filter(
(source) =>
source.file ===
(source.side === "base"
? (file.previousPath ?? file.path)
: file.path),
),
})),
changedPaths: lenses.changedPaths,
}
: undefined,
[lenses?.progress, lenses?.changedPaths, lenses?.unfoldRanges],
);

const lensProgress = useMemo(
() =>
lenses?.progress && lens
? {
files: lenses.progress.files.map((file) => ({
path: file.path,
...coverageProgress([file], lens.ranges),
viewedRanges: coverageSources(file),
changedRanges: coverageSources(file, file.changed),
unfoldRanges: lenses.unfoldRanges.filter(
(source) =>
source.file ===
(source.side === "base"
? (file.previousPath ?? file.path)
: file.path),
),
})),
changedPaths: lenses.changedPaths,
}
: undefined,
[lenses?.progress, lenses?.changedPaths, lenses?.unfoldRanges, lens],
);
const fullProgress = useMemo(() => {
if (!lenses?.progress) return undefined;
const unfold = rangesByFile(lenses.unfoldRanges);

return {
files: lenses.progress.files.map((file) => ({
path: file.path,
...coverageProgress([file]),
viewedRanges: coverageSources(file),
changedRanges: coverageSources(file, file.changed),
unfoldRanges: unfold(file),
})),
changedPaths: lenses.changedPaths,
};
}, [lenses?.progress, lenses?.changedPaths, lenses?.unfoldRanges]);

const lensProgress = useMemo(() => {
if (!lenses?.progress || !lens) return undefined;
const unfold = rangesByFile(lenses.unfoldRanges);
const scope = rangesByFile(lens.ranges);

return {
files: lenses.progress.files.map((file) => ({
path: file.path,
...coverageProgress([file], scope(file)),
viewedRanges: coverageSources(file),
changedRanges: coverageSources(file, file.changed),
unfoldRanges: unfold(file),
})),
changedPaths: lenses.changedPaths,
};
}, [lenses?.progress, lenses?.changedPaths, lenses?.unfoldRanges, lens]);

const markFile = (path: string, scoped: boolean) => {
const file = lenses?.progress?.files.find((file) => file.path === path);

if (!file || !lenses) return;

const sources = scoped
? lens?.ranges.filter(
(source) =>
source.file ===
(source.side === "base"
? (file.previousPath ?? file.path)
: file.path),
)
? lens && rangesByFile(lens.ranges)(file)
: coverageSources(file, file.changed);

void lenses.mark(sources, lenses.stats(sources).state !== "viewed");
Expand Down Expand Up @@ -450,18 +431,48 @@ export function ReviewDiffView({
);
}

type FileRange = ReviewDiffLens["ranges"][number];

/**
* Each file's own ranges, grouped once. A large diff has thousands of files
* and ranges; filtering every range for every file is quadratic.
*/
function rangesByFile(ranges: readonly FileRange[]) {
const byFile = Map.groupBy(ranges, (source) => source.file);

return (file: { path: string; previousPath?: string }): FileRange[] => {
const base = file.previousPath ?? file.path;

const named =
base === file.path
? (byFile.get(base) ?? [])
: [...(byFile.get(base) ?? []), ...(byFile.get(file.path) ?? [])];

return named.filter(
(source) => source.file === (source.side === "base" ? base : file.path),
);
};
}

/** How many changed files a lens covers. */
function lensFileCount(lenses: ReviewLenses, lens: ReviewDiffLens): number {
// The first file with a name owns it, as a scan of the files would find it.
const owners = {
base: new Map<string, string>(),
head: new Map<string, string>(),
};

for (const file of lenses.progress?.files ?? []) {
const base = file.previousPath ?? file.path;

if (!owners.base.has(base)) owners.base.set(base, file.path);

if (!owners.head.has(file.path)) owners.head.set(file.path, file.path);
}

return new Set(
lens.ranges.map(
(source) =>
lenses.progress?.files.find(
(file) =>
source.file ===
(source.side === "base"
? (file.previousPath ?? file.path)
: file.path),
)?.path ?? source.file,
(source) => owners[source.side].get(source.file) ?? source.file,
),
).size;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/review/app/src/review-lenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ export function ReviewLensesProvider({
? source.comparison
: undefined;

groups.set(key, [...(groups.get(key) ?? []), source]);
const group = groups.get(key);

if (group) group.push(source);
else groups.set(key, [source]);
}

return mergeCoverageProgress(
Expand Down