MCPcopy Create free account
hub / github.com/pingdotgg/t3code / formatWorkspaceRelativePath

Function formatWorkspaceRelativePath

apps/web/src/filePathDisplay.ts:24–57  ·  view source on GitHub ↗
(
  pathWithPosition: string,
  workspaceRoot: string | undefined,
)

Source from the content-addressed store, hash-verified

22}
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}` : ""}`;
57}

Callers 5

workEntryPreviewFunction · 0.90

Calls 6

splitPathAndPositionFunction · 0.90
stripRelativePrefixesFunction · 0.85
normalizePathSeparatorsFunction · 0.70
basenameOfPathFunction · 0.70

Tested by

no test coverage detected