From 219801e8d409f9cef14dc9815a51a9b0d076ed39 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 18 Jan 2017 12:06:28 -0800 Subject: [PATCH] Improve "Attach to Host Process" menu flow This change improves the flow of the "Attach to Host Process" menu when the language service hasn't started yet. It also changes the text display slightly. --- src/features/DebugSession.ts | 138 ++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 66 deletions(-) diff --git a/src/features/DebugSession.ts b/src/features/DebugSession.ts index 717192306b..ae67c2e6f3 100644 --- a/src/features/DebugSession.ts +++ b/src/features/DebugSession.ts @@ -67,48 +67,14 @@ export class PickPSHostProcessFeature implements IFeature { private command: vscode.Disposable; private languageClient: LanguageClient; private waitingForClientToken: vscode.CancellationTokenSource; + private getLanguageClientResolve: (value?: LanguageClient | Thenable) => void; constructor() { + this.command = vscode.commands.registerCommand('PowerShell.PickPSHostProcess', () => { - - if (!this.languageClient && !this.waitingForClientToken) { - return new Promise((resolve, reject) => { - reject("PowerShell has not fully initialized. Try to attach again after PowerShell has been initialized."); - }); - - // // If PowerShell isn't finished loading yet, show a loading message - // // until the LanguageClient is passed on to us - // var cancelled = false; - // var timedOut = false; - // this.waitingForClientToken = new vscode.CancellationTokenSource(); - - // vscode.window - // .showQuickPick( - // ["Cancel"], - // { placeHolder: "Attach to PowerShell host process: Please wait, starting PowerShell..." }, - // this.waitingForClientToken.token) - // .then(response => { - // if (response === "Cancel") { - // this.clearWaitingToken(); - // } - // }); - - // // Cancel the loading prompt after 60 seconds - // setTimeout(() => { - // if (this.waitingForClientToken) { - // this.clearWaitingToken(); - - // vscode.window.showErrorMessage( - // "Attach to PowerShell host process: PowerShell session took too long to start."); - // } - // }, 60000); - - // // Wait w/timeout on language client to be initialized and then return this.pickPSHostProcess; - } - else { - return this.pickPSHostProcess(); - } + return this.getLanguageClient() + .then(_ => this.pickPSHostProcess(), _ => undefined); }); } @@ -116,8 +82,8 @@ export class PickPSHostProcessFeature implements IFeature { this.languageClient = languageClient; if (this.waitingForClientToken) { + this.getLanguageClientResolve(this.languageClient); this.clearWaitingToken(); - // Signal language client initialized } } @@ -125,35 +91,75 @@ export class PickPSHostProcessFeature implements IFeature { this.command.dispose(); } - // In node, the function returned a Promise not sure about "Thenable" - private pickPSHostProcess(): Promise { - return new Promise((resolve, reject) => { - this.languageClient.sendRequest(GetPSHostProcessesRequest.type, null).then(hostProcesses => { - var items: ProcessItem[] = []; - - for(var p in hostProcesses) { - items.push({ - label: hostProcesses[p].processName, - description: hostProcesses[p].processId.toString(), - detail: hostProcesses[p].mainWindowTitle, - pid: hostProcesses[p].processId - }); - }; - - if (items.length === 0) { - reject("There are no PowerShell host processes to attach to."); + private getLanguageClient(): Thenable { + if (this.languageClient) { + return Promise.resolve(this.languageClient); + } + else { + // If PowerShell isn't finished loading yet, show a loading message + // until the LanguageClient is passed on to us + this.waitingForClientToken = new vscode.CancellationTokenSource(); + + return new Promise( + (resolve, reject) => { + this.getLanguageClientResolve = resolve; + + vscode.window + .showQuickPick( + ["Cancel"], + { placeHolder: "Attach to PowerShell host process: Please wait, starting PowerShell..." }, + this.waitingForClientToken.token) + .then(response => { + if (response === "Cancel") { + this.clearWaitingToken(); + reject(); + } + }); + + // Cancel the loading prompt after 60 seconds + setTimeout(() => { + if (this.waitingForClientToken) { + this.clearWaitingToken(); + reject(); + + vscode.window.showErrorMessage( + "Attach to PowerShell host process: PowerShell session took too long to start."); + } + }, 60000); } - else { - let options : vscode.QuickPickOptions = { - placeHolder: "Select a PowerShell host process to attach to", - matchOnDescription: true, - matchOnDetail: true - }; - - return vscode.window.showQuickPick(items, options).then(item => { - resolve(item ? item.pid : ""); - }); + ); + } + } + + private pickPSHostProcess(): Thenable { + return this.languageClient.sendRequest(GetPSHostProcessesRequest.type, null).then(hostProcesses => { + var items: ProcessItem[] = []; + + for (var p in hostProcesses) { + var windowTitle = ""; + if (hostProcesses[p].mainWindowTitle) { + windowTitle = `, Title: ${hostProcesses[p].mainWindowTitle}`; } + + items.push({ + label: hostProcesses[p].processName, + description: `PID: ${hostProcesses[p].processId.toString()}${windowTitle}`, + pid: hostProcesses[p].processId + }); + }; + + if (items.length === 0) { + return Promise.reject("There are no PowerShell host processes to attach to."); + } + + let options : vscode.QuickPickOptions = { + placeHolder: "Select a PowerShell host process to attach to", + matchOnDescription: true, + matchOnDetail: true + }; + + return vscode.window.showQuickPick(items, options).then(item => { + return item ? item.pid : ""; }); }); }