-
Notifications
You must be signed in to change notification settings - Fork 511
Add Debug Runspace command #1782
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
Changes from 1 commit
be326b5
e7d72b5
e72d1fe
35deb7a
64bb51c
0a31064
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,8 @@ | |
"onCommand:PowerShell.ShowSessionConsole", | ||
"onCommand:PowerShell.ShowSessionMenu", | ||
"onCommand:PowerShell.RestartSession", | ||
"onView:PowerShellCommands" | ||
"onView:PowerShellCommands", | ||
"onCommand:PowerShell.PickRunspace" | ||
], | ||
"dependencies": { | ||
"vscode-languageclient": "~5.1.1" | ||
|
@@ -318,6 +319,7 @@ | |
"runtime": "node", | ||
"variables": { | ||
"PickPSHostProcess": "PowerShell.PickPSHostProcess", | ||
"PickRunspace": "PowerShell.PickRunspace", | ||
"SpecifyScriptArgs": "PowerShell.SpecifyScriptArgs" | ||
}, | ||
"languages": [ | ||
|
@@ -407,6 +409,17 @@ | |
"request": "launch", | ||
"cwd": "" | ||
} | ||
}, | ||
{ | ||
"label": "PowerShell: Debug Runspace", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we mention this applies to the "current" PSIC session. In other places, we refer to this as the "Interactive Session". Also, since this is an "attach" it probably should be named something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current was just a way to signal to PSES that we wanna attach to the current process and not some other remote process. I kinda wanted to avoid this all together and just set the process ID to the current PSES process ID but couldn't find a nice clean way to do that. We can call this whatever makes sense. If interactive session makes more sense, let's do it. |
||
"description": "Open runspace picker to select runspace to attach debugger to", | ||
"body": { | ||
"name": "PowerShell Debug Runspace", | ||
"type": "PowerShell", | ||
"request": "attach", | ||
"processId": "current", | ||
"localRunspaceId": "^\"\\${command:PickRunspace}\"" | ||
} | ||
} | ||
], | ||
"configurationAttributes": { | ||
|
@@ -451,6 +464,11 @@ | |
"type": "number", | ||
"description": "Optional: The ID of the runspace to debug in the attached process. Defaults to 1. Works only on PowerShell 5 and above.", | ||
"default": 1 | ||
}, | ||
"localRunspaceId": { | ||
"type": "string", | ||
"description": "The ID of the runspace to debug. Defaults to 1. Works only on PowerShell 5 and above.", | ||
"default": "${command:PickRunspace}" | ||
} | ||
} | ||
} | ||
|
@@ -495,6 +513,13 @@ | |
"type": "powershell", | ||
"request": "launch", | ||
"cwd": "" | ||
}, | ||
{ | ||
"name": "PowerShell Debug Runspace", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be same as what decide to use for a name on line 414. |
||
"type": "powershell", | ||
"request": "attach", | ||
"processId": "current", | ||
"localRunspaceId": "${command:PickRunspace}" | ||
} | ||
] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,3 +381,130 @@ export class PickPSHostProcessFeature implements IFeature { | |
} | ||
} | ||
} | ||
|
||
interface IRunspaceItem extends vscode.QuickPickItem { | ||
id: string; // payload for the QuickPick UI | ||
} | ||
|
||
interface IRunspace { | ||
id: string; | ||
name: string; | ||
availability: string; | ||
} | ||
|
||
export const GetRunspaceRequestType = | ||
new RequestType<any, IGetRunspaceResponseBody, string, void>("powerShell/getRunspace"); | ||
|
||
interface IGetRunspaceResponseBody { | ||
runspaces: IRunspace[]; | ||
} | ||
|
||
export class PickRunspaceFeature implements IFeature { | ||
|
||
private command: vscode.Disposable; | ||
private languageClient: LanguageClient; | ||
private waitingForClientToken: vscode.CancellationTokenSource; | ||
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void; | ||
|
||
constructor() { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you can probably get rid of this newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
this.command = | ||
vscode.commands.registerCommand("PowerShell.PickRunspace", () => { | ||
return this.getLanguageClient() | ||
.then((_) => this.pickRunspace(), (_) => undefined); | ||
}); | ||
} | ||
|
||
public setLanguageClient(languageClient: LanguageClient) { | ||
this.languageClient = languageClient; | ||
|
||
if (this.waitingForClientToken) { | ||
this.getLanguageClientResolve(this.languageClient); | ||
this.clearWaitingToken(); | ||
} | ||
} | ||
|
||
public dispose() { | ||
this.command.dispose(); | ||
} | ||
|
||
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); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
private pickRunspace(): Thenable<string> { | ||
return this.languageClient.sendRequest(GetRunspaceRequestType, null).then((runspaces) => { | ||
const items: IRunspaceItem[] = []; | ||
|
||
for (const p in runspaces) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can probably switch this to a for...of loop then you don't have to index, you can just do: for (const runspace of runspaces) and runspace is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest commit fixes this. There was actually a mismatch in the PSES response type and the VS Code response type. |
||
if (runspaces.hasOwnProperty(p)) { | ||
|
||
// Skip default runspace | ||
if (runspaces[p].id === 1) { | ||
continue; | ||
} | ||
|
||
items.push({ | ||
label: runspaces[p].name, | ||
description: `ID: ${runspaces[p].id} - ${runspaces[p].availability}`, | ||
id: runspaces[p].id, | ||
}); | ||
} | ||
} | ||
|
||
const options: vscode.QuickPickOptions = { | ||
placeHolder: "Select PowerShell runspace to debug", | ||
matchOnDescription: true, | ||
matchOnDetail: true, | ||
}; | ||
|
||
return vscode.window.showQuickPick(items, options).then((item) => { | ||
// Return undefined when user presses Esc. | ||
// This prevents VSCode from opening launch.json in this case which happens if we return "". | ||
return item ? `${item.id}` : undefined; | ||
}); | ||
}); | ||
} | ||
|
||
private clearWaitingToken() { | ||
if (this.waitingForClientToken) { | ||
this.waitingForClientToken.dispose(); | ||
this.waitingForClientToken = undefined; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit, could you sort this up by the other "Pick" command?