Skip to content

Commit

Permalink
Keep existing items in the same order
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Oct 27, 2017
1 parent f61c650 commit 2c46c3b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 38 deletions.
76 changes: 46 additions & 30 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ import * as vscode from 'vscode';
import * as util from 'util';
import {Guid} from './guid';

enum FormatType {
LOWERCASE,
UPPERCASE,
SNIPPET,
};

interface GuidPickFormat {
format : (g : Guid) => string;
type : FormatType;
preface? : (g : Guid) => string;
epilogue? : (g : Guid) => string;
named? : boolean;
Expand Down Expand Up @@ -79,24 +86,31 @@ export class GuidCommands {
// Use placeholder token that completely selects with double click.
private static _NAME : string = '__NAME__';

private static _basicFormats : GuidPickFormat[] = [
private static _formats : GuidPickFormat[] = [
{
format: (g) => {
return g.toString();
}
},
type: FormatType.LOWERCASE
},
{
format: (g) => {
return g.toString('no-hyphen')
}
return g.toString('braced');
},
type: FormatType.LOWERCASE
},
{
format: (g) => {
return g.toString('braced');
}
}
];
private static _codeSnippets : GuidPickFormat[] = [
return g.toString().toUpperCase();
},
type: FormatType.UPPERCASE
},
{
format: (g) => {
return g.toString('braced').toUpperCase();
},
type: FormatType.UPPERCASE
},
{
named: true,
format: (g) => {
Expand All @@ -107,7 +121,8 @@ export class GuidCommands {
},
epilogue: (g) => {
return '\n';
}
},
type: FormatType.SNIPPET
},
{
named: true,
Expand All @@ -119,7 +134,20 @@ export class GuidCommands {
},
epilogue: (g) => {
return '\n';
}
},
type: FormatType.SNIPPET
},
{
format: (g) => {
return g.toString('no-hyphen')
},
type: FormatType.LOWERCASE
},
{
format: (g) => {
return g.toString('no-hyphen').toUpperCase();
},
type: FormatType.UPPERCASE
}
];

Expand Down Expand Up @@ -173,25 +201,13 @@ export class GuidCommands {
let items : GuidPickItem[] = [];
let nextIndex = 0;

if (showLowercase || (!showUppercase && !showCodeSnippets)) {
for (const format of GuidCommands._basicFormats) {
const item = new GuidPickItem(++nextIndex, guid, format);
items.push(item);
}
}
if (showUppercase) {
for (const lowercaseFormat of GuidCommands._basicFormats) {
const item = new GuidPickItem(++nextIndex, guid, {
format: g => lowercaseFormat.format(g).toUpperCase()
});
items.push(item);
}
}
if (showCodeSnippets) {
for (const format of GuidCommands._codeSnippets) {
const item = new GuidPickItem(++nextIndex, guid, format);
items.push(item);
}
for (const format of GuidCommands._formats) {
if (((showLowercase || (!showUppercase && !showCodeSnippets)) && format.type == FormatType.LOWERCASE) ||
(showUppercase && format.type == FormatType.UPPERCASE) ||
(showCodeSnippets && format.type == FormatType.SNIPPET)) {
const item = new GuidPickItem(++nextIndex, guid, format);
items.push(item);
}
}

return items;
Expand Down
53 changes: 45 additions & 8 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ suite('GuidCommands', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, true, false, true);

assert.equal(items.length, 4);
assert.equal(items.length, 5);

var item = items[0];
assert.strictEqual(item.label, '1');
Expand All @@ -42,7 +42,7 @@ suite('GuidCommands', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, true, false, true);

assert.equal(items.length, 4);
assert.equal(items.length, 5);

var item = items[1];
assert.strictEqual(item.label, '2');
Expand All @@ -54,7 +54,7 @@ suite('GuidCommands', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, true, false, true);

assert.equal(items.length, 4);
assert.equal(items.length, 5);

var item = items[2];
assert.strictEqual(item.label, '3');
Expand All @@ -71,7 +71,7 @@ suite('GuidCommands', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, true, false, true);

assert.equal(items.length, 4);
assert.equal(items.length, 5);

var item = items[3];
assert.strictEqual(item.label, '4');
Expand All @@ -84,11 +84,23 @@ suite('GuidCommands', () => {
);
});

test('quick pick 5 is simple non-hyphenated string with default options', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, true, false, true);

assert.equal(items.length, 5);

var item = items[4];
assert.strictEqual(item.label, '5');
assert.equal(item.description, '12341234123412341234123412341234');
assert.equal(item.text, '12341234123412341234123412341234');
});

