Skip to content

Commit 7338689

Browse files
committed
Add input prompt support
This change adds input prompt support to handle the powerShell/showInputPrompt request from the language server. It also converts the existing choice prompt support from using events to using server-sent requests.
1 parent 32bd70f commit 7338689

File tree

1 file changed

+76
-24
lines changed

1 file changed

+76
-24
lines changed

src/features/Console.ts

+76-24
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,46 @@ export interface OutputNotificationBody {
2020
output: string;
2121
}
2222

23-
export namespace ShowChoicePromptNotification {
24-
export const type: NotificationType<ShowChoicePromptNotificationBody> =
23+
export namespace ShowChoicePromptRequest {
24+
export const type: RequestType<ShowChoicePromptRequestArgs, ShowChoicePromptResponseBody, string> =
2525
{ get method() { return 'powerShell/showChoicePrompt'; } };
2626
}
2727

28+
export namespace ShowInputPromptRequest {
29+
export const type: RequestType<ShowInputPromptRequestArgs, ShowInputPromptResponseBody, string> =
30+
{ get method() { return 'powerShell/showInputPrompt'; } };
31+
}
32+
2833
interface ChoiceDetails {
2934
label: string;
3035
helpMessage: string;
3136
}
3237

33-
interface ShowChoicePromptNotificationBody {
38+
interface ShowInputPromptRequestArgs {
39+
name: string;
40+
label: string;
41+
}
42+
43+
interface ShowChoicePromptRequestArgs {
3444
caption: string;
3545
message: string;
3646
choices: ChoiceDetails[];
3747
defaultChoice: number;
3848
}
3949

40-
export namespace CompleteChoicePromptNotification {
41-
export const type: NotificationType<CompleteChoicePromptNotificationBody> =
42-
{ get method() { return 'powerShell/completeChoicePrompt'; } };
50+
interface ShowChoicePromptResponseBody {
51+
chosenItem: string;
52+
promptCancelled: boolean;
4353
}
4454

45-
interface CompleteChoicePromptNotificationBody {
46-
chosenItem: string;
55+
interface ShowInputPromptResponseBody {
56+
responseText: string;
4757
promptCancelled: boolean;
4858
}
4959

5060
function showChoicePrompt(
51-
promptDetails: ShowChoicePromptNotificationBody,
52-
client: LanguageClient) {
61+
promptDetails: ShowChoicePromptRequestArgs,
62+
client: LanguageClient) : Thenable<ShowChoicePromptResponseBody> {
5363

5464
var quickPickItems =
5565
promptDetails.choices.map<vscode.QuickPickItem>(choice => {
@@ -71,24 +81,60 @@ function showChoicePrompt(
7181
quickPickItems = [defaultChoiceItem].concat(quickPickItems);
7282
}
7383

74-
vscode.window
75-
.showQuickPick(
76-
quickPickItems,
77-
{ placeHolder: promptDetails.caption + " - " + promptDetails.message })
78-
.then(chosenItem => onItemSelected(chosenItem, client));
84+
// For some bizarre reason, the quick pick dialog does not
85+
// work if I return the Thenable immediately at this point.
86+
// It only works if I save the thenable to a variable and
87+
// return the variable instead...
88+
var resultThenable =
89+
vscode.window
90+
.showQuickPick(
91+
quickPickItems,
92+
{ placeHolder: promptDetails.caption + " - " + promptDetails.message })
93+
.then(onItemSelected);
94+
95+
return resultThenable;
7996
}
8097

81-
function onItemSelected(chosenItem: vscode.QuickPickItem, client: LanguageClient) {
98+
function showInputPrompt(
99+
promptDetails: ShowInputPromptRequestArgs,
100+
client: LanguageClient) : Thenable<ShowInputPromptResponseBody> {
101+
102+
var resultThenable =
103+
vscode.window.showInputBox({
104+
placeHolder: promptDetails.name + ": "
105+
}).then(onInputEntered)
106+
107+
return resultThenable;
108+
}
109+
110+
function onItemSelected(chosenItem: vscode.QuickPickItem): ShowChoicePromptResponseBody {
82111
if (chosenItem !== undefined) {
83-
client.sendNotification(
84-
CompleteChoicePromptNotification.type,
85-
{ chosenItem: chosenItem.label });
112+
return {
113+
promptCancelled: false,
114+
chosenItem: chosenItem.label
115+
};
86116
}
87117
else {
88118
// User cancelled the prompt, send the cancellation
89-
client.sendNotification(
90-
CompleteChoicePromptNotification.type,
91-
{ promptCancelled: true });
119+
return {
120+
promptCancelled: true,
121+
chosenItem: undefined
122+
};
123+
}
124+
}
125+
126+
function onInputEntered(responseText: string): ShowInputPromptResponseBody {
127+
if (responseText !== undefined) {
128+
return {
129+
promptCancelled: false,
130+
responseText: responseText
131+
}
132+
}
133+
else {
134+
return {
135+
promptCancelled: true,
136+
responseText: undefined
137+
}
92138
}
93139
}
94140

@@ -118,7 +164,13 @@ export function registerConsoleCommands(client: LanguageClient): void {
118164
consoleChannel.append(output.output);
119165
});
120166

121-
client.onNotification(
122-
ShowChoicePromptNotification.type,
167+
var t: Thenable<ShowChoicePromptResponseBody>;
168+
169+
client.onRequest(
170+
ShowChoicePromptRequest.type,
123171
promptDetails => showChoicePrompt(promptDetails, client));
172+
173+
client.onRequest(
174+
ShowInputPromptRequest.type,
175+
promptDetails => showInputPrompt(promptDetails, client));
124176
}

0 commit comments

Comments
 (0)