-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathConsole.ts
240 lines (201 loc) · 7.3 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import vscode = require('vscode');
import { IFeature } from '../feature';
import { showCheckboxQuickPick, CheckboxQuickPickItem } from '../controls/checkboxQuickPick'
import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
export namespace EvaluateRequest {
export const type: RequestType<EvaluateRequestArguments, void, void> =
{ get method() { return 'evaluate'; } };
}
export interface EvaluateRequestArguments {
expression: string;
}
export namespace OutputNotification {
export const type: NotificationType<OutputNotificationBody> =
{ get method() { return 'output'; } };
}
export interface OutputNotificationBody {
category: string;
output: string;
}
export namespace ShowChoicePromptRequest {
export const type: RequestType<ShowChoicePromptRequestArgs, ShowChoicePromptResponseBody, string> =
{ get method() { return 'powerShell/showChoicePrompt'; } };
}
export namespace ShowInputPromptRequest {
export const type: RequestType<ShowInputPromptRequestArgs, ShowInputPromptResponseBody, string> =
{ get method() { return 'powerShell/showInputPrompt'; } };
}
interface ChoiceDetails {
label: string;
helpMessage: string;
}
interface ShowInputPromptRequestArgs {
name: string;
label: string;
}
interface ShowChoicePromptRequestArgs {
isMultiChoice: boolean;
caption: string;
message: string;
choices: ChoiceDetails[];
defaultChoices: number[];
}
interface ShowChoicePromptResponseBody {
responseText: string;
promptCancelled: boolean;
}
interface ShowInputPromptResponseBody {
responseText: string;
promptCancelled: boolean;
}
function showChoicePrompt(
promptDetails: ShowChoicePromptRequestArgs,
client: LanguageClient) : Thenable<ShowChoicePromptResponseBody> {
var resultThenable: Thenable<ShowChoicePromptResponseBody> = undefined;
if (!promptDetails.isMultiChoice) {
var 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
var defaultChoice = promptDetails.defaultChoices[0];
if (defaultChoice > -1 &&
defaultChoice < promptDetails.choices.length) {
var 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 {
var checkboxQuickPickItems =
promptDetails.choices.map<CheckboxQuickPickItem>(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: ShowInputPromptRequestArgs,
client: LanguageClient) : Thenable<ShowInputPromptResponseBody> {
var resultThenable =
vscode.window.showInputBox({
placeHolder: promptDetails.name + ": "
}).then(onInputEntered)
return resultThenable;
}
function onItemsSelected(chosenItems: CheckboxQuickPickItem[]): ShowChoicePromptResponseBody {
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): ShowChoicePromptResponseBody {
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): ShowInputPromptResponseBody {
if (responseText !== undefined) {
return {
promptCancelled: false,
responseText: responseText
}
}
else {
return {
promptCancelled: true,
responseText: undefined
}
}
}
export class ConsoleFeature implements IFeature {
private commands: vscode.Disposable[];
private languageClient: LanguageClient;
constructor() {
this.commands = [
vscode.commands.registerCommand('PowerShell.RunSelection', () => {
if (this.languageClient === undefined) {
// TODO: Log error message
return;
}
var editor = vscode.window.activeTextEditor;
var selectionRange: vscode.Range = undefined;
if (!editor.selection.isEmpty) {
selectionRange =
new vscode.Range(
editor.selection.start,
editor.selection.end);
}
else {
selectionRange = editor.document.lineAt(editor.selection.start.line).range;
}
this.languageClient.sendRequest(EvaluateRequest.type, {
expression: editor.document.getText(selectionRange)
});
// Show the integrated console if it isn't already visible
vscode.commands.executeCommand("PowerShell.ShowSessionConsole");
})
];
}
public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
this.languageClient.onRequest(
ShowChoicePromptRequest.type,
promptDetails => showChoicePrompt(promptDetails, this.languageClient));
this.languageClient.onRequest(
ShowInputPromptRequest.type,
promptDetails => showInputPrompt(promptDetails, this.languageClient));
}
public dispose() {
this.commands.forEach(command => command.dispose());
}
}