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

workspaceBasenameLookup.ts

81 lines2.9 KB

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