From 80ece09d7ff4c7fe11c830420c8c208ad9687787 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 30 Oct 2017 13:49:48 +0000 Subject: [PATCH] Provide additional workspace API to add/remove workspace folders (for #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 --- src/vs/vscode.d.ts | 28 ++++++++++ .../electron-browser/mainThreadWorkspace.ts | 54 ++++++++++++++++++- src/vs/workbench/api/node/extHost.api.impl.ts | 6 +++ src/vs/workbench/api/node/extHost.protocol.ts | 2 + src/vs/workbench/api/node/extHostWorkspace.ts | 12 +++++ .../electron-browser/main.contribution.ts | 15 ++++-- .../quickopen/browser/commandsHandler.ts | 11 ++-- 7 files changed, 118 insertions(+), 10 deletions(-) diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index ef59a9672d031..ca2634256aa7f 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -5210,6 +5210,34 @@ declare module 'vscode' { */ export const onDidChangeWorkspaceFolders: Event; + /** + * 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; + + /** + * 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; + /** * Returns the [workspace folder](#WorkspaceFolder) that contains a given uri. * * returns `undefined` when the given uri doesn't match any workspace folder diff --git a/src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts b/src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts index 3f3e6e72bbe75..45836c967d0f2 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts @@ -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 { @@ -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); @@ -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 { + 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 { + 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 { + if (!this._configurationService.getValue('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 = 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 { diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index fb2762dcc0b45..9c50d45e3fd8f 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -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); }, diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 4101823ca55c3..4c5337d89a6fc 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -331,6 +331,8 @@ export interface MainThreadWorkspaceShape extends IDisposable { $startSearch(include: string | IRelativePattern, exclude: string | IRelativePattern, maxResults: number, requestId: number): Thenable; $cancelSearch(requestId: number): Thenable; $saveAll(includeUntitled?: boolean): Thenable; + $addFolder(extensioName: string, uri: URI, name?: string): Thenable; + $removeFolder(extensioName: string, uri: URI): Thenable; } export interface MainThreadFileSystemShape extends IDisposable { diff --git a/src/vs/workbench/api/node/extHostWorkspace.ts b/src/vs/workbench/api/node/extHostWorkspace.ts index 654d088c5defe..43a113f8c73ba 100644 --- a/src/vs/workbench/api/node/extHostWorkspace.ts +++ b/src/vs/workbench/api/node/extHostWorkspace.ts @@ -78,6 +78,18 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape { } } + addWorkspaceFolder(extensionName: string, uri: URI, name?: string): Thenable { + return this._proxy.$addFolder(extensionName, uri, name); + } + + removeWorkspaceFolder(extensionName: string, folder: vscode.WorkspaceFolder): Thenable { + 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; diff --git a/src/vs/workbench/electron-browser/main.contribution.ts b/src/vs/workbench/electron-browser/main.contribution.ts index fd40c95e05696..efbd0934a1207 100644 --- a/src/vs/workbench/electron-browser/main.contribution.ts +++ b/src/vs/workbench/electron-browser/main.contribution.ts @@ -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 } }; @@ -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`), @@ -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', diff --git a/src/vs/workbench/parts/quickopen/browser/commandsHandler.ts b/src/vs/workbench/parts/quickopen/browser/commandsHandler.ts index 8b5d93e1438d5..27ed3dfa3ffe3 100644 --- a/src/vs/workbench/parts/quickopen/browser/commandsHandler.ts +++ b/src/vs/workbench/parts/quickopen/browser/commandsHandler.ts @@ -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 = '>'; @@ -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);