Skip to content

Add support for new project creation using Plaster #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add support for multi-select choice prompts
This change adds support for the variant of $host.UI.PromptForChoice which
is enabled by the IHostUISupportsMultipleChoiceSelection.
  • Loading branch information
daviwil committed Dec 14, 2016
commit 56fd1a74047e8a5cca03de6e5c44aeb0e456b345
10 changes: 10 additions & 0 deletions examples/PromptExamples.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# Multi-choice prompt
$choices = @(
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Apple", "Apple"
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Banana", "Banana"
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Orange", "Orange"
)

$defaults = [int[]]@(0, 2)
$host.UI.PromptForChoice("Choose a fruit", "You may choose more than one", $choices, $defaults)
102 changes: 73 additions & 29 deletions src/features/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import vscode = require('vscode');
import { IFeature } from '../feature';
import { showCheckboxQuickPick, CheckboxQuickPickItem } from '../checkboxQuickPick'
import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';

export namespace EvaluateRequest {
Expand Down Expand Up @@ -46,14 +47,15 @@ interface ShowInputPromptRequestArgs {
}

interface ShowChoicePromptRequestArgs {
isMultiChoice: boolean;
caption: string;
message: string;
choices: ChoiceDetails[];
defaultChoice: number;
defaultChoices: number[];
}

interface ShowChoicePromptResponseBody {
chosenItem: string;
responseText: string;
promptCancelled: boolean;
}

Expand All @@ -66,36 +68,62 @@ function showChoicePrompt(
promptDetails: ShowChoicePromptRequestArgs,
client: LanguageClient) : Thenable<ShowChoicePromptResponseBody> {

var quickPickItems =
promptDetails.choices.map<vscode.QuickPickItem>(choice => {
return {
label: choice.label,
description: choice.helpMessage
}
});
var resultThenable: Thenable<ShowChoicePromptResponseBody> = undefined;

if (!promptDetails.isMultiChoice) {
var quickPickItems =
promptDetails.choices.map<vscode.QuickPickItem>(choice => {
return {
label: choice.label,
description: choice.helpMessage
}
});

if (promptDetails.defaultChoices &&
promptDetails.defaultChoices.length > 0) {

// Shift the default item to the front of the
// array so that the user can select it easily
if (promptDetails.defaultChoice > -1 &&
promptDetails.defaultChoice < promptDetails.choices.length) {
// Shift the default items to the front of the
// array so that the user can select it easily
var defaultChoice = promptDetails.defaultChoices[0];
if (defaultChoice > -1 &&
defaultChoice < promptDetails.choices.length) {

var defaultChoiceItem = quickPickItems[promptDetails.defaultChoice];
quickPickItems.splice(promptDetails.defaultChoice, 1);
var defaultChoiceItem = quickPickItems[defaultChoice];
quickPickItems.splice(defaultChoice, 1);

// Add the default choice to the head of the array
quickPickItems = [defaultChoiceItem].concat(quickPickItems);
// Add the default choice to the head of the array
quickPickItems = [defaultChoiceItem].concat(quickPickItems);
}
}

resultThenable =
vscode.window
.showQuickPick(
quickPickItems,
{ placeHolder: promptDetails.caption + " - " + promptDetails.message })
.then(onItemSelected);
}
else {
var checkboxQuickPickItems =
promptDetails.choices.map<CheckboxQuickPickItem>(choice => {
return {
label: choice.label,
description: choice.helpMessage,
isSelected: false
}
});

// For some bizarre reason, the quick pick dialog does not
// work if I return the Thenable immediately at this point.
// It only works if I save the thenable to a variable and
// return the variable instead...
var resultThenable =
vscode.window
.showQuickPick(
quickPickItems,
{ placeHolder: promptDetails.caption + " - " + promptDetails.message })
.then(onItemSelected);
// Select the defaults
promptDetails.defaultChoices.forEach(choiceIndex => {
checkboxQuickPickItems[choiceIndex].isSelected = true
});

resultThenable =
showCheckboxQuickPick(
checkboxQuickPickItems,
{ confirmPlaceHolder: `${promptDetails.caption} - ${promptDetails.message}`})
.then(onItemsSelected);
}

return resultThenable;
}
Expand All @@ -112,18 +140,34 @@ function showInputPrompt(
return resultThenable;
}

function onItemsSelected(chosenItems: CheckboxQuickPickItem[]): ShowChoicePromptResponseBody {
if (chosenItems !== undefined) {
return {
promptCancelled: false,
responseText: chosenItems.filter(item => item.isSelected).map(item => item.label).join(", ")
};
}
else {
// User cancelled the prompt, send the cancellation
return {
promptCancelled: true,
responseText: undefined
};
}
}

function onItemSelected(chosenItem: vscode.QuickPickItem): ShowChoicePromptResponseBody {
if (chosenItem !== undefined) {
return {
promptCancelled: false,
chosenItem: chosenItem.label
responseText: chosenItem.label
};
}
else {
// User cancelled the prompt, send the cancellation
return {
promptCancelled: true,
chosenItem: undefined
responseText: undefined
};
}
}
Expand Down