Skip to content

Commit

Permalink
Merge pull request #5 from heaths/relabel
Browse files Browse the repository at this point in the history
Update labels for faster use
  • Loading branch information
heaths committed Jan 30, 2016
2 parents 03d3652 + 8320dc4 commit fe3974d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 36 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@

A simple command extension for [Visual Studio Code](https://code.visualstudio.com) to insert globally unique identifiers (GUIDs) into the Code text editor in a variety of formats.

![Insert GUID](http://i.giphy.com/3o7rbIgZ9yKCKbwaTm.gif)
![Insert GUID](http://i.giphy.com/l2JI7IDkCA2ZzPoSk.gif)

This provides the same useful formats as _Create GUID_ that ships with Visual Studio, but without specific language syntaxes for some formats and without unwanted line feeds. Currently, formats supported include:

* String: `11d4dc2e-375a-4b89-9ad4-aa30105385aa`
* Registry: `{11d4dc2e-375a-4b89-9ad4-aa30105385aa}`
* C structure: `static const struct GUID __NAME__ = {0x11d4dc2e, 0x375a, 0x4b89, {0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa}};`
* C macro: `DEFINE_GUID(__NAME__, {x11d4dc2e, 0x375a, 0x4b89, 0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa);`
1. Simple string: `11d4dc2e-375a-4b89-9ad4-aa30105385aa`
2. Registry string: `{11d4dc2e-375a-4b89-9ad4-aa30105385aa}`
3. C structure: `static const struct GUID __NAME__ = {0x11d4dc2e, 0x375a, 0x4b89, {0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa}};`
4. C macro: `DEFINE_GUID(__NAME__, {x11d4dc2e, 0x375a, 0x4b89, 0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa);`

The token `__NAME__` is easy to replace by double clicking to select every character, unlike `<<name>>` used in the _Create GUID_ tool.

## Installation

You can easily search for and install extensions directly within Code.

1. Open the command palette: Ctrl+Shift+P
1. Open the command palette: __Ctrl+Shift+P__ (__Cmd+Shift+P__ on Mac)
2. Type: Install Extension
3. Type: Insert GUID
4. Press __Enter__ or click the download button to install

Code also makes it easy to update extensions:

1. Open the command palette: Ctrl+Shift+P
1. Open the command palette: __Ctrl+Shift+P__
2. Type: Show Outdated Extensions
3. Press __Enter__ or click the download button to install updates

## Using

Whenever you want to insert a new GUID at the cursor or replace the currently selected text:

1. Open the command palette: Ctrl+Shift+P
1. Open the command palette: __Ctrl+Shift+P__ (__Cmd+Shift+P__ on Mac)
2. Type: Insert GUID
3. Press __Enter__ and select the desired format
3. Press __Enter__ and select the desired format by typing the number or using the arrow keys.

You can also skip right to step 3 using the default keyboard binding, __Ctrl+Shift+[__. This is more easily remembered as __Ctrl+{__ as with the curly brace used in the registry string format, or as __Cmd+{__ on the Mac.

## License

Expand Down
6 changes: 0 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
var gulp = require('gulp');
var clean = require('gulp-clean');
var merge = require('gulp-merge');
var mocha = require('gulp-mocha');
var svgmin = require('gulp-svgmin');
var ts = require('gulp-typescript');

Expand All @@ -48,9 +47,4 @@ gulp.task('clean', function() {
.pipe(clean({read: false}));
});

gulp.task('test', ['compile'], function() {
return gulp.src('out/test/**/*.js')
.pipe(mocha({ui: 'tdd'}));
});

gulp.task('default', ['test']);
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-guid",
"version": "0.1.5",
"version": "1.0.0",
"publisher": "heaths",
"displayName": "Insert GUID",
"description": "Insert GUIDs in different formats directly into the editor.",
Expand All @@ -15,6 +15,14 @@
"title": "Insert GUID",
"description": "Insert a GUID at the current position."
}
],
"keybindings": [
{
"command": "guid.insert",
"key": "ctrl+shift+[",
"mac": "cmd+shift+[",
"when": "editorTextFocus"
}
]
},
"activationEvents": [
Expand All @@ -28,8 +36,7 @@
"main": "./out/src/extension",
"scripts": {
"vscode:prepublish": "gulp compile",
"compile": "gulp watch",
"test": "gulp test"
"compile": "gulp watch"
},
"engines": {
"vscode": "^0.10.1"
Expand All @@ -41,10 +48,8 @@
"gulp": "^3.9.0",
"gulp-clean": "^0.3.1",
"gulp-merge": "^0.1.1",
"gulp-mocha": "^2.2.0",
"gulp-svgmin": "^1.2.1",
"gulp-typescript": "^2.10.0",
"mocha": "^2.4.5",
"tsd": "^0.6.5",
"typescript": "^1.6.2",
"vscode": "0.10.x"
Expand Down
52 changes: 36 additions & 16 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ import * as util from 'util';
import {Guid} from './guid';

interface GuidPickFormat {
label : string;
format : (g : Guid) => string;
preface? : (g : Guid) => string;
epilogue? : (g : Guid) => string;
named? : boolean;
}

class GuidPickItem implements vscode.QuickPickItem {
private _index : number;
private _guid : Guid;
private _format : GuidPickFormat;

constructor(_guid : Guid, format : GuidPickFormat) {
this._guid = _guid;
constructor(index : number, guid : Guid, format : GuidPickFormat) {
this._index = index;
this._guid = guid;
this._format = format;
}

// TODO: Localize?
get label() : string {
return this._format.label;
return this._index.toString();
}

get description() : string {
Expand All @@ -56,7 +57,13 @@ class GuidPickItem implements vscode.QuickPickItem {
s = this._format.preface(this._guid);
}

return s + this._format.format(this._guid);
s += this._format.format(this._guid);

if (typeof this._format.epilogue === 'function') {
s += this._format.epilogue(this._guid);
}

return s;
}

get named() : boolean {
Expand All @@ -73,35 +80,37 @@ export class GuidCommands {
private static _NAME : string = '__NAME__';
private static _formats : GuidPickFormat[] = [
{
label: 'String',
format: (g) => {
return g.toString();
}
},
{
label: 'Registry',
format: (g) => {
return g.toString('braced');
}
},
{
label: 'C structure',
named: true,
format: (g) => {
return util.format('static const struct GUID %s = %s;', GuidCommands._NAME, g.toString('struct'));
},
preface: (g) => {
return util.format('// %s\n', g.toString('braced'));
},
epilogue: (g) => {
return '\n';
}
},
{
label: 'C macro',
named: true,
format: (g) => {
return util.format('DEFINE_GUID(%s, %s);', GuidCommands._NAME, g.toString('struct').replace(/[\{\}]/g, ''));
},
preface: (g) => {
return util.format('// %s\n', g.toString('braced'));
},
epilogue: (g) => {
return '\n';
}
}
];
Expand All @@ -113,12 +122,7 @@ export class GuidCommands {
*/
static insertCommand(textEditor : vscode.TextEditor, edit : vscode.TextEditorEdit) {
let g = new Guid();
let items : GuidPickItem[] = [];

GuidCommands._formats.forEach(format => {
let item = new GuidPickItem(g, format);
items.push(item);
});
let items = GuidCommands.getQuickPickItems(g);

// Prompt the user for a format.
vscode.window.showQuickPick<GuidPickItem>(items)
Expand All @@ -145,4 +149,20 @@ export class GuidCommands {
});
});
}

/**
* Gets an array of items to display in the Quick Pick window.
* @param guid The GUID to render in each Quick Pick item.
* @returns An array of items to display in the Quick Pick window.
*/
static getQuickPickItems(guid : Guid) : GuidPickItem[] {
let items : GuidPickItem[] = [];

GuidCommands._formats.forEach((format, index) => {
let item = new GuidPickItem(index + 1, guid, format);
items.push(item);
});

return items;
}
}
86 changes: 86 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// The MIT License (MIT)
//
// Copyright (c) Heath Stewart
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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 assert from 'assert';
import * as vscode from 'vscode';
import {Guid} from '../src/guid';
import {GuidCommands} from '../src/commands';

suite('GuidCommands', () => {
test('quick pick 1 is simple string', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g);

assert.equal(items.length, 4);

var item = items[0];
assert.strictEqual(item.label, '1');
assert.equal(item.description, '12341234-1234-1234-1234-123412341234');
assert.equal(item.text, '12341234-1234-1234-1234-123412341234');
});

test('quick pick 2 is registry string', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g);

assert.equal(items.length, 4);

var item = items[1];
assert.strictEqual(item.label, '2');
assert.equal(item.description, '{12341234-1234-1234-1234-123412341234}');
assert.equal(item.text, '{12341234-1234-1234-1234-123412341234}');
});

test('quick pick 3 is C structure', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g);

assert.equal(items.length, 4);

var item = items[2];
assert.strictEqual(item.label, '3');
assert.equal(item.description,
'static const struct GUID __NAME__ = {0x12341234, 0x1234, 0x1234, {0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34}};'
);
assert.equal(item.text,
'// {12341234-1234-1234-1234-123412341234}\n' +
'static const struct GUID __NAME__ = {0x12341234, 0x1234, 0x1234, {0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34}};\n'
);
});

test('quick pick 4 is C macro', () => {
var g = Guid.parse('12341234-1234-1234-1234-123412341234');
var items = GuidCommands.getQuickPickItems(g);

assert.equal(items.length, 4);

var item = items[3];
assert.strictEqual(item.label, '4');
assert.equal(item.description,
'DEFINE_GUID(__NAME__, 0x12341234, 0x1234, 0x1234, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34);'
);
assert.equal(item.text,
'// {12341234-1234-1234-1234-123412341234}\n' +
'DEFINE_GUID(__NAME__, 0x12341234, 0x1234, 0x1234, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34);\n'
);
});
});

0 comments on commit fe3974d

Please sign in to comment.