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 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
TEST_RESULTS_PATH: ${{ github.workspace }}/.vscode-test/test-results.xml

- name: Publish test results
if: github.repository_owner == 'heaths'
if: ${{ ! github.event.pull_request.head.repo.fork }}
uses: dorny/test-reporter@v1
with:
name: test-results-${{ matrix.os }}-${{ matrix.vscode-channel }}
Expand Down
3 changes: 3 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"clear": true
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.9.0 (2024-02-08)

* [@merlinz01](https://github.com/merlinz01) added more custom format specifiers ([#85](https://github.com/heaths/vscode-guid/pull/85))

## 1.8.1 (2024-01-09)

* Fixed CI badge on [Marketplace](https://marketplace.visualstudio.com/items?itemName=heaths.vscode-guid).
Expand Down
29 changes: 28 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 as [described below](#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,33 @@ 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 "". The formatting options are:
* `{b}|{B}` inserts a braced string in lowercase `{b}` or uppercase `{B}` e.g., `{880c86bc-384c-4cce-9e9a-4f760ca755c4}`
* `{d}|{D}` inserts a hyphenated string in lowercase `{d}` or uppercase `{D}` e.g., `880c86bc-384c-4cce-9e9a-4f760ca755c4`
* `{n}|{N}` inserts an unformatted string in lowercase `{n}` or uppercase `{N}` e.g., `880c86bc384c4cce9e9a4f760ca755c4`
* `{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}}`
* `{x0}|{X0}` inserts the first four bytes as a hexadecimal string in lowercase `{x0}` or uppercase `{X0}` e.g., `0x880c86bc`
* `{x1}|{X1}` and `{x2}|{X2}` insert the second and third two bytes as a hexadecimal string in lowercase `{x1}` and `{x2}`, or uppercase `{X1}` and `{X2}` e.g., `0x384c` and `0x4cce`
* `{x3}|{X3}` through `{x10}|{X10}` insert the subsequent bytes individually as a hexadecimal string in lowercase `{x3}` or uppercase `{X3}` e.g., `0x9e` through `0xc4`.
* `{nl}` inserts a new line
* All other characters will be interpreted literally e.g., `new GUID("{D}")` inserts `new GUID("880C86BC-384C-4CCE-9E9A-4F760CA755C4")`

For example, to generate a `GUID` for the [windows](https://crates.io/crates/windows) crate, you could define:

```json
"const G: ::windows::core::GUID = ::windows::core::GUID {{nl} data1: 0x{x0},{nl} data2: 0x{x1},{nl} data3: 0x{x2},{nl} data4: [0x{x3}, 0x{x4}, 0x{x5}, 0x{x6}, 0x{x7}, 0x{x8}, 0x{x9}, 0x{x10}],{nl}};"
```

Which would insert a `GUID` like:

```rust
const G: ::windows::core::GUID = ::windows::core::GUID {
data1: 0x9f0a1b2c,
data2: 0x4e5f,
data3: 0x6a7b,
data4: [0x8c, 0x9d, 0x0e, 0x1f, 0x2a, 0x3b, 0x4c, 0x5d],
};
```

## 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}|{X0}` inserts the first four bytes as a hexadecimal string in lowercase `{x0}` or uppercase `{X0}` e.g., `0x880c86bc`\n* `{x1}|{X1}` and `{x2}|{X2}` insert the second and third two bytes as a hexadecimal string in lowercase `{x1}` and `{x2}`, or uppercase `{X1}` and `{X2}` e.g., `0x384c` and `0x4cce`\n* `{x3}|{X3}` through `{x10}|{X10}` insert the subsequent bytes individually as a hexadecimal string in lowercase `{x3}` or uppercase `{X3}` e.g., `0x9e` through `0xc4`.\n* `{nl}` inserts a new line\n* All other characters will be interpreted literally e.g., `new GUID(\"{D}\")` inserts `new GUID(\"880C86BC-384C-4CCE-9E9A-4F760CA755C4\")`"
}
}
},
Expand Down
20 changes: 1 addition & 19 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,26 +203,8 @@ async function insertCommandImpl(textEditor: vscode.TextEditor, edit: vscode.Tex

if (pasteAutomatically !== '') {
// Format with the specified string and insert without user selection
interface Dict { [key: string]: (g: Guid) => string }
const replacements: Dict = {
'{b}': (g: Guid) => g.toString('braced'),
'{B}': (g: Guid) => g.toString('braced').toUpperCase(),
'{d}': (g: Guid) => g.toString(),
'{D}': (g: Guid) => g.toString().toUpperCase(),
'{n}': (g: Guid) => g.toString('no-hyphen'),
'{N}': (g: Guid) => g.toString('no-hyphen').toUpperCase(),
'{x}': (g: Guid) => g.toString('x'),
'{X}': (g: Guid) => g.toString('x').toUpperCase(),
}
const customFormatter = {
format: (g: Guid) => {
let ret = pasteAutomatically;
for (const replacement in replacements) {
const fn = replacements[replacement]
ret = ret.replace(replacement, fn(g));
}
return ret;
},
format: (g: Guid) => g.format(pasteAutomatically),
type: FormatType.CUSTOM
}
item = new GuidPickItem(-1, g, customFormatter)
Expand Down
68 changes: 68 additions & 0 deletions src/guid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ export class Guid {
b.toString('hex', 10, 16);
}

/**
* Replaces format specifiers according to the package README.md.
* @returns A string with all known format specifiers replaced as directed.
*/
format(format: string): string {
const replacements: { [key: string]: (g: Guid) => string } = {
'{b}': (g: Guid) => g.toString('braced'),
'{B}': (g: Guid) => g.toString('braced').toUpperCase(),
'{d}': (g: Guid) => g.toString(),
'{D}': (g: Guid) => g.toString().toUpperCase(),
'{n}': (g: Guid) => g.toString('no-hyphen'),
'{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(),
};

let ret = format;
for (const replacement in replacements) {
const fn = replacements[replacement]
ret = ret.replace(replacement, fn(this));
}
return ret.replace(/\{nl\}/g, '\n');
}

/**
* Returns the raw _buffer.
* @returns The raw _buffer.
Expand All @@ -94,6 +140,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
27 changes: 27 additions & 0 deletions src/test/suite/guid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,31 @@ 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('9f0a1b2c-4e5f-6a7b-8c9d-0e1f2a3b4c5d');
assert.strictEqual(g.toString('x0'), '9f0a1b2c');
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');
});

test('returns proper string with format specifiers replaced', () => {
const g = new Guid('9f0a1b2c-4e5f-6a7b-8c9d-0e1f2a3b4c5d');
const format = 'const G: ::windows::core::GUID = ::windows::core::GUID {{nl} data1: 0x{x0},{nl} data2: 0x{x1},{nl} data3: 0x{x2},{nl} data4: [0x{x3}, 0x{x4}, 0x{x5}, 0x{x6}, 0x{x7}, 0x{x8}, 0x{x9}, 0x{x10}],{nl}};';
const expected = `const G: ::windows::core::GUID = ::windows::core::GUID {
data1: 0x9f0a1b2c,
data2: 0x4e5f,
data3: 0x6a7b,
data4: [0x8c, 0x9d, 0x0e, 0x1f, 0x2a, 0x3b, 0x4c, 0x5d],
};`;
assert.strictEqual(g.format(format), expected);
});
});
Loading