Open History to see the last commit
1import * as NodeModule from "node:module";
2
3import type {
4 DirItem,
5 FileItem,
6 FileFinder as FileFinderType,
7 GrepCursor,
8 MixedItem,
9 Result,
10} from "@ff-labs/fff-node";
11import * as Context from "effect/Context";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14import * as LayerMap from "effect/LayerMap";
15import * as Schema from "effect/Schema";
16
17import type {
18 ProjectEntry,
19 ProjectEntryKind,
20 ProjectListEntriesResult,
21 ProjectSearchContentsInput,
22 ProjectSearchContentsResult,
23 ProjectSearchEntriesResult,
24} from "@t3tools/contracts";
25import * as HostProcess from "@t3tools/shared/HostProcess";
26import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
27
28import * as VcsProcess from "../vcs/VcsProcess.ts";
29
30// fff-node's native library stays external; single-executables require disk packages instead of importing them.
31const requireForFff = NodeModule.createRequire(import.meta.url);
32const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node");
33
34const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
35const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
36const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds";
37const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000;
38const WORKSPACE_INDEX_IDLE_TTL = "15 minutes";
39const CONTENT_SEARCH_TIME_BUDGET_MS = 250;
40const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100;
41
42export class WorkspaceSearchIndexCreateFailed extends Schema.TaggedError<WorkspaceSearchIndexCreateFailed>()(
43 "WorkspaceSearchIndexCreateFailed",
44 {
45 cwd: Schema.String,
46 reason: Schema.String,
47 cause: Schema.optional(Schema.Defect()),
48 },
49) {
50 override get message(): string {
51 return `Failed to create the workspace search index for '${this.cwd}'.`;
52 }
53}
54
55export class WorkspaceSearchIndexScanTimedOut extends Schema.TaggedError<WorkspaceSearchIndexScanTimedOut>()(
56 "WorkspaceSearchIndexScanTimedOut",
57 {
58 cwd: Schema.String,
59 timeout: Schema.String,
60 },
61) {
62 override get message(): string {
63 return `Workspace search index for '${this.cwd}' did not finish scanning within ${this.timeout}`;
64 }
65}
66
67export class WorkspaceSearchIndexSearchFailed extends Schema.TaggedError<WorkspaceSearchIndexSearchFailed>()(
68 "WorkspaceSearchIndexSearchFailed",
69 {
70 cwd: Schema.String,
71 queryLength: Schema.Number,
72 pageSize: Schema.Number,
73 reason: Schema.String,
74 cause: Schema.optional(Schema.Defect()),
75 },
76) {
77 override get message(): string {
78 return `Workspace search failed for '${this.cwd}'.`;
79 }
80}
81
82export class WorkspaceSearchIndexRefreshFailed extends Schema.TaggedError<WorkspaceSearchIndexRefreshFailed>()(
83 "WorkspaceSearchIndexRefreshFailed",
84 {
85 cwd: Schema.String,
86 reason: Schema.String,
87 cause: Schema.optional(Schema.Defect()),
88 },
89) {
90 override get message(): string {
91 return `Failed to refresh the workspace search index for '${this.cwd}'.`;
92 }
93}
94
95export class WorkspaceSearchIndexDestroyFailed extends Schema.TaggedError<WorkspaceSearchIndexDestroyFailed>()(
96 "WorkspaceSearchIndexDestroyFailed",
97 {
98 cwd: Schema.String,
99 cause: Schema.Defect(),
100 },
101) {
102 override get message(): string {
103 return `Failed to destroy the workspace search index for '${this.cwd}'.`;
104 }
105}
106
107export class WorkspaceSearchIndex extends Context.Service<
108 WorkspaceSearchIndex,
109 {
110 readonly list: () => Effect.Effect<ProjectListEntriesResult, WorkspaceSearchIndexSearchFailed>;
111 readonly search: (
112 query: string,
113 limit: number,
114 kind?: ProjectEntryKind,
115 imageOnly?: boolean,
116 ) => Effect.Effect<ProjectSearchEntriesResult, WorkspaceSearchIndexSearchFailed>;
117 readonly searchContents: (
118 input: Omit<ProjectSearchContentsInput, "cwd">,
119 ) => Effect.Effect<ProjectSearchContentsResult, WorkspaceSearchIndexSearchFailed>;
120 readonly refresh: () => Effect.Effect<void, WorkspaceSearchIndexRefreshFailed>;
121 }
122>()("t3/workspace/WorkspaceSearchIndex") {}
123
124// Only the host separator becomes `/`: on POSIX `\` is a file name character.
125function toPosixPath(input: string, hostIsWindows: boolean): string {
126 return hostIsWindows ? input.replaceAll("\\", "/") : input;
127}
128
129function trimDirectorySeparator(input: string): string {
130 return input.endsWith("/") ? input.slice(0, -1) : input;
131}
132
133function parentPathOf(input: string): string | undefined {
134 const separatorIndex = input.lastIndexOf("/");
135 return separatorIndex === -1 ? undefined : input.slice(0, separatorIndex);
136}
137
138function toProjectEntry(item: MixedItem, hostIsWindows: boolean): ProjectEntry | null {
139 const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath, hostIsWindows));
140 if (!normalizedPath) {
141 return null;
142 }
143
144 return {
145 path: normalizedPath,
146 kind: item.type,
147 };
148}
149
150function toFileEntry(item: FileItem, hostIsWindows: boolean): ProjectEntry | null {
151 const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, hostIsWindows));
152 return normalizedPath ? { path: normalizedPath, kind: "file" } : null;
153}
154
155function toDirectoryEntry(item: DirItem, hostIsWindows: boolean): ProjectEntry | null {
156 const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath, hostIsWindows));
157 return normalizedPath ? { path: normalizedPath, kind: "directory" } : null;
158}
159
160type SearchPage<I> = { readonly items: ReadonlyArray<I>; readonly totalMatched: number };
161
162function mapFileSearchResult(
163 result: SearchPage<FileItem>,
164 limit: number,
165 hostIsWindows: boolean,
166 imageOnly = false,
167): ProjectSearchEntriesResult {
168 const entries = result.items.flatMap((item) => {
169 const entry = toFileEntry(item, hostIsWindows);
170 return entry && (!imageOnly || isProjectFaviconPath(entry.path)) ? [entry] : [];
171 });
172 return {
173 entries: entries.slice(0, limit),
174 truncated: entries.length > limit || result.totalMatched > result.items.length,
175 };
176}
177
178function mapDirectorySearchResult(
179 result: SearchPage<DirItem>,
180 limit: number,
181 hostIsWindows: boolean,
182): ProjectSearchEntriesResult {
183 const entries = result.items.flatMap((item) => {
184 const entry = toDirectoryEntry(item, hostIsWindows);
185 return entry ? [entry] : [];
186 });
187 const rootDirectoryCount = result.items.some((item) => item.relativePath.length === 0) ? 1 : 0;
188 return {
189 entries: entries.slice(0, limit),
190 truncated: result.totalMatched - rootDirectoryCount > limit,
191 };
192}
193
194function mapMixedSearchResult(
195 result: SearchPage<MixedItem>,
196 limit: number,
197 hostIsWindows: boolean,
198): { readonly entries: ProjectEntry[]; readonly truncated: boolean } {
199 const entries: ProjectEntry[] = [];
200 for (const item of result.items) {
201 const entry = toProjectEntry(item, hostIsWindows);
202 if (entry) {
203 entries.push(entry);
204 }
205 if (entries.length >= limit) {
206 break;
207 }
208 }
209
210 const rootDirectoryCount = result.items.some(
211 (item) => item.type === "directory" && item.item.relativePath.length === 0,
212 )
213 ? 1
214 : 0;
215 return {
216 entries,
217 truncated: result.totalMatched - rootDirectoryCount > limit,
218 };
219}
220
221const WORD_CHARACTER = /[\p{Letter}\p{Mark}\p{Number}_]/u;
222
223function codePointAt(line: string, index: number): string | undefined {
224 const codePoint = line.codePointAt(index);
225 return codePoint === undefined ? undefined : String.fromCodePoint(codePoint);
226}
227
228function codePointBefore(line: string, index: number): string | undefined {
229 if (index <= 0) return undefined;
230 const previousCodeUnit = line.charCodeAt(index - 1);
231 const previousIndex =
232 previousCodeUnit >= 0xdc00 && previousCodeUnit <= 0xdfff ? index - 2 : index - 1;
233 return codePointAt(line, previousIndex);
234}
235
236function buildContentSearchQuery(input: Omit<ProjectSearchContentsInput, "cwd">): {
237 readonly searchQuery: string;
238 readonly regexMode: boolean;
239} {
240 if (input.caseSensitive) {
241 return { searchQuery: input.query, regexMode: input.useRegex };
242 }
243 return input.useRegex
244 ? { searchQuery: `(?i)${input.query}`, regexMode: true }
245 : { searchQuery: input.query.toLowerCase(), regexMode: false };
246}
247
248function mapContentMatchRanges(
249 line: string,
250 byteRanges: ReadonlyArray<readonly [number, number]>,
251): Array<{ readonly start: number; readonly end: number }> {
252 const lineBytes = Buffer.from(line);
253 const toStringIndex = (byteOffset: number) => lineBytes.subarray(0, byteOffset).toString().length;
254 return byteRanges.map(([startByte, endByte]) => ({
255 start: toStringIndex(startByte),
256 end: toStringIndex(endByte),
257 }));
258}
259
260// Post-filter whole words: consuming boundary regexes swallow adjacent separators and widen ranges; \b misses punctuation edges.
261function isWholeWordRange(
262 line: string,
263 range: { readonly start: number; readonly end: number },
264): boolean {
265 if (range.end <= range.start) return false;
266 const isWord = (character: string | undefined) =>
267 character !== undefined && WORD_CHARACTER.test(character);
268 const leftIsBoundary =
269 range.start === 0 ||
270 !isWord(codePointBefore(line, range.start)) ||
271 !isWord(codePointAt(line, range.start));
272 const rightIsBoundary =
273 range.end >= line.length ||
274 !isWord(codePointAt(line, range.end)) ||
275 !isWord(codePointBefore(line, range.end));
276 return leftIsBoundary && rightIsBoundary;
277}
278
279function withDirectoryAncestors(entries: ReadonlyArray<ProjectEntry>): ProjectEntry[] {
280 const entryByPath = new Map(entries.map((entry) => [entry.path, entry]));
281 for (const entry of entries) {
282 let parentPath = parentPathOf(entry.path);
283 while (parentPath) {
284 if (!entryByPath.has(parentPath)) {
285 entryByPath.set(parentPath, { path: parentPath, kind: "directory" });
286 }
287 parentPath = parentPathOf(parentPath);
288 }
289 }
290 return [...entryByPath.values()];
291}
292
293// fff applies .gitignore files only; Git also ignores paths from .git/info/exclude and the global excludes file.
294const listGitIgnoredPaths = Effect.fn("WorkspaceSearchIndex.listGitIgnoredPaths")(function* (
295 vcsProcess: VcsProcess.VcsProcess["Service"],
296 cwd: string,
297 hostIsWindows: boolean,
298) {
299 const result = yield* vcsProcess
300 .run({
301 operation: "WorkspaceSearchIndex.listGitIgnoredPaths",
302 command: "git",
303 args: [
304 "-c",
305 "core.fsmonitor=false",
306 "ls-files",
307 "--others",
308 "--ignored",
309 "--exclude-standard",
310 "--directory",
311 "-z",
312 ],
313 cwd,
314 allowNonZeroExit: true,
315 timeoutMs: 10_000,
316 maxOutputBytes: 16 * 1024 * 1024,
317 })
318 .pipe(Effect.orElseSucceed(() => undefined));
319 if (!result || result.exitCode !== 0 || result.stdoutTruncated) return new Set<string>();
320 return new Set(
321 result.stdout
322 .split("\0")
323 .filter((entry) => entry.length > 0)
324 .map((entry) => trimDirectorySeparator(toPosixPath(entry, hostIsWindows))),
325 );
326});
327
328function mixedItemPath(item: MixedItem): string {
329 return item.item.relativePath;
330}
331
332function isIgnoredPath(
333 ignoredPaths: ReadonlySet<string>,
334 relativePath: string,
335 hostIsWindows: boolean,
336): boolean {
337 if (ignoredPaths.size === 0) return false;
338 let candidate: string | undefined = trimDirectorySeparator(
339 toPosixPath(relativePath, hostIsWindows),
340 );
341 while (candidate) {
342 if (ignoredPaths.has(candidate)) return true;
343 candidate = parentPathOf(candidate);
344 }
345 return false;
346}
347
348const createFinder = Effect.fn("WorkspaceSearchIndex.createFinder")(function* (
349 cwd: string,
350 variant: WorkspaceSearchIndexVariant,
351) {
352 const result = yield* Effect.try({
353 try: () =>
354 FileFinder.create({
355 basePath: cwd,
356 disableMmapCache: true,
357 // Content indexing costs CPU/memory; path-only consumers use the lightweight index.
358 disableContentIndexing: variant !== "content",
359 aiMode: false,
360 }),
361 catch: (cause) =>
362 new WorkspaceSearchIndexCreateFailed({
363 cwd,
364 reason: "FileFinder.create threw unexpectedly.",
365 cause,
366 }),
367 });
368 if (result.ok) return result.value;
369 return yield* new WorkspaceSearchIndexCreateFailed({
370 cwd,
371 reason: result.error,
372 });
373});
374
375const waitForIndexReady = Effect.fn("WorkspaceSearchIndex.waitForIndexReady")(function* <E>(
376 cwd: string,
377 finder: FileFinderType,
378 onFailure: (input: { readonly reason: string; readonly cause?: unknown }) => E,
379): Effect.fn.Return<void, E | WorkspaceSearchIndexScanTimedOut> {
380 const result = yield* Effect.tryPromise({
381 try: () => finder.waitForIndexReady(WORKSPACE_INDEX_SCAN_TIMEOUT_MS),
382 catch: (cause) =>
383 onFailure({
384 reason: "FileFinder.waitForIndexReady rejected unexpectedly.",
385 cause,
386 }),
387 });
388 if (!result.ok) {
389 return yield* Effect.fail(onFailure({ reason: result.error }));
390 }
391 if (!result.value) {
392 return yield* new WorkspaceSearchIndexScanTimedOut({
393 cwd,
394 timeout: WORKSPACE_INDEX_SCAN_TIMEOUT,
395 });
396 }
397});
398
399export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
400 cwd: string,
401 variant: WorkspaceSearchIndexVariant = "paths",
402) {
403 const hostIsWindows = yield* HostProcess.isWindows;
404 const finder = yield* Effect.acquireRelease(createFinder(cwd, variant), (finder) =>
405 Effect.try({
406 try: () => finder.destroy(),
407 catch: (cause) => new WorkspaceSearchIndexDestroyFailed({ cwd, cause }),
408 }).pipe(Effect.orDie),
409 );
410 let initialScanTimedOut = false;
411 const vcsProcess = yield* VcsProcess.VcsProcess;
412 let ignoredPaths = yield* listGitIgnoredPaths(vcsProcess, cwd, hostIsWindows);
413 yield* waitForIndexReady(
414 cwd,
415 finder,
416 ({ reason, cause }) =>
417 new WorkspaceSearchIndexCreateFailed({
418 cwd,
419 reason,
420 cause,
421 }),
422 ).pipe(
423 Effect.catchTags({
424 WorkspaceSearchIndexScanTimedOut: (error) =>
425 variant === "paths"
426 ? Effect.sync(() => {
427 initialScanTimedOut = true;
428 })
429 : Effect.fail(error),
430 }),
431 );
432
433 const hasIncompleteInitialScan = () => initialScanTimedOut && finder.isScanning();
434
435 const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
436 query: string,
437 pageSize: number,
438 operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch",
439 execute: () => Result<A>,
440 ): Effect.fn.Return<A, WorkspaceSearchIndexSearchFailed> {
441 const result = yield* Effect.try({
442 try: execute,
443 catch: (cause) =>
444 new WorkspaceSearchIndexSearchFailed({
445 cwd,
446 queryLength: query.length,
447 pageSize,
448 reason: `FileFinder.${operation} threw unexpectedly.`,
449 cause,
450 }),
451 });
452 if (!result.ok) {
453 return yield* new WorkspaceSearchIndexSearchFailed({
454 cwd,
455 queryLength: query.length,
456 pageSize,
457 reason: result.error,
458 });
459 }
460 return result.value;
461 });
462
463 // Grows the first page until hidden entries can no longer crowd visible ones out of it.
464 const searchVisible = Effect.fn("WorkspaceSearchIndex.searchVisible")(function* <I>(
465 query: string,
466 pageSize: number,
467 operation: "directorySearch" | "fileSearch" | "mixedSearch",
468 execute: (pageSize: number) => Result<SearchPage<I>>,
469 pathOf: (item: I) => string,
470 ): Effect.fn.Return<SearchPage<I>, WorkspaceSearchIndexSearchFailed> {
471 let size = pageSize;
472 while (true) {
473 const fetchSize = size;
474 const result = yield* runSearch(query, fetchSize, operation, () => execute(fetchSize));
475 if (ignoredPaths.size === 0) return result;
476 const items = result.items.filter(
477 (item) => !isIgnoredPath(ignoredPaths, pathOf(item), hostIsWindows),
478 );
479 if (
480 items.length >= pageSize ||
481 result.items.length >= result.totalMatched ||
482 fetchSize >= WORKSPACE_INDEX_PAGE_SIZE
483 ) {
484 return {
485 items,
486 totalMatched: result.totalMatched - (result.items.length - items.length),
487 };
488 }
489 size = Math.min(WORKSPACE_INDEX_PAGE_SIZE, fetchSize * 4);
490 }
491 });
492
493 const refresh: WorkspaceSearchIndex["Service"]["refresh"] = Effect.fn(
494 "WorkspaceSearchIndex.refresh",
495 )(function* () {
496 // scanFiles waits on fff's picker lock, which its watcher can hold for seconds.
497 const result = yield* Effect.tryPromise({
498 try: () => finder.scanFilesAsync(),
499 catch: (cause) =>
500 new WorkspaceSearchIndexRefreshFailed({
501 cwd,
502 reason: "FileFinder.scanFilesAsync rejected unexpectedly.",
503 cause,
504 }),
505 });
506 if (!result.ok) {
507 return yield* new WorkspaceSearchIndexRefreshFailed({
508 cwd,
509 reason: result.error,
510 });
511 }
512 // A rescan that outlasts the wait keeps running, and the finder answers
513 // from its previous index until it finishes, so the index stays usable.
514 yield* waitForIndexReady(
515 cwd,
516 finder,
517 ({ reason, cause }) =>
518 new WorkspaceSearchIndexRefreshFailed({
519 cwd,
520 reason,
521 cause,
522 }),
523 ).pipe(Effect.catchTags({ WorkspaceSearchIndexScanTimedOut: () => Effect.void }));
524 ignoredPaths = yield* listGitIgnoredPaths(vcsProcess, cwd, hostIsWindows);
525 });
526
527 const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
528 function* () {
529 const incompleteBeforeQuery = hasIncompleteInitialScan();
530 const result = yield* searchVisible(
531 "",
532 WORKSPACE_INDEX_PAGE_SIZE,
533 "mixedSearch",
534 (pageSize) => finder.mixedSearch("", { pageSize }),
535 mixedItemPath,
536 );
537 const mapped = mapMixedSearchResult(result, WORKSPACE_INDEX_MAX_ENTRIES, hostIsWindows);
538 const sortedEntries = withDirectoryAncestors(mapped.entries).toSorted((left, right) =>
539 left.path.localeCompare(right.path),
540 );
541 const entries = sortedEntries.slice(0, WORKSPACE_INDEX_MAX_ENTRIES);
542 return {
543 entries,
544 truncated:
545 incompleteBeforeQuery || mapped.truncated || entries.length < sortedEntries.length,
546 };
547 },
548 );
549
550 const searchNames = Effect.fn("WorkspaceSearchIndex.searchNames")(function* (
551 query: string,
552 limit: number,
553 kind: ProjectEntryKind | undefined,
554 imageOnly: boolean | undefined,
555 ) {
556 const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, limit + 1);
557 if (kind === "file" || imageOnly) {
558 const result = yield* searchVisible(
559 query,
560 pageSize,
561 "fileSearch",
562 (size) => finder.fileSearch(query, { pageSize: size }),
563 (item) => item.relativePath,
564 );
565 return mapFileSearchResult(result, limit, hostIsWindows, imageOnly);
566 }
567 if (kind === "directory") {
568 const result = yield* searchVisible(
569 query,
570 pageSize,
571 "directorySearch",
572 (size) => finder.directorySearch(query, { pageSize: size }),
573 (item) => item.relativePath,
574 );
575 return mapDirectorySearchResult(result, limit, hostIsWindows);
576 }
577 const result = yield* searchVisible(
578 query,
579 pageSize,
580 "mixedSearch",
581 (size) => finder.mixedSearch(query, { pageSize: size }),
582 mixedItemPath,
583 );
584 return mapMixedSearchResult(result, limit, hostIsWindows);
585 });
586
587 const search: WorkspaceSearchIndex["Service"]["search"] = Effect.fn(
588 "WorkspaceSearchIndex.search",
589 )(function* (rawQuery, limit, kind, imageOnly) {
590 // fff parses any word containing "[" or "{" as a glob and has no escape for them; fuzzy
591 // matching is subsequence-based, so dropping them still matches "[id]" and "a{b}" paths.
592 // "{" is dropped only when the query finds nothing as a glob, so "*.{ts,tsx}" keeps working.
593 const query = rawQuery.replaceAll("[", "");
594 const incompleteBeforeQuery = hasIncompleteInitialScan();
595 let mapped = yield* searchNames(query, limit, kind, imageOnly);
596 if (mapped.entries.length === 0 && query.includes("{")) {
597 mapped = yield* searchNames(query.replaceAll("{", ""), limit, kind, imageOnly);
598 }
599 return { ...mapped, truncated: incompleteBeforeQuery || mapped.truncated };
600 });
601
602 const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn(
603 "WorkspaceSearchIndex.searchContents",
604 )(function* (input) {
605 let { searchQuery, regexMode } = buildContentSearchQuery(input);
606 const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
607 // Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
608 const rawPageSize = input.wholeWord
609 ? Math.max(input.limit, CONTENT_SEARCH_MAX_MATCHES_PER_FILE)
610 : input.limit;
611 const matches: Array<ProjectSearchContentsResult["matches"][number]> = [];
612 let nextCursor: GrepCursor | null = null;
613 let regexFallbackError: string | undefined;
614 let retryLiterally = false;
615
616 do {
617 const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));
618 const result = yield* runSearch(input.query, input.limit, "grep", () =>
619 finder.grep(searchQuery, {
620 mode: regexMode ? "regex" : "plain",
621 smartCase: !input.caseSensitive && !regexMode,
622 maxMatchesPerFile: Math.min(CONTENT_SEARCH_MAX_MATCHES_PER_FILE, rawPageSize),
623 pageSize: rawPageSize,
624 cursor: nextCursor,
625 timeBudgetMs: remainingTimeBudgetMs,
626 }),
627 );
628 // fff searches an invalid pattern as a literal, which would keep the `(?i)` prefix.
629 retryLiterally = regexMode && !input.caseSensitive && result.regexFallbackError !== undefined;
630 if (retryLiterally) {
631 regexFallbackError = result.regexFallbackError;
632 ({ searchQuery, regexMode } = buildContentSearchQuery({ ...input, useRegex: false }));
633 continue;
634 }
635
636 for (const match of result.items) {
637 if (isIgnoredPath(ignoredPaths, match.relativePath, hostIsWindows)) continue;
638 const matchRanges = mapContentMatchRanges(match.lineContent, match.matchRanges).filter(
639 (range) => !input.wholeWord || isWholeWordRange(match.lineContent, range),
640 );
641 // fff reports hits past its 512-char lineContent cap with no ranges, so whole words can't be verified there.
642 if (input.wholeWord && matchRanges.length === 0) continue;
643 matches.push({
644 path: toPosixPath(match.relativePath, hostIsWindows),
645 lineNumber: match.lineNumber,
646 lineContent: match.lineContent,
647 matchRanges,
648 });
649 }
650 nextCursor = result.nextCursor;
651 regexFallbackError ??= result.regexFallbackError;
652 } while (
653 retryLiterally ||
654 (matches.length < input.limit && nextCursor !== null && performance.now() < deadline)
655 );
656
657 return {
658 matches: matches.slice(0, input.limit),
659 truncated: matches.length > input.limit || nextCursor !== null,
660 ...(regexFallbackError !== undefined ? { regexFallbackError } : {}),
661 };
662 });
663
664 return WorkspaceSearchIndex.of({ list, refresh, search, searchContents });
665});
666
667export const WORKSPACE_SEARCH_INDEX_VARIANTS = ["paths", "content"] as const;
668export type WorkspaceSearchIndexVariant = (typeof WORKSPACE_SEARCH_INDEX_VARIANTS)[number];
669
670export const workspaceSearchIndexKey = (cwd: string, variant: WorkspaceSearchIndexVariant) =>
671 `${variant}\n${cwd}`;
672
673function parseWorkspaceSearchIndexKey(key: string): {
674 readonly cwd: string;
675 readonly variant: WorkspaceSearchIndexVariant;
676} {
677 const separatorIndex = key.indexOf("\n");
678 return {
679 variant: key.slice(0, separatorIndex) as WorkspaceSearchIndexVariant,
680 cwd: key.slice(separatorIndex + 1),
681 };
682}
683
684/** @public */
685export const layer = (key: string) => {
686 const { cwd, variant } = parseWorkspaceSearchIndexKey(key);
687 return Layer.effect(WorkspaceSearchIndex, make(cwd, variant));
688};
689
690export class WorkspaceSearchIndexMap extends LayerMap.Service<WorkspaceSearchIndexMap>()(
691 "t3/workspace/WorkspaceSearchIndexMap",
692 {
693 lookup: layer,
694 idleTimeToLive: WORKSPACE_INDEX_IDLE_TTL,
695 },
696) {}