From a7b743e02dac4e75df4a4938ca6e74d496d034a0 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 24 Oct 2017 14:32:24 +0200 Subject: [PATCH] make workspace service fit to add a workspace folder with a name (for #35407) --- .../standalone/browser/simpleServices.ts | 4 +- src/vs/platform/workspace/common/workspace.ts | 3 +- .../node/configurationService.ts | 41 ++++++++++++++----- .../test/node/configurationService.test.ts | 16 ++++++++ .../workspace/common/workspaceEditing.ts | 1 + .../workspace/node/workspaceEditingService.ts | 4 +- .../workbench/test/workbenchTestServices.ts | 4 +- 7 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/vs/editor/standalone/browser/simpleServices.ts b/src/vs/editor/standalone/browser/simpleServices.ts index e1d06c1220c31..606aff0559cdf 100644 --- a/src/vs/editor/standalone/browser/simpleServices.ts +++ b/src/vs/editor/standalone/browser/simpleServices.ts @@ -599,7 +599,9 @@ export class SimpleWorkspaceContextService implements IWorkspaceContextService { return true; } - public addFolders(foldersToAdd: URI[]): TPromise { + public addFolders(foldersToAdd: URI[]): TPromise; + public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise; + public addFolders(foldersToAdd: any[]): TPromise { return TPromise.as(void 0); } diff --git a/src/vs/platform/workspace/common/workspace.ts b/src/vs/platform/workspace/common/workspace.ts index ff061b4d369fd..16536b259fdc8 100644 --- a/src/vs/platform/workspace/common/workspace.ts +++ b/src/vs/platform/workspace/common/workspace.ts @@ -48,7 +48,7 @@ export interface IWorkspaceContextService { onDidChangeWorkspaceFolders: Event; /** - * Provides access to the workspace object the platform is running with. + * Provides access to the workspace object the platform is running with. */ getWorkspace(): IWorkspace; @@ -56,6 +56,7 @@ export interface IWorkspaceContextService { * add folders to the existing workspace */ addFolders(folders: URI[]): TPromise; + addFolders(folders: { uri: URI, name?: string }[]): TPromise; /** * remove folders from the existing workspace diff --git a/src/vs/workbench/services/configuration/node/configurationService.ts b/src/vs/workbench/services/configuration/node/configurationService.ts index d2a28dd40b271..5418ca53f42c8 100644 --- a/src/vs/workbench/services/configuration/node/configurationService.ts +++ b/src/vs/workbench/services/configuration/node/configurationService.ts @@ -106,9 +106,11 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat return this.workspace.getFolder(resource); } - public addFolders(foldersToAdd: URI[]): TPromise { + public addFolders(foldersToAdd: URI[]): TPromise; + public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise; + public addFolders(folders: URI[] | { uri: URI, name?: string }[]): TPromise { assert.ok(this.jsonEditingService, 'Workbench is not initialized yet'); - return this.workspaceEditingQueue.queue(() => this.doAddFolders(foldersToAdd)); + return this.workspaceEditingQueue.queue(() => this.doAddFolders(folders)); } public removeFolders(foldersToRemove: URI[]): TPromise { @@ -130,7 +132,7 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat return false; } - private doAddFolders(foldersToAdd: URI[]): TPromise { + private doAddFolders(foldersToAdd: (URI | { uri: URI, name?: string })[]): TPromise { if (this.getWorkbenchState() !== WorkbenchState.WORKSPACE) { return TPromise.as(void 0); // we need a workspace to begin with } @@ -144,23 +146,40 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat const workspaceConfigFolder = dirname(this.getWorkspace().configuration.fsPath); foldersToAdd.forEach(folderToAdd => { - if (this.contains(currentWorkspaceFolderUris, folderToAdd)) { + let folderResource: URI; + let folderName: string; + if (URI.isUri(folderToAdd)) { + folderResource = folderToAdd; + } else { + folderResource = folderToAdd.uri; + folderName = folderToAdd.name; + } + + if (this.contains(currentWorkspaceFolderUris, folderResource)) { return; // already existing } + let storedFolder: IStoredWorkspaceFolder; + // File resource: use "path" property - if (folderToAdd.scheme === Schemas.file) { - storedFoldersToAdd.push({ - path: massageFolderPathForWorkspace(folderToAdd.fsPath, workspaceConfigFolder, currentStoredFolders) - }); + if (folderResource.scheme === Schemas.file) { + storedFolder = { + path: massageFolderPathForWorkspace(folderResource.fsPath, workspaceConfigFolder, currentStoredFolders) + }; } // Any other resource: use "uri" property else { - storedFoldersToAdd.push({ - uri: folderToAdd.toString(true) - }); + storedFolder = { + uri: folderResource.toString(true) + }; + } + + if (folderName) { + storedFolder.name = folderName; } + + storedFoldersToAdd.push(storedFolder); }); if (storedFoldersToAdd.length > 0) { diff --git a/src/vs/workbench/services/configuration/test/node/configurationService.test.ts b/src/vs/workbench/services/configuration/test/node/configurationService.test.ts index 178de894bddd1..877a334578581 100644 --- a/src/vs/workbench/services/configuration/test/node/configurationService.test.ts +++ b/src/vs/workbench/services/configuration/test/node/configurationService.test.ts @@ -205,6 +205,22 @@ suite('WorkspaceContextService - Workspace', () => { }); }); + test('add folders (with name)', () => { + const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath); + return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')), name: 'DDD' }, { uri: URI.file(path.join(workspaceDir, 'c')), name: 'CCC' }]) + .then(() => { + const actual = testObject.getWorkspace().folders; + + assert.equal(actual.length, 4); + assert.equal(path.basename(actual[0].uri.fsPath), 'a'); + assert.equal(path.basename(actual[1].uri.fsPath), 'b'); + assert.equal(path.basename(actual[2].uri.fsPath), 'd'); + assert.equal(path.basename(actual[3].uri.fsPath), 'c'); + assert.equal(actual[2].name, 'DDD'); + assert.equal(actual[3].name, 'CCC'); + }); + }); + test('add folders triggers change event', () => { const target = sinon.spy(); testObject.onDidChangeWorkspaceFolders(target); diff --git a/src/vs/workbench/services/workspace/common/workspaceEditing.ts b/src/vs/workbench/services/workspace/common/workspaceEditing.ts index e7d86635073ac..b5f3bbd56d474 100644 --- a/src/vs/workbench/services/workspace/common/workspaceEditing.ts +++ b/src/vs/workbench/services/workspace/common/workspaceEditing.ts @@ -19,6 +19,7 @@ export interface IWorkspaceEditingService { * Add folders to the existing workspace */ addFolders(folders: URI[]): TPromise; + addFolders(folders: { uri: URI, name?: string }[]): TPromise; /** * Remove folders from the existing workspace diff --git a/src/vs/workbench/services/workspace/node/workspaceEditingService.ts b/src/vs/workbench/services/workspace/node/workspaceEditingService.ts index 5fa421760f46e..b63e8116b472d 100644 --- a/src/vs/workbench/services/workspace/node/workspaceEditingService.ts +++ b/src/vs/workbench/services/workspace/node/workspaceEditingService.ts @@ -44,7 +44,9 @@ export class WorkspaceEditingService implements IWorkspaceEditingService { ) { } - public addFolders(folders: URI[]): TPromise { + public addFolders(foldersToAdd: URI[]): TPromise; + public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise; + public addFolders(folders: any[]): TPromise { return this.contextService.addFolders(folders) .then(() => null, error => this.handleWorkspaceConfigurationEditingError(error)); } diff --git a/src/vs/workbench/test/workbenchTestServices.ts b/src/vs/workbench/test/workbenchTestServices.ts index c8d9c81ce1080..9490b11cc6db3 100644 --- a/src/vs/workbench/test/workbenchTestServices.ts +++ b/src/vs/workbench/test/workbenchTestServices.ts @@ -117,7 +117,9 @@ export class TestContextService implements IWorkspaceContextService { return this.workspace; } - public addFolders(foldersToAdd: URI[]): TPromise { + public addFolders(foldersToAdd: URI[]): TPromise; + public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise; + public addFolders(foldersToAdd: any[]): TPromise { return TPromise.as(void 0); }