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

filePathDisplay.ts

101 lines3.6 KB

1import { splitPathAndPosition } from "./terminal-links";
2​
3function normalizePathSeparators(path: string): string {
4 return path.replaceAll("\\", "/");
5}
6​
7function canonicalizeWindowsDrivePath(path: string): string {
8 return /^\/[A-Za-z]:\//.test(path) ? path.slice(1) : path;
9}
10​
11function trimTrailingPathSeparators(path: string): string {
12 return path.replace(/[\\/]+$/, "");
14​
15function basenameOfPath(path: string): string {
16 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
17 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
19​
20function stripRelativePrefixes(path: string): string {
21 return path.replace(/^\.\/+/, "").replace(/^\/+/, "");
23​
24export function formatWorkspaceRelativePath(
25 pathWithPosition: string,
26 workspaceRoot: string | undefined,
27): string {
28 const { path, line, column } = splitPathAndPosition(pathWithPosition);
29 const normalizedPath = canonicalizeWindowsDrivePath(normalizePathSeparators(path));
30​
31 let displayPath = normalizedPath;
32 if (workspaceRoot) {
33 const normalizedWorkspaceRoot = canonicalizeWindowsDrivePath(
34 normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
35 );
36 const workspaceLabel = basenameOfPath(normalizedWorkspaceRoot);
37 const pathForCompare = normalizedPath.toLowerCase();
38 const workspaceForCompare = normalizedWorkspaceRoot.toLowerCase();
39 const workspaceWithSeparator = `${workspaceForCompare}/`;
40 const workspaceLabelWithSeparator = `${workspaceLabel.toLowerCase()}/`;
41​
42 if (pathForCompare === workspaceForCompare) {
43 displayPath = workspaceLabel;
44 } else if (pathForCompare.startsWith(workspaceWithSeparator)) {
45 const relativeSuffix = normalizedPath.slice(normalizedWorkspaceRoot.length + 1);
46 displayPath = `${workspaceLabel}/${relativeSuffix}`;
47 } else if (!normalizedPath.startsWith("/")) {
48 const relativePath = stripRelativePrefixes(normalizedPath);
49 displayPath = pathForCompare.startsWith(workspaceLabelWithSeparator)
50 ? normalizedPath
51 : `${workspaceLabel}/${relativePath}`;
52 }
53 }
54​
55 if (!line) return displayPath;
56 return `${displayPath}:${line}${column ? `:${column}` : ""}`;
58​
59export function buildWorkspaceBreadcrumbSegments(
60 pathWithPosition: string,
61 workspaceRoot: string | undefined,
62): string[] {
63 const displayPath = formatWorkspaceRelativePath(pathWithPosition, workspaceRoot);
64 const { path } = splitPathAndPosition(displayPath);
65 return normalizePathSeparators(path)
66 .split("/")
67 .filter((segment) => segment.length > 0);
69​
70export function resolveWorkspaceSelectionPath(
71 pathWithPosition: string,
72 workspaceRoot: string | undefined,
73): string | null {
74 const { path } = splitPathAndPosition(pathWithPosition);
75 const normalizedPath = canonicalizeWindowsDrivePath(normalizePathSeparators(path));
76​
77 if (!workspaceRoot) {
78 return normalizedPath.startsWith("/") ? null : stripRelativePrefixes(normalizedPath);
79 }
80​
81 const normalizedWorkspaceRoot = canonicalizeWindowsDrivePath(
82 normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
83 );
84 const workspaceLabel = basenameOfPath(normalizedWorkspaceRoot);
85 const workspaceRootWithSlash = `${normalizedWorkspaceRoot}/`;
86 const workspaceLabelWithSlash = `${workspaceLabel}/`;
87​
88 if (normalizedPath === normalizedWorkspaceRoot) {
89 return "";
90 }
91 if (normalizedPath.startsWith(workspaceRootWithSlash)) {
92 return normalizedPath.slice(workspaceRootWithSlash.length);
93 }
94 if (normalizedPath.startsWith(workspaceLabelWithSlash)) {
95 return normalizedPath.slice(workspaceLabelWithSlash.length);
96 }
97 if (!normalizedPath.startsWith("/")) {
98 return stripRelativePrefixes(normalizedPath);
99 }
100 return null;