src/vs/workbench/services/path/common/pathService.ts

215 LOC · 111 covered · 104 uncovered · 10 ranges · 735 concepts · 1 introducers · 446 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- pathService.ts ×10
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { isValidBasename } from '../../../../base/common/extpath.js';
7 > import { Schemas } from '../../../../base/common/network.js';
8 > import { IPath, win32, posix } from '../../../../base/common/path.js';
9 > import { OperatingSystem, OS } from '../../../../base/common/platform.js';
10 > import { basename } from '../../../../base/common/resources.js';
11 > import { URI } from '../../../../base/common/uri.js';
12 > import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
13 > import { getVirtualWorkspaceScheme } from '../../../../platform/workspace/common/virtualWorkspace.js';
14 > import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
15 > import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
16 > import { IRemoteAgentService } from '../../remote/common/remoteAgentService.js';
17 >
18 > export const IPathService = createDecorator<IPathService>('pathService');
19 >
20 > /**
21 > * Provides access to path related properties that will match the
22 > * environment. If the environment is connected to a remote, the
23 > * path properties will match that of the remotes operating system.
24 > */
25 > export interface IPathService {
26 >
27 > readonly _serviceBrand: undefined;
28 >
29 > /**
30 > * The correct path library to use for the target environment. If
31 > * the environment is connected to a remote, this will be the
32 > * path library of the remote file system. Otherwise it will be
33 > * the local file system's path library depending on the OS.
34 > */
35 > readonly path: Promise<IPath>;
36 >
37 > /**
38 > * Determines the best default URI scheme for the current workspace.
39 > * It uses information about whether we're running remote, in browser,
40 > * or native combined with information about the current workspace to
41 > * find the best default scheme.
42 > */
43 > readonly defaultUriScheme: string;
44 >
45 > /**
46 > * Converts the given path to a file URI to use for the target
47 > * environment. If the environment is connected to a remote, it
48 > * will use the path separators according to the remote file
49 > * system. Otherwise it will use the local file system's path
50 > * separators.
51 > */
52 > fileURI(path: string): Promise<URI>;
53 >
54 > /**
55 > * Resolves the user-home directory for the target environment.
56 > * If the envrionment is connected to a remote, this will be the
57 > * remote's user home directory, otherwise the local one unless
58 > * `preferLocal` is set to `true`.
59 > */
60 > userHome(options: { preferLocal: true }): URI;
61 > userHome(options?: { preferLocal: boolean }): Promise<URI>;
62 >
63 > /**
64 > * Figures out if the provided resource has a valid file name
65 > * for the operating system the file is saved to.
66 > *
67 > * Note: this currently only supports `file` and `vscode-file`
68 > * protocols where we know the limits of the file systems behind
69 > * these OS. Other remotes are not supported and this method
70 > * will always return `true` for them.
71 > */
72 > hasValidBasename(resource: URI, basename?: string): Promise<boolean>;
73 > hasValidBasename(resource: URI, os: OperatingSystem, basename?: string): boolean;
74 >
75 > /**
76 > * @deprecated use `userHome` instead.
77 > */
78 > readonly resolvedUserHome: URI | undefined;
79 > }
80 >
81 > export abstract class AbstractPathService implements IPathService {
82 >
83 > declare readonly _serviceBrand: undefined;
84 >
85 > private resolveOS: Promise<OperatingSystem>;
86 >
87 > private resolveUserHome: Promise<URI>;
88 > private maybeUnresolvedUserHome: URI | undefined;
89 >
90 > constructor(
91 private localUserHome: URI,
92 @IRemoteAgentService private readonly remoteAgentService: IRemoteAgentService,
93 @IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
94 @IWorkspaceContextService private contextService: IWorkspaceContextService
95 ) {
96
97 // OS
98 this.resolveOS = (async () => {
99 const env = await this.remoteAgentService.getEnvironment();
100
101 return env?.os || OS;
102 })();
103
104 // User Home
105 this.resolveUserHome = (async () => {
106 const env = await this.remoteAgentService.getEnvironment();
107 const userHome = this.maybeUnresolvedUserHome = env?.userHome ?? localUserHome;
108
109 return userHome;
110 })();
111 }
113 > hasValidBasename(resource: URI, basename?: string): Promise<boolean>;
114 > hasValidBasename(resource: URI, os: OperatingSystem, basename?: string): boolean;
115 > hasValidBasename(resource: URI, arg2?: string | OperatingSystem, basename?: string): boolean | Promise<boolean> {
116
117 // async version
118 if (typeof arg2 === 'string' || typeof arg2 === 'undefined') {
119 return this.resolveOS.then(os => this.doHasValidBasename(resource, os, arg2));
120 }
121
122 // sync version
123 return this.doHasValidBasename(resource, arg2, basename);
124 }
126 > private doHasValidBasename(resource: URI, os: OperatingSystem, name?: string): boolean {
127
128 // Our `isValidBasename` method only works with our
129 // standard schemes for files on disk, either locally
130 // or remote.
131 if (resource.scheme === Schemas.file || resource.scheme === Schemas.vscodeRemote) {
132 return isValidBasename(name ?? basename(resource), os === OperatingSystem.Windows);
133 }
134
135 return true;
136 }
138 > get defaultUriScheme(): string {
139 return AbstractPathService.findDefaultUriScheme(this.environmentService, this.contextService);
140 }
142 > static findDefaultUriScheme(environmentService: IWorkbenchEnvironmentService, contextService: IWorkspaceContextService): string {
143 if (environmentService.remoteAuthority) {
144 return Schemas.vscodeRemote;
145 }
146
147 const virtualWorkspace = getVirtualWorkspaceScheme(contextService.getWorkspace());
148 if (virtualWorkspace) {
149 return virtualWorkspace;
150 }
151
152 const firstFolder = contextService.getWorkspace().folders[0];
153 if (firstFolder) {
154 return firstFolder.uri.scheme;
155 }
156
157 const configuration = contextService.getWorkspace().configuration;
158 if (configuration) {
159 return configuration.scheme;
160 }
161
162 return Schemas.file;
163 }
165 > userHome(options?: { preferLocal: boolean }): Promise<URI>;
166 > userHome(options: { preferLocal: true }): URI;
167 > userHome(options?: { preferLocal: boolean }): Promise<URI> | URI {
168 return options?.preferLocal ? this.localUserHome : this.resolveUserHome;
169 }
171 > get resolvedUserHome(): URI | undefined {
172 return this.maybeUnresolvedUserHome;
173 }
175 > get path(): Promise<IPath> {
176 return this.resolveOS.then(os => {
177 return os === OperatingSystem.Windows ?
178 win32 :
179 posix;
180 });
181 }
183 > async fileURI(_path: string): Promise<URI> {
184 let authority = '';
185
186 // normalize to fwd-slashes on windows,
187 // on other systems bwd-slashes are valid
188 // filename character, eg /f\oo/ba\r.txt
189 const os = await this.resolveOS;
190 if (os === OperatingSystem.Windows) {
191 _path = _path.replace(/\\/g, '/');
192 }
193
194 // check for authority as used in UNC shares
195 // or use the path as given
196 if (_path[0] === '/' && _path[1] === '/') {
197 const idx = _path.indexOf('/', 2);
198 if (idx === -1) {
199 authority = _path.substring(2);
200 _path = '/';
201 } else {
202 authority = _path.substring(2, idx);
203 _path = _path.substring(idx) || '/';
204 }
205 }
206
207 return URI.from({
208 scheme: Schemas.file,
209 authority,
210 path: _path,
211 query: '',
212 fragment: ''
213 });
214 }