Skip to content

Commit b2de5b8

Browse files
feat(web): find in diffs with Cmd+F
The diff panel and PR Code tab render through Pierre's virtualized CodeView, so neither the browser's find nor the File view's editor search reached them. Patch @pierre/diffs to give CodeView a find panel that reuses the editor's SearchPanelWidget. It searches every diff item's line data, so matches in folded files, collapsed context, and unmounted rows are found; navigating reveals them, unfolding a file through a new onRevealSearchMatch callback. Matches are painted with the CSS Custom Highlight API. Shortcut guards now see focus inside shadow roots, so typing in the panel no longer triggers right-panel letter shortcuts or thread undo. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5cc99e1 commit b2de5b8

11 files changed

Lines changed: 858 additions & 26 deletions

File tree

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ export default function DiffPanel({
11141114
},
11151115
}
11161116
: {})}
1117+
onRevealSearchMatch={toggleDiffFileCollapsed}
11171118
renderHeaderPrefix={(fileDiff, fileKey) => {
11181119
const unavailable = fileDiff.cacheKey?.endsWith(":pending") === true;
11191120
const collapsed = unavailable || collapsedDiffFileKeys.has(fileKey);

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ function RightPanelEmptyState(props: {
434434
const action = surfaceShortcutActionForKey(shortcutActionsRef.current, event);
435435
if (!action) return;
436436
if (document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS)) return;
437-
const target = event.target;
437+
// The composed path starts at the real target, which may sit inside a shadow root.
438+
const target = event.composedPath()[0] ?? event.target;
438439
if (target instanceof Element && surfaceShortcutTargetsTypingContext(target)) return;
439440
event.preventDefault();
440441
event.stopPropagation();

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ interface AnnotatableCodeViewProps {
9595
fileKey: string,
9696
collapsed: boolean,
9797
) => ReactNode;
98+
/** Unfold a collapsed file that holds the find match being navigated to. */
99+
onRevealSearchMatch: (fileKey: string) => void;
98100
}
99101

100102
interface DiffSelectionContext {
@@ -115,6 +117,7 @@ export function AnnotatableCodeView({
115117
renderHeaderMetadata,
116118
renderHeaderFilenameSuffix,
117119
renderHeaderPrefix,
120+
onRevealSearchMatch,
118121
}: AnnotatableCodeViewProps) {
119122
const addReviewComment = useComposerDraftStore((store) => store.addReviewComment);
120123
const removeReviewComment = useComposerDraftStore((store) => store.removeReviewComment);
@@ -262,6 +265,7 @@ export function AnnotatableCodeView({
262265
items={items}
263266
selectedLines={selectedLines}
264267
onSelectedLinesChange={setSelectedLines}
268+
onRevealSearchMatch={(item) => onRevealSearchMatch(item.id)}
265269
options={{
266270
...options,
267271
enableGutterUtility: !hasOpenComment,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { CodeViewItem } from "@pierre/diffs";
2+
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
// Find-in-diff lives in our @pierre/diffs patch and is not a package export.
6+
const searchUrl = new URL("./components/CodeViewSearch.js", import.meta.resolve("@pierre/diffs"));
7+
const { collectMatches } = (await import(/* @vite-ignore */ searchUrl.href)) as {
8+
collectMatches(
9+
items: ReadonlyArray<CodeViewItem<undefined>>,
10+
params: { text: string; caseSensitive: boolean; wholeWord: boolean; regex: boolean },
11+
): Array<{ id: string; side: string; lineNumber: number; start: number; end: number }>;
12+
};
13+
14+
const PATCH = `diff --git a/src/a.ts b/src/a.ts
15+
index 1111111..2222222 100644
16+
--- a/src/a.ts
17+
+++ b/src/a.ts
18+
@@ -1,4 +1,5 @@
19+
const keep = 1;
20+
-const oldName = 2;
21+
+const newName = 2;
22+
+const added = oldName;
23+
const tail = 4;
24+
@@ -20,3 +21,3 @@ function x() {
25+
ctx20 name
26+
-del21 Name
27+
+add22 names
28+
ctx22
29+
diff --git a/src/b.ts b/src/b.ts
30+
index 3333333..4444444 100644
31+
--- a/src/b.ts
32+
+++ b/src/b.ts
33+
@@ -1,1 +1,1 @@
34+
-nil
35+
+: nil
36+
`;
37+
38+
const items: CodeViewItem<undefined>[] = parsePatchFiles(PATCH)[0]!.files.map(
39+
(fileDiff, index) => ({ id: `file-${index}`, type: "diff", fileDiff, collapsed: index === 1 }),
40+
);
41+
42+
function find(
43+
text: string,
44+
flags: { caseSensitive?: boolean; wholeWord?: boolean; regex?: boolean } = {},
45+
) {
46+
return collectMatches(items, {
47+
text,
48+
caseSensitive: flags.caseSensitive ?? false,
49+
wholeWord: flags.wholeWord ?? false,
50+
regex: flags.regex ?? false,
51+
}).map(({ id, side, lineNumber, start, end }) => `${id}:${side}:${lineNumber}:${start}-${end}`);
52+
}
53+
54+
describe("collectMatches", () => {
55+
it("finds removed, added, and context lines in render order, folded files included", () => {
56+
expect(find("oldName")).toEqual(["file-0:deletions:2:6-13", "file-0:additions:3:14-21"]);
57+
expect(find("nil")).toEqual(["file-1:deletions:1:0-3", "file-1:additions:1:2-5"]);
58+
});
59+
60+
it("honours case, whole-word, and regex toggles", () => {
61+
expect(find("Name", { caseSensitive: true })).toEqual([
62+
"file-0:deletions:2:9-13",
63+
"file-0:additions:2:9-13",
64+
"file-0:additions:3:17-21",
65+
"file-0:deletions:21:6-10",
66+
]);
67+
expect(find("name", { wholeWord: true })).toEqual([
68+
"file-0:additions:21:6-10",
69+
"file-0:deletions:21:6-10",
70+
]);
71+
expect(find("n[a-z]+e", { regex: true })).toHaveLength(6);
72+
});
73+
74+
it("returns nothing for an empty query or an invalid pattern", () => {
75+
expect(find("")).toEqual([]);
76+
expect(find("(", { regex: true })).toEqual([]);
77+
});
78+
});

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,8 @@ function PullRequestCodeTab({
15191519
renderHeaderPrefix={renderHeaderPrefix}
15201520
renderHeaderMetadata={renderHeaderMetadata}
15211521
renderAnnotation={renderAnnotation}
1522+
// Find only asks this for a folded file, so toggling it is unfolding it.
1523+
onRevealSearchMatch={(item) => toggleFile(item.id)}
15221524
unsafeCSSExtra={REPLACE_FILE_COUNTS_CSS}
15231525
/>
15241526
</div>

‎apps/web/src/index.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,14 @@ code {
20302030
color: var(--code-foreground);
20312031
}
20322032

2033+
/* Pierre's find panel and match highlights read these. */
2034+
.diff-render-surface {
2035+
--diffs-search-panel-bg: var(--popover);
2036+
--diffs-search-panel-fg: var(--popover-foreground);
2037+
--diffs-search-panel-accent: var(--primary);
2038+
--diffs-search-panel-font: var(--font-sans);
2039+
}
2040+
20332041
/* Pierre's file renderer owns its own surface tokens. Keep the root of a file
20342042
preview on the same themeable code surface as the diff panel. */
20352043
.diff-render-surface diffs-container,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @vitest-environment jsdom
2+
3+
import { afterEach, describe, expect, it } from "vite-plus/test";
4+
5+
import { isEditableFocused } from "./editableFocus";
6+
7+
afterEach(() => {
8+
document.body.replaceChildren();
9+
});
10+
11+
describe("isEditableFocused", () => {
12+
it("sees a text field focused inside an open shadow root", () => {
13+
// A key event from a shadow input is retargeted to its host, so the host is
14+
// what page-level shortcut handlers receive as the event target.
15+
const host = document.createElement("div");
16+
const input = document.createElement("input");
17+
host.attachShadow({ mode: "open" }).append(input);
18+
document.body.append(host);
19+
20+
expect(isEditableFocused(host)).toBe(false);
21+
input.focus();
22+
expect(isEditableFocused(host)).toBe(true);
23+
expect(isEditableFocused()).toBe(true);
24+
});
25+
});

‎apps/web/src/lib/editableFocus.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const EDITABLE_SELECTOR = [
1111
/**
1212
* Whether a text-editing element owns the keyboard. Shortcuts that share
1313
* their chord with native editing (mod+z) must yield when this is true.
14+
* Focus inside an open shadow root counts: a key event's target is retargeted
15+
* to the shadow host, so the host's focused descendant is what is typed into.
1416
*/
1517
export function isEditableFocused(target: EventTarget | null = document.activeElement): boolean {
16-
return target instanceof Element && target.closest(EDITABLE_SELECTOR) !== null;
18+
let element = target instanceof Element ? target : null;
19+
while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
20+
return element !== null && element.closest(EDITABLE_SELECTOR) !== null;
1721
}

‎docs/user/keybindings.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ to copy its URL and `mod+shift+k` to copy its number with a `#` prefix.
3737
Both shortcuts can be changed in Settings. Search for “Copy Link or Thread ID”
3838
or “Copy Number”. They copy the selected PR and leave terminal input alone.
3939

40+
## Find in a diff
41+
42+
Click into a diff in the Diff panel or a pull request's Code tab, then press
43+
`mod+f` to search every file in it, including folded files and unchanged lines
44+
hidden between changes. Enter and `Shift+Enter` move between matches, and a
45+
match in a folded file opens it. Escape closes the search. This shortcut is not
46+
configurable.
47+
4048
## iPad
4149

4250
With a hardware keyboard, use `Cmd+1` through `Cmd+9` to open the first nine

0 commit comments

Comments
 (0)