1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
+ "use strict" ;
5
+
4
6
import vscode = require( "vscode" ) ;
5
7
import { NotificationType , RequestType } from "vscode-languageclient" ;
6
8
import { LanguageClient } from "vscode-languageclient/node" ;
@@ -16,11 +18,11 @@ export const ExecutionStatusChangedNotificationType =
16
18
17
19
export const ShowChoicePromptRequestType =
18
20
new RequestType < IShowChoicePromptRequestArgs ,
19
- IShowChoicePromptResponseBody , string > ( "powerShell/showChoicePrompt" ) ;
21
+ IShowChoicePromptResponseBody , string > ( "powerShell/showChoicePrompt" ) ;
20
22
21
23
export const ShowInputPromptRequestType =
22
24
new RequestType < IShowInputPromptRequestArgs ,
23
- IShowInputPromptResponseBody , string > ( "powerShell/showInputPrompt" ) ;
25
+ IShowInputPromptResponseBody , string > ( "powerShell/showInputPrompt" ) ;
24
26
25
27
export interface IEvaluateRequestArguments {
26
28
expression : string ;
@@ -127,30 +129,22 @@ function showChoicePrompt(
127
129
} ) ;
128
130
129
131
// Select the defaults
130
- promptDetails . defaultChoices . forEach ( ( choiceIndex ) => {
131
- checkboxQuickPickItems [ choiceIndex ] . isSelected = true ;
132
- } ) ;
132
+ for ( const choice of promptDetails . defaultChoices ) {
133
+ checkboxQuickPickItems [ choice ] . isSelected = true ;
134
+ } ;
133
135
134
136
resultThenable =
135
137
showCheckboxQuickPick (
136
- checkboxQuickPickItems ,
137
- { confirmPlaceHolder : promptDetails . message } )
138
+ checkboxQuickPickItems ,
139
+ { confirmPlaceHolder : promptDetails . message } )
138
140
. then ( onItemsSelected ) ;
139
141
}
140
142
141
143
return resultThenable ;
142
144
}
143
145
144
- function showInputPrompt (
145
- promptDetails : IShowInputPromptRequestArgs ,
146
- client : LanguageClient ) : Thenable < IShowInputPromptResponseBody > {
147
-
148
- const resultThenable =
149
- vscode . window . showInputBox ( {
150
- placeHolder : promptDetails . name + ": " ,
151
- } ) . then ( onInputEntered ) ;
152
-
153
- return resultThenable ;
146
+ function showInputPrompt ( promptDetails : IShowInputPromptRequestArgs ) : Thenable < IShowInputPromptResponseBody > {
147
+ return vscode . window . showInputBox ( { placeHolder : promptDetails . name + ": " } ) . then ( onInputEntered ) ;
154
148
}
155
149
156
150
function onItemsSelected ( chosenItems : ICheckboxQuickPickItem [ ] ) : IShowChoicePromptResponseBody {
@@ -199,13 +193,13 @@ function onInputEntered(responseText: string): IShowInputPromptResponseBody {
199
193
200
194
export class ConsoleFeature extends LanguageClientConsumer {
201
195
private commands : vscode . Disposable [ ] ;
196
+ private handlers : vscode . Disposable [ ] ;
202
197
private resolveStatusBarPromise : ( value ?: { } | PromiseLike < { } > ) => void ;
203
198
204
199
constructor ( private log : Logger ) {
205
200
super ( ) ;
206
201
this . commands = [
207
202
vscode . commands . registerCommand ( "PowerShell.RunSelection" , async ( ) => {
208
-
209
203
if ( vscode . window . activeTerminal &&
210
204
vscode . window . activeTerminal . name !== "PowerShell Extension" ) {
211
205
this . log . write ( "PowerShell Extension Terminal is not active! Running in current terminal using 'runSelectedText'" ) ;
@@ -224,15 +218,12 @@ export class ConsoleFeature extends LanguageClientConsumer {
224
218
let selectionRange : vscode . Range ;
225
219
226
220
if ( ! editor . selection . isEmpty ) {
227
- selectionRange =
228
- new vscode . Range (
229
- editor . selection . start ,
230
- editor . selection . end ) ;
221
+ selectionRange = new vscode . Range ( editor . selection . start , editor . selection . end ) ;
231
222
} else {
232
223
selectionRange = editor . document . lineAt ( editor . selection . start . line ) . range ;
233
224
}
234
225
235
- this . languageClient . sendRequest ( EvaluateRequestType , {
226
+ await this . languageClient . sendRequest ( EvaluateRequestType , {
236
227
expression : editor . document . getText ( selectionRange ) ,
237
228
} ) ;
238
229
@@ -247,51 +238,58 @@ export class ConsoleFeature extends LanguageClientConsumer {
247
238
public dispose ( ) {
248
239
// Make sure we cancel any status bar
249
240
this . clearStatusBar ( ) ;
250
- this . commands . forEach ( ( command ) => command . dispose ( ) ) ;
241
+ for ( const command of this . commands ) {
242
+ command . dispose ( ) ;
243
+ }
244
+ for ( const handler of this . handlers ) {
245
+ handler . dispose ( ) ;
246
+ }
251
247
}
252
248
253
249
public setLanguageClient ( languageClient : LanguageClient ) {
254
250
this . languageClient = languageClient ;
255
- this . languageClient . onRequest (
256
- ShowChoicePromptRequestType ,
257
- ( promptDetails ) => showChoicePrompt ( promptDetails , this . languageClient ) ) ;
258
-
259
- this . languageClient . onRequest (
260
- ShowInputPromptRequestType ,
261
- ( promptDetails ) => showInputPrompt ( promptDetails , this . languageClient ) ) ;
262
-
263
- // Set up status bar alerts for when PowerShell is executing a script
264
- this . languageClient . onNotification (
265
- ExecutionStatusChangedNotificationType ,
266
- ( executionStatusDetails ) => {
267
- switch ( executionStatusDetails . executionStatus ) {
268
- // If execution has changed to running, make a notification
269
- case ExecutionStatus . Running :
270
- this . showExecutionStatus ( "PowerShell" ) ;
271
- break ;
272
-
273
- // If the execution has stopped, destroy the previous notification
274
- case ExecutionStatus . Completed :
275
- case ExecutionStatus . Aborted :
276
- case ExecutionStatus . Failed :
277
- this . clearStatusBar ( ) ;
278
- break ;
279
- }
280
- } ) ;
281
-
251
+ this . handlers = [
252
+ this . languageClient . onRequest (
253
+ ShowChoicePromptRequestType ,
254
+ ( promptDetails ) => showChoicePrompt ( promptDetails , this . languageClient ) ) ,
255
+
256
+ this . languageClient . onRequest (
257
+ ShowInputPromptRequestType ,
258
+ ( promptDetails ) => showInputPrompt ( promptDetails ) ) ,
259
+
260
+ // TODO: We're not receiving these events from the server any more.
261
+ // Set up status bar alerts for when PowerShell is executing a script.
262
+ this . languageClient . onNotification (
263
+ ExecutionStatusChangedNotificationType ,
264
+ ( executionStatusDetails ) => {
265
+ switch ( executionStatusDetails . executionStatus ) {
266
+ // If execution has changed to running, make a notification
267
+ case ExecutionStatus . Running :
268
+ this . showExecutionStatus ( "PowerShell" ) ;
269
+ break ;
270
+
271
+ // If the execution has stopped, destroy the previous notification
272
+ case ExecutionStatus . Completed :
273
+ case ExecutionStatus . Aborted :
274
+ case ExecutionStatus . Failed :
275
+ this . clearStatusBar ( ) ;
276
+ break ;
277
+ }
278
+ } )
279
+ ]
282
280
}
283
281
284
282
private showExecutionStatus ( message : string ) {
285
283
vscode . window . withProgress ( {
286
- location : vscode . ProgressLocation . Window ,
287
- } , ( progress ) => {
288
- return new Promise ( ( resolve , reject ) => {
289
- this . clearStatusBar ( ) ;
284
+ location : vscode . ProgressLocation . Window ,
285
+ } , ( progress ) => {
286
+ return new Promise ( ( resolve , _reject ) => {
287
+ this . clearStatusBar ( ) ;
290
288
291
- this . resolveStatusBarPromise = resolve ;
292
- progress . report ( { message } ) ;
293
- } ) ;
289
+ this . resolveStatusBarPromise = resolve ;
290
+ progress . report ( { message } ) ;
294
291
} ) ;
292
+ } ) ;
295
293
}
296
294
297
295
private clearStatusBar ( ) {
0 commit comments