Skip to content

Commit

Permalink
make workspace service fit to add a workspace folder with a name (for #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Oct 24, 2017
1 parent f6cf092 commit a7b743e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/vs/editor/standalone/browser/simpleServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ export class SimpleWorkspaceContextService implements IWorkspaceContextService {
return true;
}

public addFolders(foldersToAdd: URI[]): TPromise<void> {
public addFolders(foldersToAdd: URI[]): TPromise<void>;
public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise<void>;
public addFolders(foldersToAdd: any[]): TPromise<void> {
return TPromise.as(void 0);
}

Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/workspace/common/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ export interface IWorkspaceContextService {
onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent>;

/**
* Provides access to the workspace object the platform is running with.
* Provides access to the workspace object the platform is running with.
*/
getWorkspace(): IWorkspace;

/**
* add folders to the existing workspace
*/
addFolders(folders: URI[]): TPromise<void>;
addFolders(folders: { uri: URI, name?: string }[]): TPromise<void>;

/**
* remove folders from the existing workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat
return this.workspace.getFolder(resource);
}

public addFolders(foldersToAdd: URI[]): TPromise<void> {
public addFolders(foldersToAdd: URI[]): TPromise<void>;
public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise<void>;
public addFolders(folders: URI[] | { uri: URI, name?: string }[]): TPromise<void> {
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<void> {
Expand All @@ -130,7 +132,7 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat
return false;
}

private doAddFolders(foldersToAdd: URI[]): TPromise<void> {
private doAddFolders(foldersToAdd: (URI | { uri: URI, name?: string })[]): TPromise<void> {
if (this.getWorkbenchState() !== WorkbenchState.WORKSPACE) {
return TPromise.as(void 0); // we need a workspace to begin with
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IWorkspaceEditingService {
* Add folders to the existing workspace
*/
addFolders(folders: URI[]): TPromise<void>;
addFolders(folders: { uri: URI, name?: string }[]): TPromise<void>;

/**
* Remove folders from the existing workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
) {
}

public addFolders(folders: URI[]): TPromise<void> {
public addFolders(foldersToAdd: URI[]): TPromise<void>;
public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise<void>;
public addFolders(folders: any[]): TPromise<void> {
return this.contextService.addFolders(folders)
.then(() => null, error => this.handleWorkspaceConfigurationEditingError(error));
}
Expand Down
4 changes: 3 additions & 1 deletion src/vs/workbench/test/workbenchTestServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export class TestContextService implements IWorkspaceContextService {
return this.workspace;
}

public addFolders(foldersToAdd: URI[]): TPromise<void> {
public addFolders(foldersToAdd: URI[]): TPromise<void>;
public addFolders(foldersToAdd: { uri: URI, name?: string }[]): TPromise<void>;
public addFolders(foldersToAdd: any[]): TPromise<void> {
return TPromise.as(void 0);
}

Expand Down

0 comments on commit a7b743e

Please sign in to comment.