Skip to content
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

Add more custom format specifiers #85

Merged
merged 9 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This provides the same useful formats as _Create GUID_ that ships with Visual St
4. C macro: `DEFINE_GUID(__NAME__, {x11d4dc2e, 0x375a, 0x4b89, 0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa);`
5. Unformatted string: `11d4dc2e375a4b899ad4aa30105385aa`
6. C structure only: `{0x11d4dc2e, 0x375a, 0x4b89, {0x9a, 0xd4, 0xaa, 0x30, 0x10, 0x53, 0x85, 0xaa}}`
7. User-specified formats: any way you wish, see [Options](#options).

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

Expand Down Expand Up @@ -49,7 +50,7 @@ You can set different configuration options to control the format of GUIDs that
* insertGuid.showLowercase: Show lowercase GUIDs (with and without braces) when presenting possible GUID formats to insert. The default is `true`.
* insertGuid.showUppercase: Show uppercase GUIDs (with and without braces) when presenting possible GUID formats to insert. The default is `false`.
* insertGuid.showCodeSnippets: Show code snippets for C++ when presenting possible GUID formats to insert. The default is `true`.
* insertGuid.pasteAutomatically: When not empty, paste the GUID in a specified format without showing selection menu. The default is "".
* insertGuid.pasteAutomatically: When not empty, paste the GUID in a specified format without showing selection menu. The default is "". See the option description in the settings for more information on the format specification.

## License

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"insertGuid.pasteAutomatically": {
"type": "string",
"default": "",
"markdownDescription": "Paste GUID without prompting using the specified format:\n* `{b}|{B}` inserts a braced string in lowercase `{b}` or uppercase `{B}` e.g., `{880c86bc-384c-4cce-9e9a-4f760ca755c4}`\n* `{d}|{D}` inserts a hyphenated string in lowercase `{d}` or uppercase `{D}` e.g., `880c86bc-384c-4cce-9e9a-4f760ca755c4`\n* `{n}|{N}` inserts an unformatted string in lowercase `{n}` or uppercase `{N}` e.g., `880c86bc384c4cce9e9a4f760ca755c4`\n* `{x}|{X}` inserts a struct-formatted string in lowercase `{x}` or uppercase `{X}` e.g., `{0x880c86bc,0x384c,0x4cce,{0x9e,0x9a,0x4f,0x76,0x0c,0xa7,0x55,0xc4}}`\n\nOther characters will be interpreted literally e.g., `new GUID(\"{D}\")` inserts `new GUID(\"880C86BC-384C-4CCE-9E9A-4F760CA755C4\")`"
"markdownDescription": "Paste GUID without prompting using the specified format:\n* `{b}|{B}` inserts a braced string in lowercase `{b}` or uppercase `{B}` e.g., `{880c86bc-384c-4cce-9e9a-4f760ca755c4}`\n* `{d}|{D}` inserts a hyphenated string in lowercase `{d}` or uppercase `{D}` e.g., `880c86bc-384c-4cce-9e9a-4f760ca755c4`\n* `{n}|{N}` inserts an unformatted string in lowercase `{n}` or uppercase `{N}` e.g., `880c86bc384c4cce9e9a4f760ca755c4`\n* `{x}|{X}` inserts a struct-formatted string in lowercase `{x}` or uppercase `{X}` e.g., `{0x880c86bc,0x384c,0x4cce,{0x9e,0x9a,0x4f,0x76,0x0c,0xa7,0x55,0xc4}}`\n* `{x0}` inserts the first four-byte section. This and the other numbered replacements are in hex format, e.g. `5f9b` and also can be uppercase, e.g. `{X0}` -> `5F9B`.\n* `{x1}` and `{x2}` insert the next two two-byte sections.\n* `{x3}` through `{x10}` insert the last eight bytes individually.\n* `{nl}` inserts a newline.\n\nOther characters will be interpreted literally e.g., `new GUID(\"{D}\")` inserts `new GUID(\"880C86BC-384C-4CCE-9E9A-4F760CA755C4\")`"
}
}
},
Expand Down
23 changes: 23 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ async function insertCommandImpl(textEditor: vscode.TextEditor, edit: vscode.Tex
'{N}': (g: Guid) => g.toString('no-hyphen').toUpperCase(),
'{x}': (g: Guid) => g.toString('x'),
'{X}': (g: Guid) => g.toString('x').toUpperCase(),
'{x0}': (g: Guid) => g.toString('x0'),
'{x1}': (g: Guid) => g.toString('x1'),
'{x2}': (g: Guid) => g.toString('x2'),
'{x3}': (g: Guid) => g.toString('x3'),
'{x4}': (g: Guid) => g.toString('x4'),
'{x5}': (g: Guid) => g.toString('x5'),
'{x6}': (g: Guid) => g.toString('x6'),
'{x7}': (g: Guid) => g.toString('x7'),
'{x8}': (g: Guid) => g.toString('x8'),
'{x9}': (g: Guid) => g.toString('x9'),
'{x10}': (g: Guid) => g.toString('x10'),
'{X0}': (g: Guid) => g.toString('x0').toUpperCase(),
'{X1}': (g: Guid) => g.toString('x1').toUpperCase(),
'{X2}': (g: Guid) => g.toString('x2').toUpperCase(),
'{X3}': (g: Guid) => g.toString('x3').toUpperCase(),
'{X4}': (g: Guid) => g.toString('x4').toUpperCase(),
'{X5}': (g: Guid) => g.toString('x5').toUpperCase(),
'{X6}': (g: Guid) => g.toString('x6').toUpperCase(),
'{X7}': (g: Guid) => g.toString('x7').toUpperCase(),
'{X8}': (g: Guid) => g.toString('x8').toUpperCase(),
'{X9}': (g: Guid) => g.toString('x9').toUpperCase(),
'{X10}': (g: Guid) => g.toString('x10').toUpperCase(),
'{nl}': (g: Guid) => '\n',
}
const customFormatter = {
format: (g: Guid) => {
Expand Down
22 changes: 22 additions & 0 deletions src/guid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ export class Guid {
return util.format('{%s}', this.toString());
} else if (format === 'no-hyphen') {
return this.toString().replace(/-/g, '');
} else if (format === 'x0') {
return b.toString('hex', 0, 4);
} else if (format === 'x1') {
return b.toString('hex', 4, 6);
} else if (format === 'x2') {
return b.toString('hex', 6, 8);
} else if (format === 'x3') {
return b.toString('hex', 8, 9);
} else if (format === 'x4') {
return b.toString('hex', 9, 10);
} else if (format === 'x5') {
return b.toString('hex', 10, 11);
} else if (format === 'x6') {
return b.toString('hex', 11, 12);
} else if (format === 'x7') {
return b.toString('hex', 12, 13);
} else if (format === 'x8') {
return b.toString('hex', 13, 14);
} else if (format === 'x9') {
return b.toString('hex', 14, 15);
} else if (format === 'x10') {
return b.toString('hex', 15, 16);
} else {
return b.toString('hex', 0, 4) + '-' +
b.toString('hex', 4, 6) + '-' +
Expand Down
26 changes: 26 additions & 0 deletions src/test/suite/guid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,30 @@ suite('Guid', () => {
const g = new Guid('01234567-89ab-cdef-0123-456789abcdef');
assert.strictEqual(g.toString(undefined), '01234567-89ab-cdef-0123-456789abcdef');
});

test('returns proper string for individual parts format', () => {
const g = new Guid('9f0a1b2c3d-4e5f-6a7b-8c9d-0e1f2a3b4c5d');
assert.strictEqual(g.toString('x0'), '9f0a1b2c3d');
assert.strictEqual(g.toString('x1'), '4e5f');
assert.strictEqual(g.toString('x2'), '6a7b');
assert.strictEqual(g.toString('x3'), '8c');
assert.strictEqual(g.toString('x4'), '9d');
assert.strictEqual(g.toString('x5'), '0e');
assert.strictEqual(g.toString('x6'), '1f');
assert.strictEqual(g.toString('x7'), '2a');
assert.strictEqual(g.toString('x8'), '3b');
assert.strictEqual(g.toString('x9'), '4c');
assert.strictEqual(g.toString('x10'), '5d');
assert.strictEqual(g.toString('X0'), '9F0A1B2C3D');
assert.strictEqual(g.toString('X1'), '4E5F');
assert.strictEqual(g.toString('X2'), '6A7b');
assert.strictEqual(g.toString('X3'), '8C');
assert.strictEqual(g.toString('X4'), '9D');
assert.strictEqual(g.toString('X5'), '0E');
assert.strictEqual(g.toString('X6'), '1F');
assert.strictEqual(g.toString('X7'), '2A');
assert.strictEqual(g.toString('X8'), '3B');
assert.strictEqual(g.toString('X9'), '4C');
assert.strictEqual(g.toString('X10'), '5D');
});
});
Loading