Skip to content

Commit 790e316

Browse files
authored
Merge pull request #441 from PowerShell/daviwil/attach-menu-flow
Improve "Attach to Host Process" menu flow
2 parents 30b8670 + 219801e commit 790e316

File tree

1 file changed

+72
-66
lines changed

1 file changed

+72
-66
lines changed

src/features/DebugSession.ts

+72-66
Original file line numberDiff line numberDiff line change
@@ -67,93 +67,99 @@ export class PickPSHostProcessFeature implements IFeature {
6767
private command: vscode.Disposable;
6868
private languageClient: LanguageClient;
6969
private waitingForClientToken: vscode.CancellationTokenSource;
70+
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void;
7071

7172
constructor() {
73+
7274
this.command =
7375
vscode.commands.registerCommand('PowerShell.PickPSHostProcess', () => {
74-
75-
if (!this.languageClient && !this.waitingForClientToken) {
76-
return new Promise<string>((resolve, reject) => {
77-
reject("PowerShell has not fully initialized. Try to attach again after PowerShell has been initialized.");
78-
});
79-
80-
// // If PowerShell isn't finished loading yet, show a loading message
81-
// // until the LanguageClient is passed on to us
82-
// var cancelled = false;
83-
// var timedOut = false;
84-
// this.waitingForClientToken = new vscode.CancellationTokenSource();
85-
86-
// vscode.window
87-
// .showQuickPick(
88-
// ["Cancel"],
89-
// { placeHolder: "Attach to PowerShell host process: Please wait, starting PowerShell..." },
90-
// this.waitingForClientToken.token)
91-
// .then(response => {
92-
// if (response === "Cancel") {
93-
// this.clearWaitingToken();
94-
// }
95-
// });
96-
97-
// // Cancel the loading prompt after 60 seconds
98-
// setTimeout(() => {
99-
// if (this.waitingForClientToken) {
100-
// this.clearWaitingToken();
101-
102-
// vscode.window.showErrorMessage(
103-
// "Attach to PowerShell host process: PowerShell session took too long to start.");
104-
// }
105-
// }, 60000);
106-
107-
// // Wait w/timeout on language client to be initialized and then return this.pickPSHostProcess;
108-
}
109-
else {
110-
return this.pickPSHostProcess();
111-
}
76+
return this.getLanguageClient()
77+
.then(_ => this.pickPSHostProcess(), _ => undefined);
11278
});
11379
}
11480

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

11884
if (this.waitingForClientToken) {
85+
this.getLanguageClientResolve(this.languageClient);
11986
this.clearWaitingToken();
120-
// Signal language client initialized
12187
}
12288
}
12389

12490
public dispose() {
12591
this.command.dispose();
12692
}
12793

128-
// In node, the function returned a Promise<string> not sure about "Thenable<string>"
129-
private pickPSHostProcess(): Promise<string> {
130-
return new Promise((resolve, reject) => {
131-
this.languageClient.sendRequest(GetPSHostProcessesRequest.type, null).then(hostProcesses => {
132-
var items: ProcessItem[] = [];
133-
134-
for(var p in hostProcesses) {
135-
items.push({
136-
label: hostProcesses[p].processName,
137-
description: hostProcesses[p].processId.toString(),
138-
detail: hostProcesses[p].mainWindowTitle,
139-
pid: hostProcesses[p].processId
140-
});
141-
};
142-
143-
if (items.length === 0) {
144-
reject("There are no PowerShell host processes to attach to.");
94+
private getLanguageClient(): Thenable<LanguageClient> {
95+
if (this.languageClient) {
96+
return Promise.resolve(this.languageClient);
97+
}
98+
else {
99+
// If PowerShell isn't finished loading yet, show a loading message
100+
// until the LanguageClient is passed on to us
101+
this.waitingForClientToken = new vscode.CancellationTokenSource();
102+
103+
return new Promise<LanguageClient>(
104+
(resolve, reject) => {
105+
this.getLanguageClientResolve = resolve;
106+
107+
vscode.window
108+
.showQuickPick(
109+
["Cancel"],
110+
{ placeHolder: "Attach to PowerShell host process: Please wait, starting PowerShell..." },
111+
this.waitingForClientToken.token)
112+
.then(response => {
113+
if (response === "Cancel") {
114+
this.clearWaitingToken();
115+
reject();
116+
}
117+
});
118+
119+
// Cancel the loading prompt after 60 seconds
120+
setTimeout(() => {
121+
if (this.waitingForClientToken) {
122+
this.clearWaitingToken();
123+
reject();
124+
125+
vscode.window.showErrorMessage(
126+
"Attach to PowerShell host process: PowerShell session took too long to start.");
127+
}
128+
}, 60000);
145129
}
146-
else {
147-
let options : vscode.QuickPickOptions = {
148-
placeHolder: "Select a PowerShell host process to attach to",
149-
matchOnDescription: true,
150-
matchOnDetail: true
151-
};
152-
153-
return vscode.window.showQuickPick(items, options).then(item => {
154-
resolve(item ? item.pid : "");
155-
});
130+
);
131+
}
132+
}
133+
134+
private pickPSHostProcess(): Thenable<string> {
135+
return this.languageClient.sendRequest(GetPSHostProcessesRequest.type, null).then(hostProcesses => {
136+
var items: ProcessItem[] = [];
137+
138+
for (var p in hostProcesses) {
139+
var windowTitle = "";
140+
if (hostProcesses[p].mainWindowTitle) {
141+
windowTitle = `, Title: ${hostProcesses[p].mainWindowTitle}`;
156142
}
143+
144+
items.push({
145+
label: hostProcesses[p].processName,
146+
description: `PID: ${hostProcesses[p].processId.toString()}${windowTitle}`,
147+
pid: hostProcesses[p].processId
148+
});
149+
};
150+
151+
if (items.length === 0) {
152+
return Promise.reject("There are no PowerShell host processes to attach to.");
153+
}
154+
155+
let options : vscode.QuickPickOptions = {
156+
placeHolder: "Select a PowerShell host process to attach to",
157+
matchOnDescription: true,
158+
matchOnDetail: true
159+
};
160+
161+
return vscode.window.showQuickPick(items, options).then(item => {
162+
return item ? item.pid : "";
157163
});
158164
});
159165
}

0 commit comments

Comments
 (0)