11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4+ "use strict" ;
5+
46import vscode = require( "vscode" ) ;
57import { NotificationType , RequestType } from "vscode-languageclient" ;
68import { LanguageClient } from "vscode-languageclient/node" ;
@@ -16,11 +18,11 @@ export const ExecutionStatusChangedNotificationType =
1618
1719export const ShowChoicePromptRequestType =
1820 new RequestType < IShowChoicePromptRequestArgs ,
19- IShowChoicePromptResponseBody , string > ( "powerShell/showChoicePrompt" ) ;
21+ IShowChoicePromptResponseBody , string > ( "powerShell/showChoicePrompt" ) ;
2022
2123export const ShowInputPromptRequestType =
2224 new RequestType < IShowInputPromptRequestArgs ,
23- IShowInputPromptResponseBody , string > ( "powerShell/showInputPrompt" ) ;
25+ IShowInputPromptResponseBody , string > ( "powerShell/showInputPrompt" ) ;
2426
2527export interface IEvaluateRequestArguments {
2628 expression : string ;
@@ -127,30 +129,22 @@ function showChoicePrompt(
127129 } ) ;
128130
129131 // 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+ } ;
133135
134136 resultThenable =
135137 showCheckboxQuickPick (
136- checkboxQuickPickItems ,
137- { confirmPlaceHolder : promptDetails . message } )
138+ checkboxQuickPickItems ,
139+ { confirmPlaceHolder : promptDetails . message } )
138140 . then ( onItemsSelected ) ;
139141 }
140142
141143 return resultThenable ;
142144}
143145
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 ) ;
154148}
155149
156150function onItemsSelected ( chosenItems : ICheckboxQuickPickItem [ ] ) : IShowChoicePromptResponseBody {
@@ -199,13 +193,13 @@ function onInputEntered(responseText: string): IShowInputPromptResponseBody {
199193
200194export class ConsoleFeature extends LanguageClientConsumer {
201195 private commands : vscode . Disposable [ ] ;
196+ private handlers : vscode . Disposable [ ] ;
202197 private resolveStatusBarPromise : ( value ?: { } | PromiseLike < { } > ) => void ;
203198
204199 constructor ( private log : Logger ) {
205200 super ( ) ;
206201 this . commands = [
207202 vscode . commands . registerCommand ( "PowerShell.RunSelection" , async ( ) => {
208-
209203 if ( vscode . window . activeTerminal &&
210204 vscode . window . activeTerminal . name !== "PowerShell Extension" ) {
211205 this . log . write ( "PowerShell Extension Terminal is not active! Running in current terminal using 'runSelectedText'" ) ;
@@ -224,15 +218,12 @@ export class ConsoleFeature extends LanguageClientConsumer {
224218 let selectionRange : vscode . Range ;
225219
226220 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 ) ;
231222 } else {
232223 selectionRange = editor . document . lineAt ( editor . selection . start . line ) . range ;
233224 }
234225
235- this . languageClient . sendRequest ( EvaluateRequestType , {
226+ await this . languageClient . sendRequest ( EvaluateRequestType , {
236227 expression : editor . document . getText ( selectionRange ) ,
237228 } ) ;
238229
@@ -247,51 +238,58 @@ export class ConsoleFeature extends LanguageClientConsumer {
247238 public dispose ( ) {
248239 // Make sure we cancel any status bar
249240 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+ }
251247 }
252248
253249 public setLanguageClient ( languageClient : LanguageClient ) {
254250 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+ ]
282280 }
283281
284282 private showExecutionStatus ( message : string ) {
285283 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 ( ) ;
290288
291- this . resolveStatusBarPromise = resolve ;
292- progress . report ( { message } ) ;
293- } ) ;
289+ this . resolveStatusBarPromise = resolve ;
290+ progress . report ( { message } ) ;
294291 } ) ;
292+ } ) ;
295293 }
296294
297295 private clearStatusBar ( ) {
0 commit comments