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 snakecase to snippets syntax #237110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 9 additions & 9 deletions extensions/json/syntaxes/snippets.tmLanguage.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change: No diff generated, but the file is marked as modified.
Impact: Likely includes the addition of the snakecase transformation rule.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/vs/editor/contrib/snippet/browser/snippet.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification: Added snakecase to the list of supported transformations.

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ variable ::= '$' var | '${' var }'
| '${' var transform '}'
transform ::= '/' regex '/' (format | text)+ '/' options
format ::= '$' int | '${' int '}'
| '${' int ':' '/upcase' | '/downcase' | '/capitalize' | '/camelcase' | '/pascalcase' '}'
| '${' int ':' '/upcase' | '/downcase' | '/capitalize' | '/camelcase' | '/pascalcase' | '/snakecase' '}'
| '${' int ':+' if '}'
| '${' int ':?' if ':' else '}'
| '${' int ':-' else '}' | '${' int ':' else '}'
Expand Down
8 changes: 8 additions & 0 deletions src/vs/editor/contrib/snippet/browser/snippetParser.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification: Added the _toSnakeCase method and integrated it into the FormatString class.

Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ export class FormatString extends Marker {
return !value ? '' : this._toPascalCase(value);
} else if (this.shorthandName === 'camelcase') {
return !value ? '' : this._toCamelCase(value);
} else if (this.shorthandName === 'snakecase') {
return !value ? '' : this._toSnakeCase(value);
} else if (Boolean(value) && typeof this.ifValue === 'string') {
return this.ifValue;
} else if (!Boolean(value) && typeof this.elseValue === 'string') {
Expand Down Expand Up @@ -421,6 +423,12 @@ export class FormatString extends Marker {
.join('');
}

private _toSnakeCase(value: string): string {
return value.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/[\s\-]+/g, '_')
.toLowerCase();
}

toTextmateString(): string {
let value = '${';
value += this.index;
Expand Down

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification: Added test cases for the snakecase transformation.

Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ suite('SnippetParser', () => {
assert.strictEqual(new FormatString(1, 'camelcase').resolve('snake_AndCamelCase'), 'snakeAndCamelCase');
assert.strictEqual(new FormatString(1, 'camelcase').resolve('kebab-AndCamelCase'), 'kebabAndCamelCase');
assert.strictEqual(new FormatString(1, 'camelcase').resolve('_JustCamelCase'), 'justCamelCase');
assert.strictEqual(new FormatString(1, 'snakecase').resolve('bar-foo'), 'bar_foo');
assert.strictEqual(new FormatString(1, 'snakecase').resolve('bar-42-foo'), 'bar_42_foo');
assert.strictEqual(new FormatString(1, 'snakecase').resolve('snake_AndPascalCase'), 'snake_and_pascal_case');
assert.strictEqual(new FormatString(1, 'snakecase').resolve('kebab-AndPascalCase'), 'kebab_and_pascal_case');
assert.strictEqual(new FormatString(1, 'snakecase').resolve('_justPascalCase'), '_just_pascal_case');
assert.strictEqual(new FormatString(1, 'notKnown').resolve('input'), 'input');

// if
Expand Down