mirror of
https://seed.radicle.dev/rad:z4D5UCArafTzTQpDZNQRuqswh3ury.git
synced 2026-11-29 06:40:29 +08:00
Render patch changes with Pierre
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
<script lang="ts">
|
||||
import type { CodeComments } from "@app/components/Diff.svelte";
|
||||
import type { Author } from "@bindings/cob/Author";
|
||||
import type { CodeLocation } from "@bindings/cob/thread/CodeLocation";
|
||||
import type { Embed } from "@bindings/cob/thread/Embed";
|
||||
import type { Thread } from "@bindings/cob/thread/Thread";
|
||||
import type { Config } from "@bindings/config/Config";
|
||||
import type { Diff as DiffType } from "@bindings/diff/Diff";
|
||||
import type { FileDiff } from "@bindings/diff/FileDiff";
|
||||
import type { Hunk } from "@bindings/diff/Hunk";
|
||||
|
||||
import { cachedGetDiff } from "@app/lib/invoke";
|
||||
import type { CodeComments } from "@app/lib/codeComments";
|
||||
import { diffOptions } from "@app/lib/diffOptions.svelte";
|
||||
import { cachedGetDiffText } from "@app/lib/invoke";
|
||||
import { sliceForRange } from "@app/lib/patchSlice";
|
||||
|
||||
import Diff from "@app/components/Diff.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
import PierreSnippet from "@app/components/PierreSnippet.svelte";
|
||||
|
||||
interface Props {
|
||||
rid: string;
|
||||
@@ -53,9 +51,6 @@
|
||||
reactOnComment,
|
||||
}: Props = $props();
|
||||
|
||||
// Per-thread hunk expand state, keyed by the thread's root comment id.
|
||||
let expandedThreads: Record<string, boolean> = $state({});
|
||||
|
||||
const noop = () => Promise.resolve();
|
||||
|
||||
// All threads passed to one instance belong to the same file, so the file
|
||||
@@ -77,110 +72,18 @@
|
||||
};
|
||||
}
|
||||
|
||||
function findFileDiff(
|
||||
diff: DiffType,
|
||||
/// Which lines of which side a comment is about, in the numbering
|
||||
/// `sliceForRange` matches on.
|
||||
function rangeOf(
|
||||
location: CodeLocation,
|
||||
): FileDiff | undefined {
|
||||
return diff.files.find(file => {
|
||||
if (file.status === "moved" || file.status === "copied") {
|
||||
return file.oldPath === location.path || file.newPath === location.path;
|
||||
}
|
||||
return file.path === location.path;
|
||||
});
|
||||
}
|
||||
|
||||
function findHunk(file: FileDiff, location: CodeLocation): Hunk | undefined {
|
||||
if (file.diff.type !== "plain") return undefined;
|
||||
): { side: "old" | "new"; start: number; end: number } | undefined {
|
||||
const range = location.new ?? location.old;
|
||||
if (!range || range.type !== "lines") return undefined;
|
||||
const targetSide: "old" | "new" = location.new ? "new" : "old";
|
||||
|
||||
return file.diff.hunks.find(hunk => {
|
||||
const hunkRange = hunk[targetSide];
|
||||
return (
|
||||
hunkRange.start <= range.range.start && hunkRange.end >= range.range.end
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function sliceHunkAroundRange(
|
||||
hunk: Hunk,
|
||||
range: { start: number; end: number },
|
||||
side: "old" | "new",
|
||||
context: number,
|
||||
): Hunk {
|
||||
const matches: number[] = [];
|
||||
hunk.lines.forEach((line, idx) => {
|
||||
let lineNo: number | undefined;
|
||||
if (line.type === "context") {
|
||||
lineNo = side === "new" ? line.lineNoNew : line.lineNoOld;
|
||||
} else if (side === "new" && line.type === "addition") {
|
||||
lineNo = line.lineNo;
|
||||
} else if (side === "old" && line.type === "deletion") {
|
||||
lineNo = line.lineNo;
|
||||
}
|
||||
if (lineNo !== undefined && lineNo >= range.start && lineNo < range.end) {
|
||||
matches.push(idx);
|
||||
}
|
||||
});
|
||||
|
||||
if (matches.length === 0) return hunk;
|
||||
|
||||
const startIdx = Math.max(0, matches[0] - context);
|
||||
const endIdx = Math.min(
|
||||
hunk.lines.length - 1,
|
||||
matches[matches.length - 1] + context,
|
||||
);
|
||||
|
||||
if (startIdx === 0 && endIdx === hunk.lines.length - 1) return hunk;
|
||||
|
||||
const slicedLines = hunk.lines.slice(startIdx, endIdx + 1);
|
||||
|
||||
let oldStart: number | undefined;
|
||||
let oldEnd: number | undefined;
|
||||
let newStart: number | undefined;
|
||||
let newEnd: number | undefined;
|
||||
for (const line of slicedLines) {
|
||||
if (line.type === "context") {
|
||||
oldStart ??= line.lineNoOld;
|
||||
oldEnd = line.lineNoOld;
|
||||
newStart ??= line.lineNoNew;
|
||||
newEnd = line.lineNoNew;
|
||||
} else if (line.type === "addition") {
|
||||
newStart ??= line.lineNo;
|
||||
newEnd = line.lineNo;
|
||||
} else if (line.type === "deletion") {
|
||||
oldStart ??= line.lineNo;
|
||||
oldEnd = line.lineNo;
|
||||
}
|
||||
if (!range) return undefined;
|
||||
const side = location.new ? "new" : "old";
|
||||
if (range.type === "chars") {
|
||||
return { side, start: range.line, end: range.line + 1 };
|
||||
}
|
||||
|
||||
const slicedOld =
|
||||
oldStart !== undefined && oldEnd !== undefined
|
||||
? { start: oldStart, end: oldEnd + 1 }
|
||||
: hunk.old;
|
||||
const slicedNew =
|
||||
newStart !== undefined && newEnd !== undefined
|
||||
? { start: newStart, end: newEnd + 1 }
|
||||
: hunk.new;
|
||||
|
||||
const oldLen = slicedOld.end - slicedOld.start;
|
||||
const newLen = slicedNew.end - slicedNew.start;
|
||||
|
||||
return {
|
||||
header: `@@ -${slicedOld.start},${oldLen} +${slicedNew.start},${newLen} @@`,
|
||||
lines: slicedLines,
|
||||
old: slicedOld,
|
||||
new: slicedNew,
|
||||
};
|
||||
}
|
||||
|
||||
function sliceFileDiff(file: FileDiff, hunk: Hunk): FileDiff {
|
||||
if (file.diff.type !== "plain") return file;
|
||||
return {
|
||||
...file,
|
||||
diff: { ...file.diff, hunks: [hunk] },
|
||||
};
|
||||
return { side, start: range.range.start, end: range.range.end };
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -190,10 +93,6 @@
|
||||
border-radius: var(--border-radius-md);
|
||||
overflow: hidden;
|
||||
}
|
||||
.wrapper :global(.container) {
|
||||
padding-top: 0;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
.file-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -214,90 +113,45 @@
|
||||
.thread + .thread {
|
||||
border-top: 1px solid var(--color-border-subtle);
|
||||
}
|
||||
.hunk-toolbar {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding: 0.25rem 0.5rem 0;
|
||||
}
|
||||
.toggle {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 0.125rem;
|
||||
border-radius: var(--border-radius-sm);
|
||||
cursor: pointer;
|
||||
color: var(--color-text-tertiary);
|
||||
}
|
||||
.toggle:hover,
|
||||
.toggle:focus-visible {
|
||||
color: var(--color-text-primary);
|
||||
background-color: var(--color-surface-subtle);
|
||||
}
|
||||
.fallback {
|
||||
font: var(--txt-body-m-regular);
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
</style>
|
||||
|
||||
{#await cachedGetDiff(rid, { base, head, unified: 3, highlight: true })}
|
||||
<div class="fallback">Loading code…</div>
|
||||
{:then diff}
|
||||
{@const location0 = threads[0]?.root.location}
|
||||
{#if path && location0}
|
||||
{@const file = findFileDiff(diff, location0)}
|
||||
{#if file}
|
||||
<div class="wrapper">
|
||||
<div class="file-header">
|
||||
<span class="file-path">{path}</span>
|
||||
</div>
|
||||
{#each threads as thread (thread.root.id)}
|
||||
{@const location = thread.root.location}
|
||||
{#if location}
|
||||
{@const hunk = findHunk(file, location)}
|
||||
{#if hunk}
|
||||
{@const codeRange = location.new ?? location.old}
|
||||
{@const targetSide = location.new ? "new" : "old"}
|
||||
{@const clampedHunk =
|
||||
codeRange && codeRange.type === "lines"
|
||||
? sliceHunkAroundRange(hunk, codeRange.range, targetSide, 3)
|
||||
: hunk}
|
||||
{@const isClamped = clampedHunk !== hunk}
|
||||
{@const expanded = expandedThreads[thread.root.id] ?? false}
|
||||
{@const displayHunk = expanded ? hunk : clampedHunk}
|
||||
<div class="thread">
|
||||
{#if isClamped}
|
||||
<div class="hunk-toolbar">
|
||||
<button
|
||||
class="toggle"
|
||||
type="button"
|
||||
title={expanded ? "Collapse hunk" : "Expand hunk"}
|
||||
onclick={() =>
|
||||
(expandedThreads = {
|
||||
...expandedThreads,
|
||||
[thread.root.id]: !expanded,
|
||||
})}>
|
||||
<Icon
|
||||
name={expanded
|
||||
? "collapse-vertical"
|
||||
: "expand-vertical"} />
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
<Diff
|
||||
file={sliceFileDiff(file, displayHunk)}
|
||||
{head}
|
||||
codeComments={codeCommentsFor(thread)} />
|
||||
</div>
|
||||
{:else}
|
||||
<div class="fallback">Code unavailable for {location.path}</div>
|
||||
{/if}
|
||||
{/if}
|
||||
{/each}
|
||||
{#if path}
|
||||
<!-- The file's own patch text, which is all the renderer needs; the structured
|
||||
diff is not fetched here at all. -->
|
||||
{#await cachedGetDiffText(rid, base, head, 3, path)}
|
||||
<div class="fallback">Loading code…</div>
|
||||
{:then filePatch}
|
||||
<div class="wrapper">
|
||||
<div class="file-header">
|
||||
<span class="file-path">{path}</span>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="fallback">Code unavailable for {path}</div>
|
||||
{/if}
|
||||
{/if}
|
||||
{/await}
|
||||
{#each threads as thread (thread.root.id)}
|
||||
{@const location = thread.root.location}
|
||||
{#if location}
|
||||
{@const range = rangeOf(location)}
|
||||
{@const slice =
|
||||
range &&
|
||||
sliceForRange(filePatch, range.side, range.start, range.end)}
|
||||
{#if slice}
|
||||
<div class="thread">
|
||||
<PierreSnippet
|
||||
patch={slice}
|
||||
{path}
|
||||
cacheKey={`snippet:${head}:${thread.root.id}`}
|
||||
threads={[thread]}
|
||||
codeComments={codeCommentsFor(thread)}
|
||||
diffIndicators={diffOptions.indicators}
|
||||
lineDiffType={diffOptions.lineDiffType} />
|
||||
</div>
|
||||
{:else}
|
||||
<div class="fallback">Code unavailable for {location.path}</div>
|
||||
{/if}
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{/await}
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user