Skip to content

Commit 6651a20

Browse files
committed
Add choice prompt handling support
This change adds choice prompt handling support by responding to the new powerShell/showChoicePrompt message. When received, the extension will show a quick pick list with the possible choices. If the user selects an option with 'enter' it will be sent for processing. If the quick pick gets cancelled either by pressing `ESC` or the UI losing focus, the prompt will be cancelled in PowerShell.
1 parent e3630d5 commit 6651a20

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"onLanguage:powershell"
3030
],
3131
"dependencies": {
32-
"vscode-languageclient": "0.10.7"
32+
"vscode-languageclient": "1.1.2"
3333
},
3434
"devDependencies": {
3535
"vscode": "0.10.6",

src/features/Console.ts

+77-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
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';
43

54
export namespace EvaluateRequest {
65
export const type: RequestType<EvaluateRequestArguments, void, void> =
@@ -21,6 +20,78 @@ export interface OutputNotificationBody {
2120
output: string;
2221
}
2322

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+
2495
export function registerConsoleCommands(client: LanguageClient): void {
2596

2697
vscode.commands.registerCommand('PowerShell.RunSelection', () => {
@@ -46,4 +117,8 @@ export function registerConsoleCommands(client: LanguageClient): void {
46117
consoleChannel.show(vscode.ViewColumn.Three);
47118
consoleChannel.append(output.output);
48119
});
120+
121+
client.onNotification(
122+
ShowChoicePromptNotification.type,
123+
promptDetails => showChoicePrompt(promptDetails, client));
49124
}

src/features/ExpandAlias.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
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';
43
import Window = vscode.window;
54

65
export namespace ExpandAliasRequest {

src/features/ShowOnlineHelp.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
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';
43

54
export namespace ShowOnlineHelpRequest {
65
export const type: RequestType<string, void, void> = { get method() { return 'powerShell/showOnlineHelp'; } };

src/main.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import path = require('path');
88
import vscode = require('vscode');
99
import settingsManager = require('./settings');
10-
import { LanguageClient, LanguageClientOptions, Executable } from 'vscode-languageclient';
10+
import { LanguageClient, LanguageClientOptions, Executable, RequestType, NotificationType } from 'vscode-languageclient';
1111

12-
import { RequestType, NotificationType, ResponseError } from 'vscode-jsonrpc';
1312
import { registerExpandAliasCommand } from './features/ExpandAlias';
1413
import { registerShowHelpCommand } from './features/ShowOnlineHelp';
1514
import { registerOpenInISECommand } from './features/OpenInISE';

0 commit comments

Comments
 (0)