Skip to content

Improve "Attach to Host Process" menu flow #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 72 additions & 66 deletions src/features/DebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,93 +67,99 @@ export class PickPSHostProcessFeature implements IFeature {
private command: vscode.Disposable;
private languageClient: LanguageClient;
private waitingForClientToken: vscode.CancellationTokenSource;
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void;

constructor() {

this.command =
vscode.commands.registerCommand('PowerShell.PickPSHostProcess', () => {

if (!this.languageClient && !this.waitingForClientToken) {
return new Promise<string>((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);
});
}

public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;

if (this.waitingForClientToken) {
this.getLanguageClientResolve(this.languageClient);
this.clearWaitingToken();
// Signal language client initialized
}
}

public dispose() {
this.command.dispose();
}

// In node, the function returned a Promise<string> not sure about "Thenable<string>"
private pickPSHostProcess(): Promise<string> {
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<LanguageClient> {
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<LanguageClient>(
(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<string> {
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 : "";
});
});
}
Expand Down