提交 752c7b78 编写于 作者: B Benjamin Pasero

first cut updateFolders API

上级 4bcaeaed
......@@ -110,9 +110,9 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat
return this.workspace.getFolder(resource);
}
public addFolders(foldersToAdd: IWorkspaceFolderCreationData[]): TPromise<void> {
public addFolders(foldersToAdd: IWorkspaceFolderCreationData[], index?: number): TPromise<void> {
assert.ok(this.jsonEditingService, 'Workbench is not initialized yet');
return this.workspaceEditingQueue.queue(() => this.doAddFolders(foldersToAdd));
return this.workspaceEditingQueue.queue(() => this.doAddFolders(foldersToAdd, index));
}
public removeFolders(foldersToRemove: URI[]): TPromise<void> {
......@@ -134,7 +134,7 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat
return false;
}
private doAddFolders(foldersToAdd: IWorkspaceFolderCreationData[]): TPromise<void> {
private doAddFolders(foldersToAdd: IWorkspaceFolderCreationData[], index?: number): TPromise<void> {
if (this.getWorkbenchState() !== WorkbenchState.WORKSPACE) {
return TPromise.as(void 0); // we need a workspace to begin with
}
......@@ -176,7 +176,16 @@ export class WorkspaceService extends Disposable implements IWorkspaceConfigurat
});
if (storedFoldersToAdd.length > 0) {
return this.setFolders([...currentStoredFolders, ...storedFoldersToAdd]);
let newStoredWorkspaceFolders: IStoredWorkspaceFolder[] = [];
if (typeof index === 'number' && index >= 0 && index < currentStoredFolders.length - 1) {
newStoredWorkspaceFolders = currentStoredFolders.slice(0);
newStoredWorkspaceFolders.splice(index, 0, ...storedFoldersToAdd);
} else {
newStoredWorkspaceFolders = [...currentStoredFolders, ...storedFoldersToAdd];
}
return this.setFolders(newStoredWorkspaceFolders);
}
return TPromise.as(void 0);
......
......@@ -192,6 +192,34 @@ suite('WorkspaceContextService - Workspace', () => {
});
});
test('add folders (at specific index)', () => {
const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }], 0)
.then(() => {
const actual = testObject.getWorkspace().folders;
assert.equal(actual.length, 4);
assert.equal(path.basename(actual[0].uri.fsPath), 'd');
assert.equal(path.basename(actual[1].uri.fsPath), 'c');
assert.equal(path.basename(actual[2].uri.fsPath), 'a');
assert.equal(path.basename(actual[3].uri.fsPath), 'b');
});
});
test('add folders (at specific wrong index)', () => {
const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }], 10)
.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');
});
});
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' }])
......
......@@ -27,6 +27,12 @@ export interface IWorkspaceEditingService {
*/
removeFolders(folders: URI[], donotNotifyError?: boolean): TPromise<void>;
/**
* Allows to add and remove folders to the existing workspace at once.
* When `donotNotifyError` is `true`, error will be bubbled up otherwise, the service handles the error with proper message and action
*/
updateFolders(index: number, deleteCount?: number, foldersToAdd?: IWorkspaceFolderCreationData[], donotNotifyError?: boolean): TPromise<void>;
/**
* creates a new workspace with the provided folders and opens it. if path is provided
* the workspace will be saved into that location.
......
......@@ -47,7 +47,40 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
) {
}
public updateFolders(index: number, deleteCount?: number, foldersToAdd?: IWorkspaceFolderCreationData[], donotNotifyError?: boolean): TPromise<void> {
const folders = this.contextService.getWorkspace().folders;
if (index < 0 || index > folders.length - 1) {
return TPromise.wrapError(new Error(nls.localize('errorInvalidIndex', "The index for updating workspace folders is invalid.")));
}
if (typeof deleteCount === 'number' && (deleteCount < 0 || index + deleteCount > folders.length - 1)) {
return TPromise.wrapError(new Error(nls.localize('errorInvalidDelete', "The number of workspace folders to delete is invalid.")));
}
const wantsToDelete = typeof deleteCount === 'number';
const wantsToAdd = Array.isArray(foldersToAdd) && foldersToAdd.length;
// Add Folders
if (wantsToAdd && !wantsToDelete) {
return this.doAddFolders(foldersToAdd, index, donotNotifyError);
}
// Delete Folders
if (wantsToDelete && !wantsToAdd) {
return this.removeFolders(folders.slice(index, index + deleteCount).map(f => f.uri));
}
// Add & Delete Folders (first remove and then add to allow for updating existing folders)
return this.removeFolders(folders.slice(index, index + deleteCount).map(f => f.uri)).then(() => {
return this.doAddFolders(foldersToAdd, index, donotNotifyError);
});
}
public addFolders(foldersToAdd: IWorkspaceFolderCreationData[], donotNotifyError: boolean = false): TPromise<void> {
return this.doAddFolders(foldersToAdd, void 0, donotNotifyError);
}
private doAddFolders(foldersToAdd: IWorkspaceFolderCreationData[], index?: number, donotNotifyError: boolean = false): TPromise<void> {
const state = this.contextService.getWorkbenchState();
// If we are in no-workspace or single-folder workspace, adding folders has to
......@@ -66,7 +99,7 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
}
// Delegate addition of folders to workspace service otherwise
return this.contextService.addFolders(foldersToAdd)
return this.contextService.addFolders(foldersToAdd, index)
.then(() => null, error => donotNotifyError ? TPromise.wrapError(error) : this.handleWorkspaceConfigurationEditingError(error));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册