51
52
export function getPathLabel(resource: URI, formatting: IPathLabelFormatting): string {
53
>
const { os, tildify: tildifier, relative: relatifier } = formatting;
labels.ts
54
>
55
>
// return early with a relative path if we can resolve one
56
>
if (relatifier) {
57
>
const relativePath = getRelativePathLabel(resource, relatifier, os);
58
>
if (typeof relativePath === 'string') {
59
>
return relativePath;
60
>
}
61
>
}
62
>
63
>
// otherwise try to resolve a absolute path label and
64
>
// apply target OS standard path separators if target
65
>
// OS differs from actual OS we are running in
66
>
let absolutePath = resource.fsPath;
67
>
if (os === OperatingSystem.Windows && !isWindows) {
68
>
absolutePath = absolutePath.replace(/\//g, '\\');
69
>
} else if (os !== OperatingSystem.Windows && isWindows) {
70
absolutePath = absolutePath.replace(/\\/g, '/');
71
}
73
>
// macOS/Linux: tildify with provided user home directory
74
>
if (os !== OperatingSystem.Windows && tildifier?.userHome) {
75
>
const userHome = tildifier.userHome.fsPath;
76
>
77
>
// This is a bit of a hack, but in order to figure out if the
78
>
// resource is in the user home, we need to make sure to convert it
79
>
// to a user home resource. We cannot assume that the resource is
80
>
// already a user home resource.
81
>
let userHomeCandidate: string;
82
>
if (resource.scheme !== tildifier.userHome.scheme && resource.path[0] === posix.sep && resource.path[1] !== posix.sep) {
83
>
userHomeCandidate = tildifier.userHome.with({ path: resource.path }).fsPath;
84
>
} else {
85
>
userHomeCandidate = absolutePath;
86
>
}
87
>
88
>
absolutePath = tildify(userHomeCandidate, userHome, os);
89
>
}
90
>
91
>
// normalize
92
>
const pathLib = os === OperatingSystem.Windows ? win32 : posix;
93
>
return pathLib.normalize(normalizeDriveLetter(absolutePath, os === OperatingSystem.Windows));
94
>
}
95
96
>
function getRelativePathLabel(resource: URI, relativePathProvider: IRelativePathProvider, os: OperatingSystem): string | undefined {
labels.ts
97
>
const pathLib = os === OperatingSystem.Windows ? win32 : posix;
98
>
const extUriLib = os === OperatingSystem.Linux ? extUri : extUriIgnorePathCase;
99
>
100
>
const workspace = relativePathProvider.getWorkspace();
101
>
const firstFolder = workspace.folders.at(0);
102
>
if (!firstFolder) {
103
return undefined;
104
}
106
>
// This is a bit of a hack, but in order to figure out the folder
107
>
// the resource belongs to, we need to make sure to convert it
108
>
// to a workspace resource. We cannot assume that the resource is
109
>
// already matching the workspace.
110
>
if (resource.scheme !== firstFolder.uri.scheme && resource.path[0] === posix.sep && resource.path[1] !== posix.sep) {
111
>
resource = firstFolder.uri.with({ path: resource.path });
112
>
}
113
>
114
>
const folder = relativePathProvider.getWorkspaceFolder(resource);
115
>
if (!folder) {
116
return undefined;
117
}
119
>
let relativePathLabel: string | undefined = undefined;
120
>
if (extUriLib.isEqual(folder.uri, resource)) {
121
relativePathLabel = ''; // no label if paths are identical
123
>
relativePathLabel = extUriLib.relativePath(folder.uri, resource) ?? '';
124
>
}
125
>
126
>
// normalize
127
>
if (relativePathLabel) {
128
>
relativePathLabel = pathLib.normalize(relativePathLabel);
129
>
}
130
>
131
>
// always show root basename if there are multiple folders
132
>
if (workspace.folders.length > 1 && !relativePathProvider.noPrefix) {
133
const rootName = folder.name ? folder.name : extUriLib.basenameOrAuthority(folder.uri);
134
relativePathLabel = relativePathLabel ? `${rootName} • ${relativePathLabel}` : rootName;
135
}
137
>
return relativePathLabel;
138
>
}
139
140
export function normalizeDriveLetter(path: string, isWindowsOS: boolean = isWindows): string {
141
if (hasDriveLetter(path, isWindowsOS)) {
142
>
return path.charAt(0).toUpperCase() + path.slice(1);
labels.ts
143
>
}
144
145
return path;