Skip to content

Commit

Permalink
Merge remote master
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Sep 1, 2018
2 parents 381b04b + 3f1275f commit f106826
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 53 deletions.
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
### 1.3.1 (2017-10-26)

* fixed item formats to remain in same order

### 1.3.0 (2017-10-26)

* @thumbquat added non-hyphenated format

### 1.1.0 (2017-07-10)

<<<<<<< HEAD
* ([@101100](https://github.com/101100)) Added configuration options for different formats
* Added CHANGELOG.md
=======
* [[101100](https://github.com/101100)] Added configuration options for different formats
* Added CHANGELOG.md
>>>>>>> b622f6a84d9f1474cb9fa949e607f6e2f1ae2eb0
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-guid",
"version": "1.1.0",
"version": "1.3.1",
"publisher": "heaths",
"displayName": "Insert GUID",
"description": "Insert GUIDs in different formats directly into the editor.",
Expand Down
88 changes: 54 additions & 34 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,19 +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('braced');
}
}
];
private static _codeSnippets : GuidPickFormat[] = [
},
type: FormatType.LOWERCASE
},
{
format: (g) => {
return g.toString().toUpperCase();
},
type: FormatType.UPPERCASE
},
{
format: (g) => {
return g.toString('braced').toUpperCase();
},
type: FormatType.UPPERCASE
},
{
named: true,
format: (g) => {
Expand All @@ -102,7 +121,8 @@ export class GuidCommands {
},
epilogue: (g) => {
return '\n';
}
},
type: FormatType.SNIPPET
},
{
named: true,
Expand All @@ -114,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 All @@ -141,13 +174,12 @@ export class GuidCommands {

// 'edit' no longer valid so start a new edit.
textEditor.edit(edit => {

let current = textEditor.selection;

if (current.isEmpty) {
edit.insert(current.start, item.text);
} else {
edit.replace(current, item.text);
for (const selection of textEditor.selections) {
if (selection.isEmpty) {
edit.insert(selection.start, item.text);
} else {
edit.replace(selection, item.text);
}
}

if (item.named) {
Expand All @@ -169,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
12 changes: 7 additions & 5 deletions src/guid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import * as uuid from 'node-uuid';
import {workspace} from 'vscode';
import {v4, parse, unparse} from 'node-uuid';
import * as util from 'util';

/**
Expand All @@ -34,7 +34,7 @@ export class Guid {
*/
constructor() {
this._buffer = new Buffer(16);
this._buffer = uuid.v4(null, this._buffer);
this._buffer = v4(null, this._buffer);
}

/**
Expand All @@ -49,7 +49,7 @@ export class Guid {
*/
static parse(input : string) : Guid {
let guid = new Guid();
guid._buffer = uuid.parse(input, guid._buffer);
guid._buffer = parse(input, guid._buffer);

return guid;
}
Expand Down Expand Up @@ -77,8 +77,10 @@ export class Guid {
b.toString('hex', 13, 14), b.toString('hex', 14, 15), b.toString('hex', 15, 16));
} else if (format === 'braced' || format === 'b') {
return util.format('{%s}', this.toString());
} else if (format === 'no-hyphen') {
return this.toString().replace(/-/g,'')
} else {
return uuid.unparse(this._buffer);
return unparse(this._buffer);
}
}
}
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');
});
});
5 changes: 5 additions & 0 deletions test/guid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ suite('Guid', () => {
assert.equal(g.toString('braced'), '{01234567-89ab-cdef-0123-456789abcdef}');
});

test('returns proper "no-hyphen" format', () => {
var g = Guid.parse('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString('no-hyphen'), '0123456789abcdef0123456789abcdef');
});

test('returns proper string for missing format', () => {
var g = Guid.parse('01234567-89ab-cdef-0123-456789abcdef');
assert.equal(g.toString(), '01234567-89ab-cdef-0123-456789abcdef');
Expand Down

0 comments on commit f106826

Please sign in to comment.