-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathConsole.ts
237 lines (199 loc) · 8.19 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import vscode = require("vscode");
import { NotificationType, RequestType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { ICheckboxQuickPickItem, showCheckboxQuickPick } from "../controls/checkboxQuickPick";
import { ILogger } from "../logging";
import { getSettings } from "../settings";
import { LanguageClientConsumer } from "../languageClientConsumer";
export const EvaluateRequestType = new RequestType<IEvaluateRequestArguments, void, void>("evaluate");
export const OutputNotificationType = new NotificationType<IOutputNotificationBody>("output");
export const ShowChoicePromptRequestType =
new RequestType<IShowChoicePromptRequestArgs,
IShowChoicePromptResponseBody, string>("powerShell/showChoicePrompt");
export const ShowInputPromptRequestType =
new RequestType<IShowInputPromptRequestArgs,
IShowInputPromptResponseBody, string>("powerShell/showInputPrompt");
export interface IEvaluateRequestArguments {
expression: string;
}
export interface IOutputNotificationBody {
category: string;
output: string;
}
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 | undefined;
promptCancelled: boolean;
}
interface IShowInputPromptResponseBody {
responseText: string | undefined;
promptCancelled: boolean;
}
function showChoicePrompt(promptDetails: IShowChoicePromptRequestArgs): 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.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.message })
.then(onItemSelected);
} else {
const checkboxQuickPickItems =
promptDetails.choices.map<ICheckboxQuickPickItem>((choice) => {
return {
label: choice.label,
description: choice.helpMessage,
isSelected: false,
};
});
// Select the defaults
for (const choice of promptDetails.defaultChoices) {
checkboxQuickPickItems[choice].isSelected = true;
}
resultThenable =
showCheckboxQuickPick(
checkboxQuickPickItems,
{ confirmPlaceHolder: promptDetails.message })
.then(onItemsSelected);
}
return resultThenable;
}
async function showInputPrompt(promptDetails: IShowInputPromptRequestArgs): Promise<IShowInputPromptResponseBody> {
const responseText = await vscode.window.showInputBox({ placeHolder: promptDetails.name + ": " });
return onInputEntered(responseText);
}
function onItemsSelected(chosenItems: ICheckboxQuickPickItem[] | undefined): 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 | undefined): 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 | undefined): IShowInputPromptResponseBody {
if (responseText !== undefined) {
return {
promptCancelled: false,
responseText,
};
} else {
return {
promptCancelled: true,
responseText: undefined,
};
}
}
export class ConsoleFeature extends LanguageClientConsumer {
private commands: vscode.Disposable[];
private handlers: vscode.Disposable[] = [];
constructor(private logger: ILogger) {
super();
this.commands = [
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {
if (vscode.window.activeTerminal &&
vscode.window.activeTerminal.name !== "PowerShell Extension") {
this.logger.write("PowerShell Extension Terminal is not active! Running in current 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(!getSettings().integratedConsole.focusConsoleOnExecute);
await vscode.commands.executeCommand("workbench.action.terminal.scrollToBottom");
return;
}
const editor = vscode.window.activeTextEditor;
if (editor === undefined) {
return;
}
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 client = await LanguageClientConsumer.getLanguageClient();
await client.sendRequest(EvaluateRequestType, {
expression: editor.document.getText(selectionRange),
});
// Show the Extension Terminal if it isn't already visible and
// scroll terminal to bottom so new output is visible
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
await vscode.commands.executeCommand("workbench.action.terminal.scrollToBottom");
}),
];
}
public dispose(): void {
for (const command of this.commands) {
command.dispose();
}
for (const handler of this.handlers) {
handler.dispose();
}
}
public override onLanguageClientSet(languageClient: LanguageClient): void {
this.handlers = [
languageClient.onRequest(
ShowChoicePromptRequestType,
(promptDetails) => showChoicePrompt(promptDetails)),
languageClient.onRequest(
ShowInputPromptRequestType,
(promptDetails) => showInputPrompt(promptDetails)),
];
}
}