Skip to content

Commit

Permalink
Provide additional workspace API to add/remove workspace folders (for #…
Browse files Browse the repository at this point in the history
…35407) (#36820)

* Provide additional workspace API to add/remove workspace folders (for #35407)

* add/removeFolders => add/removeFolder

* make add/remove folder return a boolean

* use proper service for workspace editing

* workspac => workspace

* do not log promise canceled messages

* show confirm dialog
  • Loading branch information
bpasero authored Oct 30, 2017
1 parent 7ebc204 commit 80ece09
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
28 changes: 28 additions & 0 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5210,6 +5210,34 @@ declare module 'vscode' {
*/
export const onDidChangeWorkspaceFolders: Event<WorkspaceFoldersChangeEvent>;

/**
* Adds a workspace folder to the currently opened workspace.
*
* This method will be a no-op if the folder is already part of the workspace.
*
* Note: if this workspace had no folder opened, all extensions will be restarted
* so that the (deprecated) `rootPath` property is updated to point to the first workspace
* folder.
*
* @param folder a workspace folder to add.
* @return A thenable that resolves when the workspace folder was added successfully.
*/
export function addWorkspaceFolder(uri: Uri, name?: string): Thenable<boolean>;

/**
* Remove a workspace folder from the currently opened workspace.
*
* This method will be a no-op when called while not having a workspace opened.
*
* Note: if the first workspace folder is removed, all extensions will be restarted
* so that the (deprecated) `rootPath` property is updated to point to the first workspace
* folder.
*
* @param folder a [workspace folder](#WorkspaceFolder) to remove.
* @return A thenable that resolves when the workspace folder was removed successfully
*/
export function removeWorkspaceFolder(folder: WorkspaceFolder): Thenable<boolean>;

/**
* Returns the [workspace folder](#WorkspaceFolder) that contains a given uri.
* * returns `undefined` when the given uri doesn't match any workspace folder
Expand Down
54 changes: 52 additions & 2 deletions src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import { MainThreadWorkspaceShape, ExtHostWorkspaceShape, ExtHostContext, MainCo
import { IFileService } from 'vs/platform/files/common/files';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
import { IRelativePattern } from 'vs/base/common/glob';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import { IMessageService } from 'vs/platform/message/common/message';
import { localize } from 'vs/nls';

@extHostNamedCustomer(MainContext.MainThreadWorkspace)
export class MainThreadWorkspace implements MainThreadWorkspaceShape {
Expand All @@ -30,7 +33,9 @@ export class MainThreadWorkspace implements MainThreadWorkspaceShape {
@IWorkspaceContextService private readonly _contextService: IWorkspaceContextService,
@ITextFileService private readonly _textFileService: ITextFileService,
@IConfigurationService private _configurationService: IConfigurationService,
@IFileService private readonly _fileService: IFileService
@IFileService private readonly _fileService: IFileService,
@IWorkspaceEditingService private _workspaceEditingService: IWorkspaceEditingService,
@IMessageService private _messageService: IMessageService
) {
this._proxy = extHostContext.get(ExtHostContext.ExtHostWorkspace);
this._contextService.onDidChangeWorkspaceFolders(this._onDidChangeWorkspace, this, this._toDispose);
Expand All @@ -52,6 +57,51 @@ export class MainThreadWorkspace implements MainThreadWorkspaceShape {
this._proxy.$acceptWorkspaceData(this._contextService.getWorkbenchState() === WorkbenchState.EMPTY ? null : this._contextService.getWorkspace());
}

$addFolder(extensionName: string, uri: URI, name?: string): Thenable<boolean> {
return this.confirmAddRemoveFolder(extensionName, uri, false).then(confirmed => {
if (!confirmed) {
return TPromise.as(false);
}

return this._workspaceEditingService.addFolders([{ uri, name }]).then(() => true);
});
}

$removeFolder(extensionName: string, uri: URI): Thenable<boolean> {
return this.confirmAddRemoveFolder(extensionName, uri, true).then(confirmed => {
if (!confirmed) {
return TPromise.as(false);
}

return this._workspaceEditingService.removeFolders([uri]).then(() => true);
});
}

private confirmAddRemoveFolder(extensionName, uri: URI, isRemove: boolean): Thenable<boolean> {
if (!this._configurationService.getValue<boolean>('workbench.confirmChangesToWorkspaceFromExtensions')) {
return TPromise.as(true); // return confirmed if the setting indicates this
}

return this._messageService.confirm({
message: isRemove ?
localize('folderMessageRemove', "Extension {0} wants to remove a folder from the workspace. Please confirm.", extensionName) :
localize('folderMessageAdd', "Extension {0} wants to add a folder to the workspace. Please confirm.", extensionName),
detail: localize('folderPath', "Folder path: '{0}'", uri.scheme === 'file' ? uri.fsPath : uri.toString()),
type: 'question',
primaryButton: isRemove ? localize('removeFolder', "&&Remove Folder") : localize('addFolder', "&&Add Folder"),
checkbox: {
label: localize('doNotAskAgain', "Do not ask me again")
}
}).then(confirmation => {
let updateConfirmSettingsPromise: TPromise<void> = TPromise.as(void 0);
if (confirmation.confirmed && confirmation.checkboxChecked === true) {
updateConfirmSettingsPromise = this._configurationService.updateValue('workbench.confirmChangesToWorkspaceFromExtensions', false, ConfigurationTarget.USER);
}

return updateConfirmSettingsPromise.then(() => confirmation.confirmed);
});
}

// --- search ---

$startSearch(include: string | IRelativePattern, exclude: string | IRelativePattern, maxResults: number, requestId: number): Thenable<URI[]> {
Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ export function createApiFactory(
set name(value) {
throw errors.readonly();
},
addWorkspaceFolder(uri, name) {
return extHostWorkspace.addWorkspaceFolder(extension.displayName || extension.name, uri, name);
},
removeWorkspaceFolder(folder) {
return extHostWorkspace.removeWorkspaceFolder(extension.displayName || extension.name, folder);
},
onDidChangeWorkspaceFolders: function (listener, thisArgs?, disposables?) {
return extHostWorkspace.onDidChangeWorkspace(listener, thisArgs, disposables);
},
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ export interface MainThreadWorkspaceShape extends IDisposable {
$startSearch(include: string | IRelativePattern, exclude: string | IRelativePattern, maxResults: number, requestId: number): Thenable<URI[]>;
$cancelSearch(requestId: number): Thenable<boolean>;
$saveAll(includeUntitled?: boolean): Thenable<boolean>;
$addFolder(extensioName: string, uri: URI, name?: string): Thenable<boolean>;
$removeFolder(extensioName: string, uri: URI): Thenable<boolean>;
}

export interface MainThreadFileSystemShape extends IDisposable {
Expand Down
12 changes: 12 additions & 0 deletions src/vs/workbench/api/node/extHostWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape {
}
}

addWorkspaceFolder(extensionName: string, uri: URI, name?: string): Thenable<boolean> {
return this._proxy.$addFolder(extensionName, uri, name);
}

removeWorkspaceFolder(extensionName: string, folder: vscode.WorkspaceFolder): Thenable<boolean> {
if (this.getWorkspaceFolders().indexOf(folder) === -1) {
return Promise.resolve(false);
}

return this._proxy.$removeFolder(extensionName, folder.uri);
}

getWorkspaceFolder(uri: vscode.Uri, resolveParent?: boolean): vscode.WorkspaceFolder {
if (!this._workspace) {
return undefined;
Expand Down
15 changes: 10 additions & 5 deletions src/vs/workbench/electron-browser/main.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ let workbenchProperties: { [path: string]: IJSONSchema; } = {
'type': 'boolean',
'description': nls.localize('closeOnFileDelete', "Controls if editors showing a file should close automatically when the file is deleted or renamed by some other process. Disabling this will keep the editor open as dirty on such an event. Note that deleting from within the application will always close the editor and that dirty files will never close to preserve your data."),
'default': true
},
'workbench.confirmChangesToWorkspaceFromExtensions': {
'type': 'boolean',
'description': nls.localize('confirmChangesFromExtensions', "Controls if a confirmation should be shown for extensions that add or remove workspace folders."),
'default': true
}
};

Expand All @@ -256,8 +261,8 @@ if (isMacintosh) {
'enum': ['default', 'antialiased', 'none'],
'default': 'default',
'description':
nls.localize('fontAliasing',
`Controls font aliasing method in the workbench.
nls.localize('fontAliasing',
`Controls font aliasing method in the workbench.
- default: Sub-pixel font smoothing. On most non-retina displays this will give the sharpest text
- antialiased: Smooth the font on the level of the pixel, as opposed to the subpixel. Can make the font appear lighter overall
- none: Disables font smoothing. Text will show with jagged sharp edges`),
Expand Down Expand Up @@ -297,13 +302,13 @@ let properties: { [path: string]: IJSONSchema; } = {
],
'default': 'off',
'description':
nls.localize('openFilesInNewWindow',
`Controls if files should open in a new window.
nls.localize('openFilesInNewWindow',
`Controls if files should open in a new window.
- default: files will open in the window with the files' folder open or the last active window unless opened via the dock or from finder (macOS only)
- on: files will open in a new window
- off: files will open in the window with the files' folder open or the last active window
Note that there can still be cases where this setting is ignored (e.g. when using the -new-window or -reuse-window command line option).`
)
)
},
'window.openFoldersInNewWindow': {
'type': 'string',
Expand Down
11 changes: 8 additions & 3 deletions src/vs/workbench/parts/quickopen/browser/commandsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { BoundedMap, ISerializedBoundedLinkedMap } from 'vs/base/common/map';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ResolvedKeybinding } from 'vs/base/common/keyCodes';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { isPromiseCanceledError } from 'vs/base/common/errors';

export const ALL_COMMANDS_PREFIX = '>';

Expand Down Expand Up @@ -263,9 +264,13 @@ abstract class BaseCommandEntry extends QuickOpenEntryGroup {
return nls.localize('entryAriaLabel', "{0}, commands", this.getLabel());
}

protected onError(error?: Error): void;
protected onError(messagesWithAction?: IMessageWithAction): void;
protected onError(arg1?: any): void {
private onError(error?: Error): void;
private onError(messagesWithAction?: IMessageWithAction): void;
private onError(arg1?: any): void {
if (isPromiseCanceledError(arg1)) {
return;
}

const messagesWithAction: IMessageWithAction = arg1;
if (messagesWithAction && typeof messagesWithAction.message === 'string' && Array.isArray(messagesWithAction.actions)) {
this.messageService.show(Severity.Error, messagesWithAction);
Expand Down

0 comments on commit 80ece09

Please sign in to comment.