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

workspaceBasenameLookup.ts

80 lines2.9 KB

1import { isAbsolutePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
2​
3export const WORKSPACE_BASENAME_LOOKUP_LIMIT = 25;
4​
5// All callers open one panel; the newest click must win across callers.
6let latestLookupSequence = 0;
7​
8export function claimWorkspaceBasenameLookup(): () => boolean {
9 latestLookupSequence += 1;
10 const claimed = latestLookupSequence;
11 return () => claimed === latestLookupSequence;
13​
14export interface WorkspaceEntryCandidate {
15 readonly path: string;
16 readonly kind: "file" | "directory";
18​
19function basenameOfPath(path: string): string {
20 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
21 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
23​
24export function needsWorkspaceBasenameLookup(relativePath: string): boolean {
25 const trimmed = relativePath.trim();
26 return (
27 trimmed !== "." &&
28 trimmed !== ".." &&
29 trimmed.length > 0 &&
30 !trimmed.includes("/") &&
31 !trimmed.includes("\\")
32 );
34​
35export function pickWorkspaceBasenameMatch(
36 basename: string,
37 entries: ReadonlyArray<WorkspaceEntryCandidate>,
38): string | null {
39 const target = basename.trim();
40 if (!target) return null;
41 const files = entries.filter((entry) => entry.kind === "file");
42 const exact = files.find((entry) => basenameOfPath(entry.path) === target);
43 if (exact) return exact.path;
44 const folded = target.toLowerCase();
45 const foldedMatches = files.filter(
46 (entry) => basenameOfPath(entry.path).toLowerCase() === folded,
47 );
48 return foldedMatches.length === 1 ? (foldedMatches[0]?.path ?? null) : null;
50​
51// Repository-relative paths can repeat the project's suffix in subfolder projects.
52export function stripRepeatedWorkspacePrefix(relativePath: string, cwd: string): string | null {
53 if (isAbsolutePath(relativePath)) return null;
54 const pathSegments = relativePath.split(/[\\/]+/);
55 const cwdSegments = cwd.split(/[\\/]+/).filter(Boolean);
56 const fold = isWindowsAbsolutePath(cwd)
57 ? (segment: string | undefined) => segment?.toLowerCase()
58 : (segment: string | undefined) => segment;
59 for (let count = Math.min(cwdSegments.length, pathSegments.length - 1); count > 0; count--) {
60 const prefix = cwdSegments.slice(-count);
61 if (prefix.every((segment, index) => fold(segment) === fold(pathSegments[index]))) {
62 return pathSegments.slice(count).join("/") || null;
63 }
64 }
65 return null;
67​
68export function pickStrippedWorkspaceMatch(
69 strippedPath: string,
70 cwd: string,
71 entries: ReadonlyArray<WorkspaceEntryCandidate>,
72): string | null {
73 const caseInsensitive = isWindowsAbsolutePath(cwd);
74 const key = (path: string) => {
75 const normalized = path.replaceAll("\\", "/");
76 return caseInsensitive ? normalized.toLowerCase() : normalized;
77 };
78 const files = entries.filter((entry) => entry.kind === "file");
79 return files.find((entry) => key(entry.path) === key(strippedPath))?.path ?? null;