Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectFilePicker.logic.ts

97 lines3.1 KB

1import type { ProjectEntry } from "@t3tools/contracts";
2import { normalizeSearchQuery } from "@t3tools/shared/searchRanking";
3​
4export const PROJECT_FILE_PICKER_RESULT_LIMIT = 200;
5​
6export interface ProjectFilePickerMatch {
7 readonly name: string;
8 readonly nameMatchIndices: ReadonlyArray<number>;
9 readonly path: string;
10 readonly pathMatchIndices: ReadonlyArray<number>;
12​
13function fileName(path: string): string {
14 return path.slice(path.lastIndexOf("/") + 1);
16​
17/** Return null for unmatched subsequences; server-matched entries still render without highlights. */
18function findMatchIndices(value: string, queryChars: ReadonlyArray<string>): number[] | null {
19 if (queryChars.length === 0) return [];
20​
21 // Whole-string lowercasing preserves context-sensitive rules like Greek final sigma (Σ → ς);
22 // lowercasing lengthens İ (to i̇) and shortens nothing, so only a length change needs mapping back.
23 const normalizedValue = value.toLowerCase();
24 const sourceSpans: Array<readonly [number, number]> = [];
25 if (normalizedValue.length !== value.length) {
26 let sourceOffset = 0;
27 for (const char of value) {
28 const span = [sourceOffset, sourceOffset + char.length] as const;
29 for (let n = char.toLowerCase().length; n > 0; n -= 1) sourceSpans.push(span);
30 sourceOffset = span[1];
31 }
32 }
33​
34 const indices: number[] = [];
35 let queryIndex = 0;
36 let offset = 0;
37​
38 for (const char of normalizedValue) {
39 if (char === queryChars[queryIndex]) {
40 const [start, end] = sourceSpans[offset] ?? [offset, offset + char.length];
41 for (let i = Math.max(start, (indices.at(-1) ?? -1) + 1); i < end; i += 1) indices.push(i);
42 queryIndex += 1;
43 if (queryIndex === queryChars.length) return indices;
44 }
45 offset += char.length;
46 }
47​
48 return null;
50​
51export function groupMatchRanges(
52 indices: ReadonlyArray<number>,
53): Array<readonly [start: number, end: number]> {
54 const ranges: Array<[number, number]> = [];
55 for (const index of indices) {
56 const previous = ranges.at(-1);
57 if (previous && previous[1] === index) {
58 previous[1] = index + 1;
59 } else {
60 ranges.push([index, index + 1]);
61 }
62 }
63 return ranges;
65​
66/** Preserve server ranking and query normalization while filtering files and computing highlights. */
67export function getProjectFilePickerMatches(
68 entries: ReadonlyArray<ProjectEntry>,
69 rawQuery: string,
70 limit = PROJECT_FILE_PICKER_RESULT_LIMIT,
71): ProjectFilePickerMatch[] {
72 if (limit <= 0) return [];
73​
74 const queryChars = Array.from(
75 normalizeSearchQuery(rawQuery, {
76 trimLeadingPattern: /^[@./]+/,
77 }).replaceAll(/\s/g, ""),
78 );
79 const matches: ProjectFilePickerMatch[] = [];
80​
81 for (const entry of entries) {
82 if (entry.kind !== "file") continue;
83​
84 const name = fileName(entry.path);
85 const nameMatchIndices = findMatchIndices(name, queryChars);
86 const pathMatchIndices = findMatchIndices(entry.path, queryChars);
87 matches.push({
88 name,
89 nameMatchIndices: nameMatchIndices ?? [],
90 path: entry.path,
91 pathMatchIndices: pathMatchIndices ?? [],
92 });
93 if (matches.length >= limit) break;
94 }
95​
96 return matches;