Skip to content

Commit 80e2c7a

Browse files
committed
Introduce WorkspaceFolder model that wraps information about a workspace folder.
Use WorkspaceFolder instead of URIs in IWorkspaceContextService
1 parent 3339ad8 commit 80e2c7a

53 files changed

Lines changed: 551 additions & 240 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/vs/base/common/labels.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export interface ILabelProvider {
1818
}
1919

2020
export interface IWorkspaceFolderProvider {
21-
getWorkspaceFolder(resource: URI): URI;
21+
getWorkspaceFolder(resource: URI): { uri: URI };
2222
getWorkspace(): {
23-
folders: URI[];
23+
folders: { uri: URI }[];
2424
};
2525
}
2626

@@ -43,14 +43,14 @@ export function getPathLabel(resource: URI | string, rootProvider?: IWorkspaceFo
4343
const hasMultipleRoots = rootProvider.getWorkspace().folders.length > 1;
4444

4545
let pathLabel: string;
46-
if (isEqual(baseResource.fsPath, resource.fsPath, !platform.isLinux /* ignorecase */)) {
46+
if (isEqual(baseResource.uri.fsPath, resource.fsPath, !platform.isLinux /* ignorecase */)) {
4747
pathLabel = ''; // no label if pathes are identical
4848
} else {
49-
pathLabel = normalize(ltrim(resource.fsPath.substr(baseResource.fsPath.length), nativeSep), true);
49+
pathLabel = normalize(ltrim(resource.fsPath.substr(baseResource.uri.fsPath.length), nativeSep), true);
5050
}
5151

5252
if (hasMultipleRoots) {
53-
const rootName = basename(baseResource.fsPath);
53+
const rootName = basename(baseResource.uri.fsPath);
5454
pathLabel = pathLabel ? join(rootName, pathLabel) : rootName; // always show root basename if there are multiple
5555
}
5656

src/vs/editor/standalone/browser/simpleServices.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingReso
1818
import { IKeybindingEvent, KeybindingSource, IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
1919
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
2020
import { IConfirmation, IMessageService } from 'vs/platform/message/common/message';
21-
import { IWorkspaceContextService, IWorkspace, WorkbenchState } from 'vs/platform/workspace/common/workspace';
21+
import { IWorkspaceContextService, IWorkspace, WorkbenchState, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
2222
import * as editorCommon from 'vs/editor/common/editorCommon';
2323
import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
2424
import { Selection } from 'vs/editor/common/core/selection';
@@ -532,7 +532,7 @@ export class SimpleWorkspaceContextService implements IWorkspaceContextService {
532532

533533
constructor() {
534534
const resource = URI.from({ scheme: SimpleWorkspaceContextService.SCHEME, authority: 'model', path: '/' });
535-
this.workspace = { id: '4064f6ec-cb38-4ad0-af64-ee6467e63c82', folders: [resource], name: resource.fsPath };
535+
this.workspace = { id: '4064f6ec-cb38-4ad0-af64-ee6467e63c82', folders: [{ uri: resource, raw: resource.toString(), name: '', index: 0, }], name: resource.fsPath };
536536
}
537537

538538
public getWorkspace(): IWorkspace {
@@ -549,7 +549,7 @@ export class SimpleWorkspaceContextService implements IWorkspaceContextService {
549549
return WorkbenchState.EMPTY;
550550
}
551551

552-
public getWorkspaceFolder(resource: URI): URI {
552+
public getWorkspaceFolder(resource: URI): WorkspaceFolder {
553553
return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME ? this.workspace.folders[0] : void 0;
554554
}
555555

src/vs/platform/configuration/common/configuration.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ export class Configuration<T> {
208208

209209
private _globalConfiguration: ConfigurationModel<T>;
210210
private _workspaceConsolidatedConfiguration: ConfigurationModel<T>;
211-
private _legacyWorkspaceConsolidatedConfiguration: ConfigurationModel<T>;
212211
protected _foldersConsolidatedConfigurations: StrictResourceMap<ConfigurationModel<T>>;
213212

214213
constructor(protected _defaults: ConfigurationModel<T>, protected _user: ConfigurationModel<T>, protected _workspaceConfiguration: ConfigurationModel<T> = new ConfigurationModel<T>(), protected folders: StrictResourceMap<ConfigurationModel<T>> = new StrictResourceMap<ConfigurationModel<T>>(), protected _workspace?: Workspace) {
@@ -226,7 +225,6 @@ export class Configuration<T> {
226225
protected merge(): void {
227226
this._globalConfiguration = new ConfigurationModel<T>().merge(this._defaults).merge(this._user);
228227
this._workspaceConsolidatedConfiguration = new ConfigurationModel<T>().merge(this._globalConfiguration).merge(this._workspaceConfiguration);
229-
this._legacyWorkspaceConsolidatedConfiguration = null;
230228
this._foldersConsolidatedConfigurations = new StrictResourceMap<ConfigurationModel<T>>();
231229
for (const folder of this.folders.keys()) {
232230
this.mergeFolder(folder);
@@ -255,20 +253,6 @@ export class Configuration<T> {
255253
};
256254
}
257255

258-
lookupLegacy<C>(key: string): IConfigurationValue<C> {
259-
if (!this._legacyWorkspaceConsolidatedConfiguration) {
260-
this._legacyWorkspaceConsolidatedConfiguration = this._workspace ? new ConfigurationModel<any>().merge(this._workspaceConfiguration).merge(this.folders.get(this._workspace.folders[0])) : null;
261-
}
262-
const consolidateConfigurationModel = this.getConsolidateConfigurationModel({});
263-
return {
264-
default: objects.clone(getConfigurationValue<C>(this._defaults.contents, key)),
265-
user: objects.clone(getConfigurationValue<C>(this._user.contents, key)),
266-
workspace: objects.clone(this._legacyWorkspaceConsolidatedConfiguration ? getConfigurationValue<C>(this._legacyWorkspaceConsolidatedConfiguration.contents, key) : void 0),
267-
folder: void 0,
268-
value: objects.clone(getConfigurationValue<C>(consolidateConfigurationModel.contents, key))
269-
};
270-
}
271-
272256
keys(overrides: IConfigurationOverrides = {}): IConfigurationKeys {
273257
const folderConfigurationModel = this.getFolderConfigurationModelForResource(overrides.resource);
274258
return {
@@ -330,7 +314,7 @@ export class Configuration<T> {
330314
return this._workspaceConsolidatedConfiguration;
331315
}
332316

333-
return this._foldersConsolidatedConfigurations.get(root) || this._workspaceConsolidatedConfiguration;
317+
return this._foldersConsolidatedConfigurations.get(root.uri) || this._workspaceConsolidatedConfiguration;
334318
}
335319

336320
private getFolderConfigurationModelForResource(resource: URI): ConfigurationModel<any> {
@@ -339,7 +323,7 @@ export class Configuration<T> {
339323
}
340324

341325
const root = this._workspace.getFolder(resource);
342-
return root ? this.folders.get(root) : null;
326+
return root ? this.folders.get(root.uri) : null;
343327
}
344328

345329
public toData(): IConfigurationData<any> {

src/vs/platform/workspace/common/workspace.ts

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
'use strict';
66

77
import URI from 'vs/base/common/uri';
8+
import * as paths from 'vs/base/common/paths';
89
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
910
import { TrieMap } from 'vs/base/common/map';
1011
import Event from 'vs/base/common/event';
12+
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier, IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
13+
import { coalesce, distinct } from 'vs/base/common/arrays';
1114
import { isLinux } from 'vs/base/common/platform';
12-
import { distinct } from 'vs/base/common/arrays';
13-
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
1415

1516
export const IWorkspaceContextService = createDecorator<IWorkspaceContextService>('contextService');
1617

@@ -52,7 +53,7 @@ export interface IWorkspaceContextService {
5253
* Returns the folder for the given resource from the workspace.
5354
* Can be null if there is no workspace or the resource is not inside the workspace.
5455
*/
55-
getWorkspaceFolder(resource: URI): URI;
56+
getWorkspaceFolder(resource: URI): WorkspaceFolder;
5657

5758
/**
5859
* Return `true` if the current workspace has the given identifier otherwise `false`.
@@ -85,39 +86,59 @@ export interface IWorkspace {
8586
/**
8687
* Folders in the workspace.
8788
*/
88-
readonly folders: URI[];
89+
readonly folders: WorkspaceFolder[];
8990

9091
/**
9192
* the location of the workspace configuration
9293
*/
9394
readonly configuration?: URI;
9495
}
9596

97+
export interface WorkspaceFolder {
98+
99+
/**
100+
* The associated URI for this workspace folder.
101+
*/
102+
readonly uri: URI;
103+
104+
/**
105+
* The name of this workspace folder. Defaults to
106+
* the basename its [uri-path](#Uri.path)
107+
*/
108+
readonly name: string;
109+
110+
/**
111+
* The ordinal number of this workspace folder.
112+
*/
113+
readonly index: number;
114+
115+
/**
116+
* The raw path of this workspace folder
117+
*/
118+
readonly raw: string;
119+
}
120+
96121
export class Workspace implements IWorkspace {
97122

98-
private _foldersMap: TrieMap<URI> = new TrieMap<URI>();
99-
private _folders: URI[];
123+
private _foldersMap: TrieMap<WorkspaceFolder> = new TrieMap<WorkspaceFolder>();
124+
private _folders: WorkspaceFolder[];
100125

101126
constructor(
102127
public readonly id: string,
103128
private _name: string,
104-
folders: URI[],
129+
folders: WorkspaceFolder[],
105130
private _configuration: URI = null,
106131
public readonly ctime?: number
107132
) {
108133
this.folders = folders;
109134
}
110135

111-
private ensureUnique(folders: URI[]): URI[] {
112-
return distinct(folders, folder => isLinux ? folder.fsPath : folder.fsPath.toLowerCase());
113-
}
114-
115-
public get folders(): URI[] {
136+
public get folders(): WorkspaceFolder[] {
116137
return this._folders;
117138
}
118139

119-
public set folders(folders: URI[]) {
120-
this._folders = this.ensureUnique(folders);
140+
public set folders(folders: WorkspaceFolder[]) {
141+
this._folders = folders;
121142
this.updateFoldersMap();
122143
}
123144

@@ -137,7 +158,7 @@ export class Workspace implements IWorkspace {
137158
this._configuration = configuration;
138159
}
139160

140-
public getFolder(resource: URI): URI {
161+
public getFolder(resource: URI): WorkspaceFolder {
141162
if (!resource) {
142163
return null;
143164
}
@@ -146,13 +167,45 @@ export class Workspace implements IWorkspace {
146167
}
147168

148169
private updateFoldersMap(): void {
149-
this._foldersMap = new TrieMap<URI>();
170+
this._foldersMap = new TrieMap<WorkspaceFolder>();
150171
for (const folder of this.folders) {
151-
this._foldersMap.insert(folder.fsPath, folder);
172+
this._foldersMap.insert(folder.uri.fsPath, folder);
152173
}
153174
}
154175

155176
public toJSON(): IWorkspace {
156177
return { id: this.id, folders: this.folders, name: this.name };
157178
}
158179
}
180+
181+
export function toWorkspaceFolders(configuredFolders: IStoredWorkspaceFolder[], relativeTo?: URI): WorkspaceFolder[] {
182+
let workspaceFolders = parseWorkspaceFolders(configuredFolders, relativeTo);
183+
return ensureUnique(coalesce(workspaceFolders))
184+
.map(({ uri, raw, name }, index) => ({ uri, raw, name: name || paths.basename(uri.fsPath), index }));
185+
}
186+
187+
function parseWorkspaceFolders(configuredFolders: IStoredWorkspaceFolder[], relativeTo: URI): WorkspaceFolder[] {
188+
return configuredFolders.map(({ path, name }, index) => {
189+
const uri = toUri(path, relativeTo);
190+
if (!uri) {
191+
return void 0;
192+
}
193+
return { uri, raw: path, index, name };
194+
});
195+
}
196+
197+
function toUri(path: string, relativeTo: URI): URI {
198+
if (path) {
199+
if (paths.isAbsolute(path)) {
200+
return URI.file(path);
201+
}
202+
if (relativeTo) {
203+
return URI.file(paths.join(relativeTo.fsPath, path));
204+
}
205+
}
206+
return null;
207+
}
208+
209+
function ensureUnique(folders: WorkspaceFolder[]): WorkspaceFolder[] {
210+
return distinct(folders, folder => isLinux ? folder.uri.fsPath : folder.uri.fsPath.toLowerCase());
211+
}

src/vs/platform/workspace/test/common/testWorkspace.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import URI from 'vs/base/common/uri';
7-
import { Workspace } from 'vs/platform/workspace/common/workspace';
7+
import { Workspace, toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
8+
import { isWindows } from 'vs/base/common/platform';
89

9-
const wsUri = URI.file('C:\\testWorkspace');
10+
const wsUri = URI.file(isWindows ? 'C:\\testWorkspace' : '/testWorkspace');
1011
export const TestWorkspace = testWorkspace(wsUri);
1112

1213
export function testWorkspace(resource: URI): Workspace {
1314
return new Workspace(
1415
resource.toString(),
1516
resource.fsPath,
16-
[resource]
17+
toWorkspaceFolders([{ path: resource.fsPath }])
1718
);
1819
}

0 commit comments

Comments
 (0)