Open History to see the last commit
1export const WORKSPACE_BASENAME_LOOKUP_LIMIT = 25;
2
3// One counter for every caller: they all open the same panel, so the newest
4// click wins regardless of which one started the lookup.
5let latestLookupSequence = 0;
6
7export function claimWorkspaceBasenameLookup(): () => boolean {
8 latestLookupSequence += 1;
9 const claimed = latestLookupSequence;
10 return () => claimed === latestLookupSequence;
11}
12
13export interface WorkspaceEntryCandidate {
14 readonly path: string;
15 readonly kind: "file" | "directory";
16}
17
18function basenameOfPath(path: string): string {
19 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
20 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
21}
22
23export function needsWorkspaceBasenameLookup(relativePath: string): boolean {
24 const trimmed = relativePath.trim();
25 return (
26 trimmed !== "." &&
27 trimmed !== ".." &&
28 trimmed.length > 0 &&
29 !trimmed.includes("/") &&
30 !trimmed.includes("\\")
31 );
32}
33
34export function pickWorkspaceBasenameMatch(
35 basename: string,
36 entries: ReadonlyArray<WorkspaceEntryCandidate>,
37): string | null {
38 const target = basename.trim();
39 if (!target) return null;
40 const files = entries.filter((entry) => entry.kind === "file");
41 const exact = files.find((entry) => basenameOfPath(entry.path) === target);
42 if (exact) return exact.path;
43 const folded = target.toLowerCase();
44 const foldedMatches = files.filter(
45 (entry) => basenameOfPath(entry.path).toLowerCase() === folded,
46 );
47 return foldedMatches.length === 1 ? (foldedMatches[0]?.path ?? null) : null;
48}