From 9feebedeb148188e7ef256ecd6a4a9cf37a9d056 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sat, 4 Feb 2017 16:05:14 -0700 Subject: [PATCH] Find module display quick pick list first. This PR puts the quick pick list up first with "Cancel" as the only item and text that indicates what is happening and that it could take some time. @daviwil - please check my use of the CancellationTokenSource - especially in the pickPowerShellModule function where the request completes but the user (or timeout) has already cancelled the op. Not sure about returning Promise.resolve(""). My JavaScript FU is pretty weak. Also feel free to help with the wording of the various messages in the UI. Finally what timeout should we use? I set it for 1 minute but that might be long. It seems to take ~20 seconds on my machine. But as the gallery grows, so could that time. Finally, for the future, it would be nice if we could detect a user key press and if the find isn't done - cancel and restart a new find based on what the user typed. Also, need timer support so that you only kick off a new find after say 1 second of paused typing. --- src/features/PowerShellFindModule.ts | 90 +++++++++++++++++++--------- 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/src/features/PowerShellFindModule.ts b/src/features/PowerShellFindModule.ts index 831ad8df1b..a95ca40675 100644 --- a/src/features/PowerShellFindModule.ts +++ b/src/features/PowerShellFindModule.ts @@ -20,28 +20,35 @@ export class FindModuleFeature implements IFeature { private command: vscode.Disposable; private languageClient: LanguageClient; + private cancelFindToken: vscode.CancellationTokenSource; constructor() { this.command = vscode.commands.registerCommand('PowerShell.PowerShellFindModule', () => { - var items: QuickPickItem[] = []; + // It takes a while to get the list of PowerShell modules, display some UI to let user know + this.cancelFindToken = new vscode.CancellationTokenSource(); + vscode.window + .showQuickPick( + ["Cancel"], + { placeHolder: "Please wait, retrieving list of PowerShell modules. This can take some time..." }, + this.cancelFindToken.token) + .then(response => { if (response === "Cancel") { this.clearCancelFindToken(); } }); + + // Cancel the loading prompt after 60 seconds + setTimeout(() => { + if (this.cancelFindToken) { + this.clearCancelFindToken(); + + vscode.window.showErrorMessage( + "The online source for PowerShell modules is not responding. Cancelling Find/Install PowerShell command."); + } + }, 60000); - vscode.window.setStatusBarMessage(this.getCurrentTime() + " Initializing..."); - this.languageClient.sendRequest(FindModuleRequest.type, null).then((modules) => { - for(var item in modules) { - items.push({ label: modules[item].name, description: modules[item].description }); - }; - - vscode.window.setStatusBarMessage(""); - Window.showQuickPick(items,{placeHolder: "Results: (" + modules.length + ")"}).then((selection) => { - if (!selection) { return; } - switch (selection.label) { - default : - var moduleName = selection.label; - //vscode.window.setStatusBarMessage("Installing PowerShell Module " + moduleName, 1500); - this.languageClient.sendRequest(InstallModuleRequest.type, moduleName); - } - }); - }); + this.pickPowerShellModule().then((moduleName) => { + if (moduleName) { + // vscode.window.setStatusBarMessage("Installing PowerShell Module " + moduleName, 1500); + this.languageClient.sendRequest(InstallModuleRequest.type, moduleName); + } + }); }); } @@ -53,18 +60,43 @@ export class FindModuleFeature implements IFeature { this.command.dispose(); } - private getCurrentTime() { + private pickPowerShellModule(): Thenable { + return this.languageClient.sendRequest(FindModuleRequest.type, null).then((modules) => { + var items: QuickPickItem[] = []; + + // We've got the modules info, let's cancel the timeout unless it's already been cancelled + if (this.cancelFindToken) { + this.clearCancelFindToken(); + } + else { + // Already timed out, would be weird to dislay modules after we said it timed out. + return Promise.resolve(""); + } - var timeNow = new Date(); - var hours = timeNow.getHours(); - var minutes = timeNow.getMinutes(); - var seconds = timeNow.getSeconds(); + for (var item in modules) { + items.push({ label: modules[item].name, description: modules[item].description }); + }; - var timeString = "" + ((hours > 12) ? hours - 12 : hours); - timeString += ((minutes < 10) ? ":0" : ":") + minutes; - timeString += ((seconds < 10) ? ":0" : ":") + seconds; - timeString += (hours >= 12) ? " PM" : " AM"; + if (items.length === 0) { + return Promise.reject("No PowerShell modules were found."); + } - return timeString; + let options: vscode.QuickPickOptions = { + placeHolder: "Select a PowerShell module to install", + matchOnDescription: true, + matchOnDetail: true + }; + + return vscode.window.showQuickPick(items, options).then(item => { + return item ? item.label : ""; + }); + }); } -} \ No newline at end of file + + private clearCancelFindToken() { + if (this.cancelFindToken) { + this.cancelFindToken.dispose(); + this.cancelFindToken = undefined; + } + } +}