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 builder-method for snippet choice #84048

Merged
merged 4 commits into from Nov 13, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/vs/vscode.d.ts
Expand Up @@ -2967,6 +2967,17 @@ declare module 'vscode' {
*/
appendPlaceholder(value: string | ((snippet: SnippetString) => any), number?: number): SnippetString;

/**
* Builder-function that appends a choice (`${1|a,b,c}`) to
* the [`value`](#SnippetString.value) of this snippet string.
*
* @param values The values for choices - the array of strings
* @param number The number of this tabstop, defaults to an auto-increment
* value starting at 1.
* @return This snippet string.
*/
appendChoice(values: string[], number?: number): SnippetString;

/**
* Builder-function that appends a variable (`${VAR}`) to
* the [`value`](#SnippetString.value) of this snippet string.
Expand Down
12 changes: 12 additions & 0 deletions src/vs/workbench/api/common/extHostTypes.ts
Expand Up @@ -740,6 +740,18 @@ export class SnippetString {
return this;
}

appendChoice(values: string[], number: number = this._tabstop++): SnippetString {
const value = SnippetString._escape(values.toString());

this.value += '${';
this.value += number;
this.value += '|';
this.value += value;
this.value += '|}';

return this;
}

appendVariable(name: string, defaultValue?: string | ((snippet: SnippetString) => any)): SnippetString {

if (typeof defaultValue === 'function') {
Expand Down
19 changes: 19 additions & 0 deletions src/vs/workbench/test/electron-browser/api/extHostTypes.test.ts
Expand Up @@ -527,6 +527,25 @@ suite('ExtHostTypes', function () {
string.appendVariable('BAR', b => { });
assert.equal(string.value, '${BAR}');

string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r']);
assert.equal(string.value, '${1|b,a,r|}');

string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r'], 0);
assert.equal(string.value, '${0|b,a,r|}');

string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,boo|}bar');

string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', '$boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,\\$boo|}bar');

string = new types.SnippetString();
string.appendText('foo').appendPlaceholder('farboo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1:farboo}${2|far,boo|}bar');
});

test('instanceof doesn\'t work for FileSystemError #49386', function () {
Expand Down