From 492ae2a0230bda6a033fa02c9d925836ec732352 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 20 Sep 2017 15:06:30 +0200 Subject: [PATCH] Workspace folder picker command (for #32936) (#34648) * Workspace folder picker command (for #32936) * fix test * make this proper API --- .../vscode-api-tests/src/commands.test.ts | 2 +- .../vscode-api-tests/src/window.test.ts | 12 +++++++++ src/vs/vscode.proposed.d.ts | 27 ++++++++++++++++++- src/vs/workbench/api/node/extHost.api.impl.ts | 5 +++- src/vs/workbench/api/node/extHostQuickOpen.ts | 23 ++++++++++++++-- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/extensions/vscode-api-tests/src/commands.test.ts b/extensions/vscode-api-tests/src/commands.test.ts index 47b4d85f34799..0d92795d241fd 100644 --- a/extensions/vscode-api-tests/src/commands.test.ts +++ b/extensions/vscode-api-tests/src/commands.test.ts @@ -134,4 +134,4 @@ suite('commands namespace tests', () => { return Promise.all([a, b, c, d]); }); -}); +}); \ No newline at end of file diff --git a/extensions/vscode-api-tests/src/window.test.ts b/extensions/vscode-api-tests/src/window.test.ts index 275ac5ee5caa2..c197808fd9e57 100644 --- a/extensions/vscode-api-tests/src/window.test.ts +++ b/extensions/vscode-api-tests/src/window.test.ts @@ -349,6 +349,18 @@ suite('window namespace tests', () => { return Promise.all([a, b]); }); + test('showWorkspaceFolderPick', function () { + const p = (window).showWorkspaceFolderPick(undefined); + + return commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem').then(() => { + return p.then(workspace => { + assert.ok(true); + }, error => { + assert.ok(false); + }); + }); + }); + test('Default value for showInput Box accepted even if fails validateInput, #33691', function () { const result = window.showInputBox({ validateInput: (value: string) => { diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 9c41370771897..cadf55ea65c3c 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -23,6 +23,31 @@ declare module 'vscode' { export namespace window { export function showOpenDialog(options: OpenDialogOptions): Thenable; export function showSaveDialog(options: SaveDialogOptions): Thenable; + + /** + * Shows a selection list of [workspace folders](#workspace.workspaceFolders) to pick from. + * Returns `undefined` if no folder is open. + * + * @param options Configures the behavior of the workspace folder list. + * @return A promise that resolves to the workspace folder or `undefined`. + */ + export function showWorkspaceFolderPick(options?: WorkspaceFolderPickOptions): Thenable; + } + + /** + * Options to configure the behaviour of the [workspace folder](#WorkspaceFolder) pick UI. + */ + export interface WorkspaceFolderPickOptions { + + /** + * An optional string to show as place holder in the input box to guide the user what to pick on. + */ + placeHolder?: string; + + /** + * Set to `true` to keep the picker open when focus moves to another part of the editor or to another window. + */ + ignoreFocusOut?: boolean; } // todo@joh discover files etc @@ -172,4 +197,4 @@ declare module 'vscode' { export namespace languages { export function registerColorProvider(selector: DocumentSelector, provider: DocumentColorProvider): Disposable; } -} +} \ No newline at end of file diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index 215451c68f63c..c5707a083c488 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -94,7 +94,7 @@ export function createApiFactory( const extHostDiagnostics = threadService.set(ExtHostContext.ExtHostDiagnostics, new ExtHostDiagnostics(threadService)); const languageFeatures = threadService.set(ExtHostContext.ExtHostLanguageFeatures, new ExtHostLanguageFeatures(threadService, extHostDocuments, extHostCommands, extHostHeapService, extHostDiagnostics)); const extHostFileSystemEvent = threadService.set(ExtHostContext.ExtHostFileSystemEventService, new ExtHostFileSystemEventService()); - const extHostQuickOpen = threadService.set(ExtHostContext.ExtHostQuickOpen, new ExtHostQuickOpen(threadService)); + const extHostQuickOpen = threadService.set(ExtHostContext.ExtHostQuickOpen, new ExtHostQuickOpen(threadService, extHostWorkspace, extHostCommands)); const extHostTerminalService = threadService.set(ExtHostContext.ExtHostTerminalService, new ExtHostTerminalService(threadService)); const extHostSCM = threadService.set(ExtHostContext.ExtHostSCM, new ExtHostSCM(threadService, extHostCommands)); const extHostTask = threadService.set(ExtHostContext.ExtHostTask, new ExtHostTask(threadService, extHostWorkspace)); @@ -347,6 +347,9 @@ export function createApiFactory( showQuickPick(items: any, options: vscode.QuickPickOptions, token?: vscode.CancellationToken) { return extHostQuickOpen.showQuickPick(items, options, token); }, + showWorkspaceFolderPick(options: vscode.WorkspaceFolderPickOptions) { + return extHostQuickOpen.showWorkspaceFolderPick(options); + }, showInputBox(options?: vscode.InputBoxOptions, token?: vscode.CancellationToken) { return extHostQuickOpen.showInput(options, token); }, diff --git a/src/vs/workbench/api/node/extHostQuickOpen.ts b/src/vs/workbench/api/node/extHostQuickOpen.ts index edaaf12385dba..75acd0087043d 100644 --- a/src/vs/workbench/api/node/extHostQuickOpen.ts +++ b/src/vs/workbench/api/node/extHostQuickOpen.ts @@ -7,19 +7,26 @@ import { TPromise } from 'vs/base/common/winjs.base'; import { wireCancellationToken } from 'vs/base/common/async'; import { CancellationToken } from 'vs/base/common/cancellation'; -import { QuickPickOptions, QuickPickItem, InputBoxOptions } from 'vscode'; +import { QuickPickOptions, QuickPickItem, InputBoxOptions, WorkspaceFolderPickOptions, WorkspaceFolder } from 'vscode'; import { MainContext, MainThreadQuickOpenShape, ExtHostQuickOpenShape, MyQuickPickItems, IMainContext } from './extHost.protocol'; +import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace'; +import { ExtHostCommands } from 'vs/workbench/api/node/extHostCommands'; export type Item = string | QuickPickItem; export class ExtHostQuickOpen implements ExtHostQuickOpenShape { private _proxy: MainThreadQuickOpenShape; + private _workspace: ExtHostWorkspace; + private _commands: ExtHostCommands; + private _onDidSelectItem: (handle: number) => void; private _validateInput: (input: string) => string; - constructor(mainContext: IMainContext) { + constructor(mainContext: IMainContext, workspace: ExtHostWorkspace, commands: ExtHostCommands) { this._proxy = mainContext.get(MainContext.MainThreadQuickOpen); + this._workspace = workspace; + this._commands = commands; } showQuickPick(itemsOrItemsPromise: string[] | Thenable, options?: QuickPickOptions, token?: CancellationToken): Thenable; @@ -117,4 +124,16 @@ export class ExtHostQuickOpen implements ExtHostQuickOpenShape { } return undefined; } + + // ---- workspace folder picker + + showWorkspaceFolderPick(options?: WorkspaceFolderPickOptions, token = CancellationToken.None): Thenable { + return this._commands.executeCommand('_workbench.pickWorkspaceFolder', [options]).then((folder: WorkspaceFolder) => { + if (!folder) { + return undefined; + } + + return this._workspace.getWorkspaceFolders().filter(folder => folder.uri.toString() === folder.uri.toString())[0]; + }); + } }