forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.ts
313 lines (262 loc) · 10.8 KB
/
Console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as path from "path";
import vscode = require("vscode");
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
import { ICheckboxQuickPickItem, showCheckboxQuickPick } from "../controls/checkboxQuickPick";
import { IFeature } from "../feature";
import { Logger } from "../logging";
import Settings = require("../settings");
export const EvaluateRequestType = new RequestType<IEvaluateRequestArguments, void, void, void>("evaluate");
export const OutputNotificationType = new NotificationType<IOutputNotificationBody, void>("output");
export const ExecutionStatusChangedNotificationType =
new NotificationType<IExecutionStatusDetails, void>("powerShell/executionStatusChanged");
export const ShowChoicePromptRequestType =
new RequestType<IShowChoicePromptRequestArgs,
IShowChoicePromptResponseBody, string, void>("powerShell/showChoicePrompt");
export const ShowInputPromptRequestType =
new RequestType<IShowInputPromptRequestArgs,
IShowInputPromptResponseBody, string, void>("powerShell/showInputPrompt");
export interface IEvaluateRequestArguments {
expression: string;
}
export interface IOutputNotificationBody {
category: string;
output: string;
}
interface IExecutionStatusDetails {
executionOptions: IExecutionOptions;
executionStatus: ExecutionStatus;
hadErrors: boolean;
}
interface IChoiceDetails {
label: string;
helpMessage: string;
}
interface IShowInputPromptRequestArgs {
name: string;
label: string;
}
interface IShowChoicePromptRequestArgs {
isMultiChoice: boolean;
caption: string;
message: string;
choices: IChoiceDetails[];
defaultChoices: number[];
}
interface IShowChoicePromptResponseBody {
responseText: string;
promptCancelled: boolean;
}
interface IShowInputPromptResponseBody {
responseText: string;
promptCancelled: boolean;
}
enum ExecutionStatus {
Pending,
Running,
Failed,
Aborted,
Completed,
}
interface IExecutionOptions {
writeOutputToHost: boolean;
writeErrorsToHost: boolean;
addToHistory: boolean;
interruptCommandPrompt: boolean;
}
function showChoicePrompt(
promptDetails: IShowChoicePromptRequestArgs,
client: LanguageClient): Thenable<IShowChoicePromptResponseBody> {
let resultThenable: Thenable<IShowChoicePromptResponseBody>;
if (!promptDetails.isMultiChoice) {
let quickPickItems =
promptDetails.choices.map<vscode.QuickPickItem>((choice) => {
return {
label: choice.label,
description: choice.helpMessage,
};
});
if (promptDetails.defaultChoices && promptDetails.defaultChoices.length > 0) {
// Shift the default items to the front of the
// array so that the user can select it easily
const defaultChoice = promptDetails.defaultChoices[0];
if (defaultChoice > -1 &&
defaultChoice < promptDetails.choices.length) {
const defaultChoiceItem = quickPickItems[defaultChoice];
quickPickItems.splice(defaultChoice, 1);
// Add the default choice to the head of the array
quickPickItems = [defaultChoiceItem].concat(quickPickItems);
}
}
resultThenable =
vscode.window
.showQuickPick(
quickPickItems,
{ placeHolder: promptDetails.caption + " - " + promptDetails.message })
.then(onItemSelected);
} else {
const checkboxQuickPickItems =
promptDetails.choices.map<ICheckboxQuickPickItem>((choice) => {
return {
label: choice.label,
description: choice.helpMessage,
isSelected: false,
};
});
// Select the defaults
promptDetails.defaultChoices.forEach((choiceIndex) => {
checkboxQuickPickItems[choiceIndex].isSelected = true;
});
resultThenable =
showCheckboxQuickPick(
checkboxQuickPickItems,
{ confirmPlaceHolder: `${promptDetails.caption} - ${promptDetails.message}`})
.then(onItemsSelected);
}
return resultThenable;
}
function showInputPrompt(
promptDetails: IShowInputPromptRequestArgs,
client: LanguageClient): Thenable<IShowInputPromptResponseBody> {
const resultThenable =
vscode.window.showInputBox({
placeHolder: promptDetails.name + ": ",
}).then(onInputEntered);
return resultThenable;
}
function onItemsSelected(chosenItems: ICheckboxQuickPickItem[]): IShowChoicePromptResponseBody {
if (chosenItems !== undefined) {
return {
promptCancelled: false,
responseText: chosenItems.filter((item) => item.isSelected).map((item) => item.label).join(", "),
};
} else {
// User cancelled the prompt, send the cancellation
return {
promptCancelled: true,
responseText: undefined,
};
}
}
function onItemSelected(chosenItem: vscode.QuickPickItem): IShowChoicePromptResponseBody {
if (chosenItem !== undefined) {
return {
promptCancelled: false,
responseText: chosenItem.label,
};
} else {
// User cancelled the prompt, send the cancellation
return {
promptCancelled: true,
responseText: undefined,
};
}
}
function onInputEntered(responseText: string): IShowInputPromptResponseBody {
if (responseText !== undefined) {
return {
promptCancelled: false,
responseText,
};
} else {
return {
promptCancelled: true,
responseText: undefined,
};
}
}
export class ConsoleFeature implements IFeature {
private commands: vscode.Disposable[];
private languageClient: LanguageClient;
private resolveStatusBarPromise: (value?: {} | PromiseLike<{}>) => void;
constructor(private log: Logger) {
this.commands = [
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {
if (this.languageClient === undefined) {
this.log.writeAndShowError(`<${ConsoleFeature.name}>: ` +
"Unable to instantiate; language client undefined.");
return;
}
if (vscode.window.activeTerminal.name !== "PowerShell Integrated Console") {
this.log.write("PSIC is not active terminal. Running in active terminal using 'runSelectedText'");
await vscode.commands.executeCommand("workbench.action.terminal.runSelectedText");
// We need to honor the focusConsoleOnExecute setting here too. However, the boolean that `show`
// takes is called `preserveFocus` which when `true` the terminal will not take focus.
// This is the inverse of focusConsoleOnExecute so we have to inverse the boolean.
vscode.window.activeTerminal.show(!Settings.load().integratedConsole.focusConsoleOnExecute);
await vscode.commands.executeCommand("workbench.action.terminal.scrollToBottom");
return;
}
// For the Integrated Console, we'll try to replace $PSScriptRoot. In the future, this should
// be the default behavior.
const editor = vscode.window.activeTextEditor;
let selectionRange: vscode.Range;
if (!editor.selection.isEmpty) {
selectionRange =
new vscode.Range(
editor.selection.start,
editor.selection.end);
} else {
selectionRange = editor.document.lineAt(editor.selection.start.line).range;
}
const rawText = vscode.window.activeTextEditor.document.getText(selectionRange);
// Replace $PSScriptRoot with the file path.
// TODO: Handle inside quotes.
const psScriptRootValue = path.resolve(vscode.window.activeTextEditor.document.uri.fsPath, "..");
const formattedText = rawText.replace(/\$PSScriptRoot/i, psScriptRootValue);
// Send the formatted text and scroll the terminal to the bottom.
await vscode.window.activeTerminal.sendText(formattedText, true);
await vscode.commands.executeCommand("workbench.action.terminal.scrollToBottom");
}),
];
}
public dispose() {
// Make sure we cancel any status bar
this.clearStatusBar();
this.commands.forEach((command) => command.dispose());
}
public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
this.languageClient.onRequest(
ShowChoicePromptRequestType,
(promptDetails) => showChoicePrompt(promptDetails, this.languageClient));
this.languageClient.onRequest(
ShowInputPromptRequestType,
(promptDetails) => showInputPrompt(promptDetails, this.languageClient));
// Set up status bar alerts for when PowerShell is executing a script
this.languageClient.onNotification(
ExecutionStatusChangedNotificationType,
(executionStatusDetails) => {
switch (executionStatusDetails.executionStatus) {
// If execution has changed to running, make a notification
case ExecutionStatus.Running:
this.showExecutionStatus("PowerShell");
break;
// If the execution has stopped, destroy the previous notification
case ExecutionStatus.Completed:
case ExecutionStatus.Aborted:
case ExecutionStatus.Failed:
this.clearStatusBar();
break;
}
});
}
private showExecutionStatus(message: string) {
vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
}, (progress) => {
return new Promise((resolve, reject) => {
this.clearStatusBar();
this.resolveStatusBarPromise = resolve;
progress.report({ message });
});
});
}
private clearStatusBar() {
if (this.resolveStatusBarPromise) {
this.resolveStatusBarPromise();
}
}
}