1
1
import vscode = require( 'vscode' ) ;
2
- import { LanguageClient } from 'vscode-languageclient' ;
3
- import { RequestType , NotificationType , ResponseError } from 'vscode-jsonrpc' ;
2
+ import { LanguageClient , RequestType , NotificationType } from 'vscode-languageclient' ;
4
3
5
4
export namespace EvaluateRequest {
6
5
export const type : RequestType < EvaluateRequestArguments , void , void > =
@@ -21,6 +20,78 @@ export interface OutputNotificationBody {
21
20
output : string ;
22
21
}
23
22
23
+ export namespace ShowChoicePromptNotification {
24
+ export const type : NotificationType < ShowChoicePromptNotificationBody > =
25
+ { get method ( ) { return 'powerShell/showChoicePrompt' ; } } ;
26
+ }
27
+
28
+ interface ChoiceDetails {
29
+ label : string ;
30
+ helpMessage : string ;
31
+ }
32
+
33
+ interface ShowChoicePromptNotificationBody {
34
+ caption : string ;
35
+ message : string ;
36
+ choices : ChoiceDetails [ ] ;
37
+ defaultChoice : number ;
38
+ }
39
+
40
+ export namespace CompleteChoicePromptNotification {
41
+ export const type : NotificationType < CompleteChoicePromptNotificationBody > =
42
+ { get method ( ) { return 'powerShell/completeChoicePrompt' ; } } ;
43
+ }
44
+
45
+ interface CompleteChoicePromptNotificationBody {
46
+ chosenItem : string ;
47
+ promptCancelled : boolean ;
48
+ }
49
+
50
+ function showChoicePrompt (
51
+ promptDetails : ShowChoicePromptNotificationBody ,
52
+ client : LanguageClient ) {
53
+
54
+ var quickPickItems =
55
+ promptDetails . choices . map < vscode . QuickPickItem > ( choice => {
56
+ return {
57
+ label : choice . label ,
58
+ description : choice . helpMessage
59
+ }
60
+ } ) ;
61
+
62
+ // Shift the default item to the front of the
63
+ // array so that the user can select it easily
64
+ if ( promptDetails . defaultChoice > - 1 &&
65
+ promptDetails . defaultChoice < promptDetails . choices . length ) {
66
+
67
+ var defaultChoiceItem = quickPickItems [ promptDetails . defaultChoice ] ;
68
+ quickPickItems . splice ( promptDetails . defaultChoice , 1 ) ;
69
+
70
+ // Add the default choice to the head of the array
71
+ quickPickItems = [ defaultChoiceItem ] . concat ( quickPickItems ) ;
72
+ }
73
+
74
+ vscode . window
75
+ . showQuickPick (
76
+ quickPickItems ,
77
+ { placeHolder : promptDetails . caption + " - " + promptDetails . message } )
78
+ . then ( chosenItem => onItemSelected ( chosenItem , client ) ) ;
79
+ }
80
+
81
+ function onItemSelected ( chosenItem : vscode . QuickPickItem , client : LanguageClient ) {
82
+ if ( chosenItem !== undefined ) {
83
+ client . sendNotification (
84
+ CompleteChoicePromptNotification . type ,
85
+ { chosenItem : chosenItem . label } ) ;
86
+ }
87
+ else {
88
+ // User cancelled the prompt, send the cancellation
89
+ client . sendNotification (
90
+ CompleteChoicePromptNotification . type ,
91
+ { promptCancelled : true } ) ;
92
+ }
93
+ }
94
+
24
95
export function registerConsoleCommands ( client : LanguageClient ) : void {
25
96
26
97
vscode . commands . registerCommand ( 'PowerShell.RunSelection' , ( ) => {
@@ -46,4 +117,8 @@ export function registerConsoleCommands(client: LanguageClient): void {
46
117
consoleChannel . show ( vscode . ViewColumn . Three ) ;
47
118
consoleChannel . append ( output . output ) ;
48
119
} ) ;
120
+
121
+ client . onNotification (
122
+ ShowChoicePromptNotification . type ,
123
+ promptDetails => showChoicePrompt ( promptDetails , client ) ) ;
49
124
}
0 commit comments