@@ -30,7 +30,7 @@ index cc32d28..3a46bf4 100644
3030 * Returns the editor while this item is being edited. Partial diffs get an
3131 * editor after their files load; scrolling away or collapsing keeps it.
3232diff --git a/dist/components/CodeView.js b/dist/components/CodeView.js
33- index 140f94f..12facc8 100644
33+ index 140f94f..96fc774 100644
3434--- a/dist/components/CodeView.js
3535+++ b/dist/components/CodeView.js
3636@@ -16,6 +16,7 @@ import { prefersReducedMotion } from "../utils/prefersReducedMotion.js";
@@ -84,7 +84,7 @@ index 140f94f..12facc8 100644
8484 this.root?.style.removeProperty("overflow-anchor");
8585 this.container?.remove();
8686 this.stickyOffset.remove();
87- @@ -626,6 +635,125 @@ var CodeView = class CodeView {
87+ @@ -626,6 +635,137 @@ var CodeView = class CodeView {
8888 getItem(itemId) {
8989 return this.idToItem.get(itemId)?.item;
9090 }
@@ -98,8 +98,20 @@ index 140f94f..12facc8 100644
9898+ getSearchItems() {
9999+ return this.items.map((item) => item.item);
100100+ }
101- + getFirstVisibleItemIndex() {
102- + return this.items.length === 0 ? 0 : this.findFirstVisibleIndex(Math.max(this.getScrollTop() - this.getItemTopOffset(), 0));
101+ + /** Index of the first match at or below the top of the viewport, or 0 when every match is above it. */
102+ + getFirstVisibleSearchMatchIndex(entries) {
103+ + if (this.items.length === 0) return 0;
104+ + const visibleTop = this.getScrollTop() - this.getItemTopOffset() + this.getStickyHeaderOffset();
105+ + const itemIndex = this.findFirstVisibleIndex(Math.max(this.getScrollTop() - this.getItemTopOffset(), 0));
106+ + const item = this.items[itemIndex];
107+ + for (let index = 0; index < entries.length; index++) {
108+ + const entry = entries[index];
109+ + if (entry.itemIndex < itemIndex) continue;
110+ + if (entry.itemIndex > itemIndex) return index;
111+ + const position = this.getLineScrollPosition(item, entry);
112+ + if (position == null || item.top + position.top >= visibleTop) return index;
113+ + }
114+ + return 0;
103115+ }
104116+ /**
105117+ * Bring a match on screen. A folded file is unfolded by the host through
@@ -210,39 +222,39 @@ index 140f94f..12facc8 100644
210222 /**
211223 * Returns the editor while this item is being edited. Partial diffs get an
212224 * editor after their files load; scrolling away or collapsing keeps it.
213- @@ -645,6 +773 ,7 @@ var CodeView = class CodeView {
225+ @@ -645,6 +785 ,7 @@ var CodeView = class CodeView {
214226 this.render();
215227 this.syncItemEditors();
216228 this.syncSelection();
217229+ this.search.refresh();
218230 return true;
219231 }
220232 updateItemId(oldId, newId) {
221- @@ -678,6 +807 ,7 @@ var CodeView = class CodeView {
233+ @@ -678,6 +819 ,7 @@ var CodeView = class CodeView {
222234 this.renamePendingScrollTarget(oldId, newId);
223235 this.renamePendingLayoutAnchor(oldId, newId);
224236 this.render();
225237+ this.search.refresh();
226238 return true;
227239 }
228240 addItem(input) {
229- @@ -687,6 +817 ,7 @@ var CodeView = class CodeView {
241+ @@ -687,6 +829 ,7 @@ var CodeView = class CodeView {
230242 this.appendItemsInternal(inputs);
231243 this.syncItemEditors();
232244 this.syncSelection();
233245+ this.search.refresh();
234246 }
235247 removeItem(itemId) {
236248 const item = this.idToItem.get(itemId);
237- @@ -709,6 +840 ,7 @@ var CodeView = class CodeView {
249+ @@ -709,6 +852 ,7 @@ var CodeView = class CodeView {
238250 for (const removedItem of removedItemsById?.values() ?? []) removedItem.instance.cleanUp();
239251 }
240252 this.syncSelection();
241253+ this.search.refresh();
242254 }
243255 /**
244256 * Append new records to the viewer while preserving existing layout state.
245- @@ -1330,6 +1462 ,10 @@ var CodeView = class CodeView {
257+ @@ -1330,6 +1474 ,10 @@ var CodeView = class CodeView {
246258 item.item = nextItem;
247259 item.version = nextItem.version;
248260 item.renderedOptionsRevision = -1;
@@ -253,7 +265,7 @@ index 140f94f..12facc8 100644
253265 return true;
254266 }
255267 getMaxScrollTopForHeight(scrollHeight) {
256- @@ -1710,6 +1846 ,10 @@ var CodeView = class CodeView {
268+ @@ -1710,6 +1858 ,10 @@ var CodeView = class CodeView {
257269 this.updateStickyPositioning();
258270 this.validateStickyContainerHeight();
259271 this.fixContainerFocus();
@@ -264,7 +276,7 @@ index 140f94f..12facc8 100644
264276 if (fitPerfectly || this.scrollAnimation != null) this.render();
265277 };
266278 flushManagers(updatedItems) {
267- @@ -1804,7 +1944 ,8 @@ var CodeView = class CodeView {
279+ @@ -1804,7 +1956 ,8 @@ var CodeView = class CodeView {
268280 this.notifyScroll();
269281 this.render();
270282 };
@@ -274,7 +286,7 @@ index 140f94f..12facc8 100644
274286 this.pendingScrollTarget = void 0;
275287 this.pendingLayoutAnchor = void 0;
276288 this.scrollAnimation = void 0;
277- @@ -2066,7 +2207 ,12 @@ var CodeView = class CodeView {
289+ @@ -2066,7 +2219 ,12 @@ var CodeView = class CodeView {
278290 const item = this.items[index];
279291 if (item == null) throw new Error("CodeView.recomputeLayout: invalid item index");
280292 item.top = runningTop;
@@ -288,7 +300,7 @@ index 140f94f..12facc8 100644
288300 else item.height = item.instance.updateCodeViewLayout(item.item.file, runningTop, reset, item.item.annotations ?? []);
289301 runningTop += item.height;
290302 if (index < this.items.length - 1) runningTop += layout.gap;
291- @@ -2086,6 +2232 ,27 @@ var CodeView = class CodeView {
303+ @@ -2086,6 +2244 ,27 @@ var CodeView = class CodeView {
292304 return this.getLayout().gap + this.itemMetricsCache.diffHeaderHeight;
293305 }
294306 };
@@ -318,10 +330,10 @@ index 140f94f..12facc8 100644
318330 if (item.type === "diff") return item.instance.updateCodeViewLayout(item.item.fileDiff, item.top, void 0, item.item.annotations ?? []);
319331diff --git a/dist/components/CodeViewSearch.js b/dist/components/CodeViewSearch.js
320332new file mode 100644
321- index 0000000..7d80244
333+ index 0000000..067ae92
322334--- /dev/null
323335+++ b/dist/components/CodeViewSearch.js
324- @@ -0,0 +1,362 @@
336+ @@ -0,0 +1,357 @@
325337+ import { isPrimaryModifier } from "../editor/platform.js";
326338+ import { resolveFindAgainShortcut } from "../editor/command.js";
327339+ import { SearchPanelWidget } from "../editor/searchPanel.js";
@@ -422,7 +434,7 @@ index 0000000..7d80244
422434+ this.#paint();
423435+ return this.#current != null ? matches[this.#current] : void 0;
424436+ }
425- + const next = matches[this.#current ?? this.#firstMatchFromViewport( )];
437+ + const next = matches[this.#current ?? this.#view.getFirstVisibleSearchMatchIndex(this.#entries )];
426438+ if (next != null) this.#goTo(next[0]);
427439+ return next;
428440+ },
@@ -512,11 +524,6 @@ index 0000000..7d80244
512524+ this.#paint();
513525+ }
514526+ }
515- + #firstMatchFromViewport() {
516- + const itemIndex = this.#view.getFirstVisibleItemIndex();
517- + const index = this.#entries.findIndex((entry) => entry.itemIndex >= itemIndex);
518- + return index === -1 ? 0 : index;
519- + }
520527+ #goTo(offset) {
521528+ const index = this.#entries.findIndex((entry) => entry.range[0] === offset);
522529+ if (index === -1) return;
0 commit comments