Skip to content

Commit 5b0f581

Browse files
committed
Remove parallel array for tracking header-change-only patches
1 parent d482185 commit 5b0f581

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,19 @@ async function makeHunk(
676676
return { lines };
677677
}
678678

679+
export function patchHeaderDiffOnly(patch: StructuredPatch): boolean {
680+
if (patch.hunks.length === 0) {
681+
return false;
682+
}
683+
let onlyHeaderChanges = true;
684+
for (let j = 0; j < patch.hunks.length; j++) {
685+
if (hasNonHeaderChanges(patch.hunks[j].lines)) {
686+
onlyHeaderChanges = false;
687+
}
688+
}
689+
return onlyHeaderChanges;
690+
}
691+
679692
export function hasNonHeaderChanges(contentLines: string[]) {
680693
for (const line of contentLines) {
681694
if (lineHasNonHeaderChange(line)) {

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

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
ConciseDiffViewCachedState,
55
DEFAULT_THEME_DARK,
66
DEFAULT_THEME_LIGHT,
7-
hasNonHeaderChanges,
87
isNoNewlineAtEofLine,
98
parseSinglePatch,
9+
patchHeaderDiffOnly,
1010
} from "$lib/components/diff/concise-diff-view.svelte";
1111
import type { BundledTheme } from "shiki";
1212
import { browser } from "$app/environment";
@@ -151,6 +151,7 @@ export type CommonFileDetails = {
151151
export type TextFileDetails = CommonFileDetails & {
152152
type: "text";
153153
structuredPatch: StructuredPatch;
154+
patchHeaderDiffOnly: boolean;
154155
};
155156

156157
export type ImageFileDetails = CommonFileDetails & {
@@ -159,12 +160,14 @@ export type ImageFileDetails = CommonFileDetails & {
159160
};
160161

161162
export function makeTextDetails(fromFile: string, toFile: string, status: FileStatus, patchText: string): TextFileDetails {
163+
const patch = parseSinglePatch(patchText);
162164
return {
163165
type: "text",
164166
fromFile,
165167
toFile,
166168
status,
167-
structuredPatch: parseSinglePatch(patchText),
169+
structuredPatch: patch,
170+
patchHeaderDiffOnly: patchHeaderDiffOnly(patch),
168171
};
169172
}
170173

@@ -283,30 +286,6 @@ export function getFileStatusProps(status: FileStatus): FileStatusProps {
283286
}
284287
}
285288

286-
export function findHeaderChangeOnlyPatches(fileDetails: FileDetails[]) {
287-
const result: boolean[] = [];
288-
289-
for (const details of fileDetails) {
290-
if (details.type !== "text") {
291-
result.push(false);
292-
continue;
293-
}
294-
if (details.structuredPatch.hunks.length === 0) {
295-
result.push(false);
296-
continue;
297-
}
298-
let onlyHeaderChanges = true;
299-
for (let j = 0; j < details.structuredPatch.hunks.length; j++) {
300-
if (hasNonHeaderChanges(details.structuredPatch.hunks[j].lines)) {
301-
onlyHeaderChanges = false;
302-
}
303-
}
304-
result.push(onlyHeaderChanges);
305-
}
306-
307-
return result;
308-
}
309-
310289
export type ViewerStatistics = {
311290
addedLines: number;
312291
removedLines: number;
@@ -356,14 +335,17 @@ export class MultiFileDiffViewerState {
356335
readonly filteredFileDetails: FileDetails[] = $derived(
357336
this.fileTreeFilterDebounced.current ? this.fileDetails.filter((f) => this.filterFile(f)) : this.fileDetails,
358337
);
359-
readonly patchHeaderDiffOnly: boolean[] = $derived(findHeaderChangeOnlyPatches(this.fileDetails));
360338
readonly searchResults: Promise<SearchResults> = $derived(this.findSearchResults());
361339

362340
private constructor() {
363341
// Auto-check all patch header diff only diffs
364342
$effect(() => {
365-
for (let i = 0; i < this.patchHeaderDiffOnly.length; i++) {
366-
if (this.patchHeaderDiffOnly[i] && this.checked[i] === undefined) {
343+
for (let i = 0; i < this.fileDetails.length; i++) {
344+
const details = this.fileDetails[i];
345+
if (details.type !== "text") {
346+
continue;
347+
}
348+
if (details.patchHeaderDiffOnly && this.checked[i] === undefined) {
367349
this.checked[i] = true;
368350
}
369351
}

web/src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
{/if}
322322
</div>
323323
{/if}
324-
{#if !viewer.collapsed[index] && value.type === "text" && (!viewer.patchHeaderDiffOnly[index] || !globalOptions.omitPatchHeaderOnlyHunks)}
324+
{#if !viewer.collapsed[index] && value.type === "text" && (!value.patchHeaderDiffOnly || !globalOptions.omitPatchHeaderOnlyHunks)}
325325
<div class="mb border-b">
326326
<ConciseDiffView
327327
patch={value.structuredPatch}

web/src/routes/FileHeader.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
});
2929
}
3030
}
31+
32+
let patchHeaderDiffOnly = $derived(value.type === "text" && value.patchHeaderDiffOnly);
3133
</script>
3234

3335
{#snippet fileName()}
@@ -101,11 +103,11 @@
101103
{/if}
102104
{@render fileName()}
103105
<div class="ms-0.5 ml-auto flex items-center gap-2">
104-
{#if viewer.patchHeaderDiffOnly[index]}
106+
{#if patchHeaderDiffOnly}
105107
<span class="rounded-sm bg-neutral-3 px-1.5">Patch-header-only diff</span>
106108
{/if}
107109
{@render actionsPopover()}
108-
{#if !viewer.patchHeaderDiffOnly[index] || !globalOptions.omitPatchHeaderOnlyHunks || value.type === "image"}
110+
{#if !patchHeaderDiffOnly || !globalOptions.omitPatchHeaderOnlyHunks || value.type === "image"}
109111
{@render collapseToggle()}
110112
{/if}
111113
</div>

0 commit comments

Comments
 (0)