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 all commits
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
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)
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"main": "./out/main",
"activationEvents": [
"onLanguage:powershell"
"onLanguage:powershell",
"onCommand:PowerShell.NewProjectFromTemplate"
],
"dependencies": {
"vscode-languageclient": "1.3.1"
Expand Down Expand Up @@ -128,6 +129,16 @@
"command": "PowerShell.SelectPSSARules",
"title": "Select PSScriptAnalyzer Rules",
"category": "PowerShell"
},
{
"command": "PowerShell.ShowSessionOutput",
"title": "Show Session Output",
"category": "PowerShell"
},
{
"command": "PowerShell.NewProjectFromTemplate",
"title": "Create New Project from Plaster Template",
"category": "PowerShell"
}
],
"snippets": [
Expand Down
145 changes: 84 additions & 61 deletions src/checkboxQuickPick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,97 @@
*--------------------------------------------------------*/

import vscode = require("vscode");
import QuickPickItem = vscode.QuickPickItem;

export class CheckboxQuickPickItem {
name: string;
var confirmItemLabel: string = "$(checklist) Confirm";
var checkedPrefix: string = "[ $(check) ]";
var uncheckedPrefix: string = "[ ]";
var defaultPlaceHolder: string = "Select 'Confirm' to confirm or press 'Esc' key to cancel";

export interface CheckboxQuickPickItem {
label: string;
description?: string;
isSelected: boolean;
}

export class CheckboxQuickPick {
private static readonly confirm: string = "$(check)";
private static readonly checkboxOn: string = "[ x ]";
private static readonly checkboxOff: string = "[ ]";
private static readonly confirmPlaceHolder: string = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes";

public static show(
checkboxQuickPickItems: CheckboxQuickPickItem[],
callback: (items: CheckboxQuickPickItem[]) => void): void {
CheckboxQuickPick.showInner(checkboxQuickPickItems.slice(), callback);
}

private static showInner(
items: CheckboxQuickPickItem[],
callback: (items: CheckboxQuickPickItem[]) => void): void {
export interface CheckboxQuickPickOptions {
confirmPlaceHolder: string;
}

var defaultOptions:CheckboxQuickPickOptions = { confirmPlaceHolder: defaultPlaceHolder};

export function showCheckboxQuickPick(
items: CheckboxQuickPickItem[],
options: CheckboxQuickPickOptions = defaultOptions): Thenable<CheckboxQuickPickItem[]> {

return showInner(items, options).then(
(selectedItem) => {
// We're mutating the original item list so just return it for now.
// If 'selectedItem' is undefined it means the user cancelled the
// inner showQuickPick UI so pass the undefined along.
return selectedItem != undefined ? items : undefined;
})
}

function getQuickPickItems(items: CheckboxQuickPickItem[]): vscode.QuickPickItem[] {

let quickPickItems: vscode.QuickPickItem[] = [];
quickPickItems.push({ label: confirmItemLabel, description: "" });

items.forEach(item =>
quickPickItems.push({
label: convertToCheckBox(item),
description: item.description
}));

return quickPickItems;
}

function showInner(
items: CheckboxQuickPickItem[],
options: CheckboxQuickPickOptions): Thenable<vscode.QuickPickItem> {

var quickPickThenable: Thenable<vscode.QuickPickItem> =
vscode.window.showQuickPick(
CheckboxQuickPick.getQuickPickItems(items),
getQuickPickItems(items),
{
ignoreFocusOut: true,
matchOnDescription: true,
placeHolder: CheckboxQuickPick.confirmPlaceHolder
}).then((selection) => {
if (!selection) {
return;
}

if (selection.label === CheckboxQuickPick.confirm) {
callback(items);
return;
}

let index: number = CheckboxQuickPick.getRuleIndex(items, selection.description);
CheckboxQuickPick.toggleSelection(items[index]);
CheckboxQuickPick.showInner(items, callback);
placeHolder: options.confirmPlaceHolder
});
}

private static getRuleIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
return items.findIndex(item => item.name === itemLabel);
}

private static getQuickPickItems(items: CheckboxQuickPickItem[]): QuickPickItem[] {
let quickPickItems: QuickPickItem[] = [];
quickPickItems.push({ label: CheckboxQuickPick.confirm, description: "Confirm" });
items.forEach(item =>
quickPickItems.push({
label: CheckboxQuickPick.convertToCheckBox(item.isSelected), description: item.name
}));
return quickPickItems;
}

private static toggleSelection(item: CheckboxQuickPickItem): void {
item.isSelected = !item.isSelected;
}

private static convertToCheckBox(state: boolean): string {
if (state) {
return CheckboxQuickPick.checkboxOn;
}
else {
return CheckboxQuickPick.checkboxOff;
}
}

return quickPickThenable.then(
(selection) => {
if (!selection) {
//return Promise.reject<vscode.QuickPickItem>("showCheckBoxQuickPick cancelled")
return Promise.resolve<vscode.QuickPickItem>(undefined);
}

if (selection.label === confirmItemLabel) {
return selection;
}

let index: number = getItemIndex(items, selection.label);

if (index >= 0) {
toggleSelection(items[index]);
}
else {
console.log(`Couldn't find CheckboxQuickPickItem for label '${selection.label}'`);
}

return showInner(items, options);
});
}

function getItemIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
var trimmedLabel = itemLabel.substr(itemLabel.indexOf("]") + 2);
return items.findIndex(item => item.label === trimmedLabel);
}

function toggleSelection(item: CheckboxQuickPickItem): void {
item.isSelected = !item.isSelected;
}

function convertToCheckBox(item: CheckboxQuickPickItem): string {
return `${item.isSelected ? checkedPrefix : uncheckedPrefix} ${item.label}`;
}
116 changes: 83 additions & 33 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;

// 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) {
if (!promptDetails.isMultiChoice) {
var quickPickItems =
promptDetails.choices.map<vscode.QuickPickItem>(choice => {
return {
label: choice.label,
description: choice.helpMessage
}
});

var defaultChoiceItem = quickPickItems[promptDetails.defaultChoice];
quickPickItems.splice(promptDetails.defaultChoice, 1);
if (promptDetails.defaultChoices &&
promptDetails.defaultChoices.length > 0) {

// 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[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 All @@ -144,12 +188,12 @@ function onInputEntered(responseText: string): ShowInputPromptResponseBody {
}

export class ConsoleFeature implements IFeature {
private command: vscode.Disposable;
private commands: vscode.Disposable[];
private languageClient: LanguageClient;
private consoleChannel: vscode.OutputChannel;

constructor() {
this.command =
this.commands = [
vscode.commands.registerCommand('PowerShell.RunSelection', () => {
if (this.languageClient === undefined) {
// TODO: Log error message
Expand All @@ -175,7 +219,13 @@ export class ConsoleFeature implements IFeature {

// Show the output window if it isn't already visible
this.consoleChannel.show(vscode.ViewColumn.Three);
});
}),

vscode.commands.registerCommand('PowerShell.ShowSessionOutput', () => {
// Show the output window if it isn't already visible
this.consoleChannel.show(vscode.ViewColumn.Three);
})
];

this.consoleChannel = vscode.window.createOutputChannel("PowerShell Output");
}
Expand All @@ -197,7 +247,7 @@ export class ConsoleFeature implements IFeature {
}

public dispose() {
this.command.dispose();
this.commands.forEach(command => command.dispose());
this.consoleChannel.dispose();
}
}
Loading