Skip to content

Commit 4710de6

Browse files
committed
Add "Run selection" command
This change introduces a new "Run selection" command which executes the selected section of PowerShell code and displays the output in the Output pane. This makes it easier for a developer to test the code that they are writing in the absence of a full interactive console.
1 parent c190eb0 commit 4710de6

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.2.0
44
### Friday, November 20, 2015
55

6+
- (Experimental) Added a new "Run selection" (F8) command which executes the current code selection and displays the output
67
- Added a new online help command! Press Ctrl+F1 to get help for the symbol under the cursor.
78
- Enabled PowerShell language features for untitled and in-memory (e.g. in Git diff viewer) PowerShell files
89
- Added `powershell.scriptAnalysis.enable` configuration variable to allow disabling script analysis for performance (issue #11)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ that VS Code provides.
1313
- Go to Definition of cmdlets and variables
1414
- Find References of cmdlets and variables
1515
- Document and workspace symbol discovery
16+
- Run selected selection of PowerShell code using `F8`
1617
- Launch online help for the symbol under the cursor using `Ctrl+F1`
1718
- Local script debugging and basic interactive console support!
1819

package.json

+24-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,30 @@
3838
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
3939
},
4040
"contributes": {
41-
"keybindings": [{
42-
"command": "PowerShell.OnlineHelp",
43-
"key": "ctrl+f1",
44-
"when": "editorTextFocus"
45-
}],
46-
"commands": [{
47-
"command": "PowerShell.OnlineHelp",
48-
"title": "Get online help for command",
49-
"category": "PowerShell"
50-
}],
41+
"keybindings": [
42+
{
43+
"command": "PowerShell.OnlineHelp",
44+
"key": "ctrl+f1",
45+
"when": "editorTextFocus && editorLangId == 'powershell'"
46+
},
47+
{
48+
"command": "PowerShell.RunSelection",
49+
"key": "f8",
50+
"when": "editorTextFocus && editorLangId == 'powershell'"
51+
}
52+
],
53+
"commands": [
54+
{
55+
"command": "PowerShell.OnlineHelp",
56+
"title": "Get online help for command",
57+
"category": "PowerShell"
58+
},
59+
{
60+
"command": "PowerShell.RunSelection",
61+
"title": "Run selection",
62+
"category": "PowerShell"
63+
}
64+
],
5165
"snippets": [
5266
{
5367
"language": "powershell",

src/features/Console.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import vscode = require('vscode');
2+
import { LanguageClient } from 'vscode-languageclient';
3+
import { RequestType, NotificationType, ResponseError } from 'vscode-jsonrpc';
4+
5+
export namespace EvaluateRequest {
6+
export const type: RequestType<EvaluateRequestArguments, void, void> =
7+
{ get method() { return 'evaluate'; } };
8+
}
9+
10+
export interface EvaluateRequestArguments {
11+
expression: string;
12+
}
13+
14+
export namespace OutputNotification {
15+
export const type: NotificationType<OutputNotificationBody> =
16+
{ get method() { return 'output'; } };
17+
}
18+
19+
export interface OutputNotificationBody {
20+
category: string;
21+
output: string;
22+
}
23+
24+
export function registerConsoleCommands(client: LanguageClient): void {
25+
26+
vscode.commands.registerCommand('PowerShell.RunSelection', () => {
27+
var editor = vscode.window.activeTextEditor;
28+
29+
client.sendRequest(EvaluateRequest.type, {
30+
expression:
31+
editor.document.getText(
32+
new vscode.Range(
33+
editor.selection.anchor,
34+
editor.selection.active))
35+
});
36+
});
37+
38+
var consoleChannel = vscode.window.createOutputChannel("PowerShell Output");
39+
client.onNotification(OutputNotification.type, (output) => {
40+
consoleChannel.show(vscode.ViewColumn.Three);
41+
consoleChannel.append(output.output);
42+
});
43+
}

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LanguageClient, LanguageClientOptions, Executable } from 'vscode-langua
1111

1212
import { RequestType, NotificationType, ResponseError } from 'vscode-jsonrpc';
1313
import { registerShowHelpCommand } from './features/ShowOnlineHelp';
14+
import { registerConsoleCommands } from './features/Console';
1415

1516
export function activate(context: vscode.ExtensionContext): void {
1617

@@ -95,6 +96,7 @@ export function activate(context: vscode.ExtensionContext): void {
9596

9697
// Register other features
9798
registerShowHelpCommand(client);
99+
registerConsoleCommands(client);
98100
}
99101

100102
function resolveLanguageServerPath(settings: settingsManager.ISettings) : string {

0 commit comments

Comments
 (0)