test('quick pick items are correct when all settings are true', () => {
const g = Guid.parse('12341234-dead-beef-1234-123412341234');
const items = GuidCommands.getQuickPickItems(g, true, true, true);

assert.equal(items.length, 6);
assert.equal(items.length, 8);

const item1 = items[0];
assert.strictEqual(item1.label, '1');
Expand Down Expand Up @@ -129,13 +141,23 @@ suite('GuidCommands', () => {
'// {12341234-dead-beef-1234-123412341234}\n' +
'DEFINE_GUID(__NAME__, 0x12341234, 0xdead, 0xbeef, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34);\n'
);

const item7 = items[6];
assert.strictEqual(item7.label, '7');
assert.equal(item7.description, '12341234deadbeef1234123412341234');
assert.equal(item7.text, '12341234deadbeef1234123412341234');

const item8 = items[7];
assert.strictEqual(item8.label, '8');
assert.equal(item8.description, '12341234DEADBEEF1234123412341234');
assert.equal(item8.text, '12341234DEADBEEF1234123412341234');
});

test('quick pick items are correct with only lowercase enabled', () => {
const g = Guid.parse('12341234-dead-beef-1234-123412341234');
const items = GuidCommands.getQuickPickItems(g, true, false, false);

assert.equal(items.length, 2);
assert.equal(items.length, 3);

const item1 = items[0];
assert.strictEqual(item1.label, '1');
Expand All @@ -146,13 +168,18 @@ suite('GuidCommands', () => {
assert.strictEqual(item2.label, '2');
assert.equal(item2.description, '{12341234-dead-beef-1234-123412341234}');
assert.equal(item2.text, '{12341234-dead-beef-1234-123412341234}');

const item3 = items[2];
assert.strictEqual(item3.label, '3');
assert.equal(item3.description, '12341234deadbeef1234123412341234');
assert.equal(item3.text, '12341234deadbeef1234123412341234');
});

test('quick pick items are correct with only uppercase enabled', () => {
const g = Guid.parse('12341234-dead-beef-1234-123412341234');
const items = GuidCommands.getQuickPickItems(g, false, true, false);

assert.equal(items.length, 2);
assert.equal(items.length, 3);

const item1 = items[0];
assert.strictEqual(item1.label, '1');
Expand All @@ -163,6 +190,11 @@ suite('GuidCommands', () => {
assert.strictEqual(item2.label, '2');
assert.equal(item2.description, '{12341234-DEAD-BEEF-1234-123412341234}');
assert.equal(item2.text, '{12341234-DEAD-BEEF-1234-123412341234}');

const item3 = items[2];
assert.strictEqual(item3.label, '3');
assert.equal(item3.description, '12341234DEADBEEF1234123412341234');
assert.equal(item3.text, '12341234DEADBEEF1234123412341234');
});


Expand Down Expand Up @@ -197,7 +229,7 @@ suite('GuidCommands', () => {
var g = Guid.parse('12341234-dead-beef-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g, false, false, false);

assert.equal(items.length, 2);
assert.equal(items.length, 3);

// with all options set to false, only the lowercase options are shown
const item1 = items[0];
Expand All @@ -209,5 +241,10 @@ suite('GuidCommands', () => {
assert.strictEqual(item2.label, '2');
assert.equal(item2.description, '{12341234-dead-beef-1234-123412341234}');
assert.equal(item2.text, '{12341234-dead-beef-1234-123412341234}');

const item3 = items[2];
assert.strictEqual(item3.label, '3');
assert.equal(item3.description, '12341234deadbeef1234123412341234');
assert.equal(item3.text, '12341234deadbeef1234123412341234');
});
});

0 comments on commit 2c46c3b

Please sign in to comment.