Skip to content

Commit d482185

Browse files
committed
Unwrap patch promises and remove double parsing
1 parent 7acb5eb commit d482185

File tree

5 files changed

+16
-42
lines changed

5 files changed

+16
-42
lines changed

web/src/lib/components/diff/ConciseDiffView.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
type SearchSegment,
1414
} from "$lib/components/diff/concise-diff-view.svelte";
1515
import Spinner from "$lib/components/Spinner.svelte";
16-
import { type StructuredPatch } from "diff";
1716
import { onDestroy } from "svelte";
1817
import { type MutableValue } from "$lib/util";
1918
import { box } from "svelte-toolbelt";
@@ -33,7 +32,7 @@
3332
cacheKey,
3433
}: ConciseDiffViewProps<K> = $props();
3534
36-
const parsedPatch: Promise<StructuredPatch> = $derived.by(async () => {
35+
const parsedPatch = $derived.by(() => {
3736
if (rawPatchContent !== undefined) {
3837
return parseSinglePatch(rawPatchContent);
3938
} else if (patch !== undefined) {

web/src/lib/components/diff/concise-diff-view.svelte.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ export function parseSinglePatch(rawPatchContent: string): StructuredPatch {
10131013

10141014
export interface ConciseDiffViewProps<K> {
10151015
rawPatchContent?: string;
1016-
patch?: Promise<StructuredPatch>;
1016+
patch?: StructuredPatch;
10171017

10181018
syntaxHighlighting?: boolean;
10191019
syntaxHighlightingTheme?: BundledTheme;
@@ -1030,7 +1030,7 @@ export interface ConciseDiffViewProps<K> {
10301030
}
10311031

10321032
export type ConciseDiffViewStateProps<K> = ReadableBoxedValues<{
1033-
patch: Promise<StructuredPatch>;
1033+
patch: StructuredPatch;
10341034

10351035
syntaxHighlighting: boolean;
10361036
syntaxHighlightingTheme: BundledTheme | undefined;

web/src/lib/diff-viewer-multi-file.svelte.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fetchGithubCommitDiff, fetchGithubComparison, fetchGithubPRComparison, type FileStatus, getGithubToken, type GithubDiff } from "./github.svelte";
2-
import { type StructuredPatch, parsePatch } from "diff";
2+
import { type StructuredPatch } from "diff";
33
import {
44
ConciseDiffViewCachedState,
55
DEFAULT_THEME_DARK,
@@ -150,8 +150,7 @@ export type CommonFileDetails = {
150150

151151
export type TextFileDetails = CommonFileDetails & {
152152
type: "text";
153-
patchText: string;
154-
structuredPatch: Promise<StructuredPatch>;
153+
structuredPatch: StructuredPatch;
155154
};
156155

157156
export type ImageFileDetails = CommonFileDetails & {
@@ -165,8 +164,7 @@ export function makeTextDetails(fromFile: string, toFile: string, status: FileSt
165164
fromFile,
166165
toFile,
167166
status,
168-
patchText,
169-
structuredPatch: (async () => parseSinglePatch(patchText))(),
167+
structuredPatch: parseSinglePatch(patchText),
170168
};
171169
}
172170

@@ -286,35 +284,20 @@ export function getFileStatusProps(status: FileStatus): FileStatusProps {
286284
}
287285

288286
export function findHeaderChangeOnlyPatches(fileDetails: FileDetails[]) {
289-
const patchStrings = fileDetails.map((details) => {
290-
if (details.type === "text") {
291-
return details.patchText;
292-
}
293-
return undefined;
294-
});
295-
296287
const result: boolean[] = [];
297288

298-
for (let i = 0; i < patchStrings.length; i++) {
299-
const patchString = patchStrings[i];
300-
if (patchString === undefined || patchString.length === 0) {
301-
result.push(false);
302-
continue;
303-
}
304-
// TODO: Parsing twice is wasteful
305-
const patches = parsePatch(patchString);
306-
if (patches.length !== 1) {
289+
for (const details of fileDetails) {
290+
if (details.type !== "text") {
307291
result.push(false);
308292
continue;
309293
}
310-
const patch = patches[0];
311-
if (patch.hunks.length === 0) {
294+
if (details.structuredPatch.hunks.length === 0) {
312295
result.push(false);
313296
continue;
314297
}
315298
let onlyHeaderChanges = true;
316-
for (let j = 0; j < patch.hunks.length; j++) {
317-
if (hasNonHeaderChanges(patch.hunks[j].lines)) {
299+
for (let j = 0; j < details.structuredPatch.hunks.length; j++) {
300+
if (hasNonHeaderChanges(details.structuredPatch.hunks[j].lines)) {
318301
onlyHeaderChanges = false;
319302
}
320303
}
@@ -368,7 +351,7 @@ export class MultiFileDiffViewerState {
368351

369352
readonly fileTreeFilterDebounced = new Debounced(() => this.fileTreeFilter, 500);
370353
readonly searchQueryDebounced = new Debounced(() => this.searchQuery, 500);
371-
readonly stats: Promise<ViewerStatistics> = $derived(this.countStats());
354+
readonly stats: ViewerStatistics = $derived(this.countStats());
372355
readonly fileTreeRoots: TreeNode<FileTreeNodeData>[] = $derived(makeFileTree(this.fileDetails));
373356
readonly filteredFileDetails: FileDetails[] = $derived(
374357
this.fileTreeFilterDebounced.current ? this.fileDetails.filter((f) => this.filterFile(f)) : this.fileDetails,
@@ -543,7 +526,7 @@ export class MultiFileDiffViewerState {
543526
return false;
544527
}
545528

546-
private async countStats(): Promise<ViewerStatistics> {
529+
private countStats(): ViewerStatistics {
547530
let addedLines = 0;
548531
let removedLines = 0;
549532
const fileAddedLines: number[] = [];
@@ -554,7 +537,7 @@ export class MultiFileDiffViewerState {
554537
if (details.type !== "text") {
555538
continue;
556539
}
557-
const diff = await details.structuredPatch;
540+
const diff = details.structuredPatch;
558541

559542
for (let j = 0; j < diff.hunks.length; j++) {
560543
const hunk = diff.hunks[j];

web/src/routes/+page.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,7 @@
283283
</div>
284284
<div class="flex flex-row items-center gap-2 px-3 pb-2">
285285
<SidebarToggle class="data-[side=right]:order-10" />
286-
{#await viewer.stats}
287-
<DiffStats />
288-
{:then stats}
289-
<DiffStats add={stats.addedLines} remove={stats.removedLines} />
290-
{/await}
286+
<DiffStats add={viewer.stats.addedLines} remove={viewer.stats.removedLines} />
291287
<DiffSearch />
292288
</div>
293289
<div class="flex flex-1 flex-col border-t">

web/src/routes/FileHeader.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@
9797
onkeyup={(event) => event.key === "Enter" && viewer.scrollToFile(index, { autoExpand: false, smooth: true })}
9898
>
9999
{#if value.type === "text"}
100-
{#await viewer.stats}
101-
<DiffStats brief />
102-
{:then stats}
103-
<DiffStats brief add={stats.fileAddedLines[index]} remove={stats.fileRemovedLines[index]} />
104-
{/await}
100+
<DiffStats brief add={viewer.stats.fileAddedLines[index]} remove={viewer.stats.fileRemovedLines[index]} />
105101
{/if}
106102
{@render fileName()}
107103
<div class="ms-0.5 ml-auto flex items-center gap-2">

0 commit comments

Comments
 (0)